1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.rest;
21
22 import java.io.IOException;
23 import java.util.List;
24
25 import javax.ws.rs.DefaultValue;
26 import javax.ws.rs.Encoded;
27 import javax.ws.rs.HeaderParam;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.PathParam;
30 import javax.ws.rs.QueryParam;
31 import javax.ws.rs.core.Context;
32 import javax.ws.rs.core.UriInfo;
33
34 import org.apache.commons.lang.StringUtils;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.apache.hadoop.hbase.classification.InterfaceAudience;
38 import org.apache.hadoop.hbase.client.HTableInterface;
39 import org.apache.hadoop.hbase.client.Scan;
40 import org.apache.hadoop.hbase.filter.Filter;
41 import org.apache.hadoop.hbase.filter.PrefixFilter;
42 import org.apache.hadoop.hbase.util.Bytes;
43
44 @InterfaceAudience.Private
45 public class TableResource extends ResourceBase {
46
47 String table;
48 private static final Log LOG = LogFactory.getLog(TableResource.class);
49
50
51
52
53
54
55 public TableResource(String table) throws IOException {
56 super();
57 this.table = table;
58 }
59
60
61 String getName() {
62 return table;
63 }
64
65
66
67
68
69 boolean exists() throws IOException {
70 return servlet.getAdmin().tableExists(table);
71 }
72
73 @Path("exists")
74 public ExistsResource getExistsResource() throws IOException {
75 return new ExistsResource(this);
76 }
77
78 @Path("regions")
79 public RegionsResource getRegionsResource() throws IOException {
80 return new RegionsResource(this);
81 }
82
83 @Path("scanner")
84 public ScannerResource getScannerResource() throws IOException {
85 return new ScannerResource(this);
86 }
87
88 @Path("schema")
89 public SchemaResource getSchemaResource() throws IOException {
90 return new SchemaResource(this);
91 }
92
93 @Path("multiget")
94 public MultiRowResource getMultipleRowResource(
95 final @QueryParam("v") String versions) throws IOException {
96 return new MultiRowResource(this, versions);
97 }
98
99 @Path("{rowspec: [^*]+}")
100 public RowResource getRowResource(
101
102
103 final @PathParam("rowspec") @Encoded String rowspec,
104 final @QueryParam("v") String versions,
105 final @QueryParam("check") String check) throws IOException {
106 return new RowResource(this, rowspec, versions, check);
107 }
108
109 @Path("{suffixglobbingspec: .*\\*/.+}")
110 public RowResource getRowResourceWithSuffixGlobbing(
111
112
113 final @PathParam("suffixglobbingspec") @Encoded String suffixglobbingspec,
114 final @QueryParam("v") String versions,
115 final @QueryParam("check") String check) throws IOException {
116 return new RowResource(this, suffixglobbingspec, versions, check);
117 }
118
119 @Path("{scanspec: .*[*]$}")
120 public TableScanResource getScanResource(
121 final @Context UriInfo uriInfo,
122 final @PathParam("scanspec") String scanSpec,
123 final @HeaderParam("Accept") String contentType,
124 @DefaultValue(Integer.MAX_VALUE + "")
125 @QueryParam(Constants.SCAN_LIMIT) int userRequestedLimit,
126 @DefaultValue("") @QueryParam(Constants.SCAN_START_ROW) String startRow,
127 @DefaultValue("") @QueryParam(Constants.SCAN_END_ROW) String endRow,
128 @DefaultValue("") @QueryParam(Constants.SCAN_COLUMN) List<String> column,
129 @DefaultValue("1") @QueryParam(Constants.SCAN_MAX_VERSIONS) int maxVersions,
130 @DefaultValue("-1") @QueryParam(Constants.SCAN_BATCH_SIZE) int batchSize,
131 @DefaultValue("0") @QueryParam(Constants.SCAN_START_TIME) long startTime,
132 @DefaultValue(Long.MAX_VALUE + "") @QueryParam(Constants.SCAN_END_TIME) long endTime,
133 @DefaultValue("true") @QueryParam(Constants.SCAN_BATCH_SIZE) boolean cacheBlocks) {
134 try {
135 Filter filter = null;
136 if (scanSpec.indexOf('*') > 0) {
137 String prefix = scanSpec.substring(0, scanSpec.indexOf('*'));
138 filter = new PrefixFilter(Bytes.toBytes(prefix));
139 }
140 LOG.debug("Query parameters : Table Name = > " + this.table + " Start Row => " + startRow
141 + " End Row => " + endRow + " Columns => " + column + " Start Time => " + startTime
142 + " End Time => " + endTime + " Cache Blocks => " + cacheBlocks + " Max Versions => "
143 + maxVersions + " Batch Size => " + batchSize);
144 HTableInterface hTable = RESTServlet.getInstance().getTable(this.table);
145 Scan tableScan = new Scan();
146 tableScan.setBatch(batchSize);
147 tableScan.setMaxVersions(maxVersions);
148 tableScan.setTimeRange(startTime, endTime);
149 tableScan.setStartRow(Bytes.toBytes(startRow));
150 tableScan.setStopRow(Bytes.toBytes(endRow));
151 for (String csplit : column) {
152 String[] familysplit = csplit.trim().split(":");
153 if (familysplit.length == 2) {
154 if (familysplit[1].length() > 0) {
155 LOG.debug("Scan family and column : " + familysplit[0] + " " + familysplit[1]);
156 tableScan.addColumn(Bytes.toBytes(familysplit[0]), Bytes.toBytes(familysplit[1]));
157 } else {
158 tableScan.addFamily(Bytes.toBytes(familysplit[0]));
159 LOG.debug("Scan family : " + familysplit[0] + " and empty qualifier.");
160 tableScan.addColumn(Bytes.toBytes(familysplit[0]), null);
161 }
162 } else if (StringUtils.isNotEmpty(familysplit[0])){
163 LOG.debug("Scan family : " + familysplit[0]);
164 tableScan.addFamily(Bytes.toBytes(familysplit[0]));
165 }
166 }
167 if (filter != null) {
168 tableScan.setFilter(filter);
169 }
170 int fetchSize = this.servlet.getConfiguration().getInt(Constants.SCAN_FETCH_SIZE, 10);
171 tableScan.setCaching(fetchSize);
172 return new TableScanResource(hTable.getScanner(tableScan), userRequestedLimit);
173 } catch (Exception exp) {
174 servlet.getMetrics().incrementFailedScanRequests(1);
175 processException(exp);
176 LOG.warn(exp);
177 return null;
178 }
179 }
180 }