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.TableName;
38 import org.apache.hadoop.hbase.classification.InterfaceAudience;
39 import org.apache.hadoop.hbase.client.Scan;
40 import org.apache.hadoop.hbase.client.Table;
41 import org.apache.hadoop.hbase.filter.Filter;
42 import org.apache.hadoop.hbase.filter.FilterList;
43 import org.apache.hadoop.hbase.filter.ParseFilter;
44 import org.apache.hadoop.hbase.filter.PrefixFilter;
45 import org.apache.hadoop.hbase.util.Bytes;
46
47 @InterfaceAudience.Private
48 public class TableResource extends ResourceBase {
49
50 String table;
51 private static final Log LOG = LogFactory.getLog(TableResource.class);
52
53
54
55
56
57
58 public TableResource(String table) throws IOException {
59 super();
60 this.table = table;
61 }
62
63
64 String getName() {
65 return table;
66 }
67
68
69
70
71
72 boolean exists() throws IOException {
73 return servlet.getAdmin().tableExists(TableName.valueOf(table));
74 }
75
76 @Path("exists")
77 public ExistsResource getExistsResource() throws IOException {
78 return new ExistsResource(this);
79 }
80
81 @Path("regions")
82 public RegionsResource getRegionsResource() throws IOException {
83 return new RegionsResource(this);
84 }
85
86 @Path("scanner")
87 public ScannerResource getScannerResource() throws IOException {
88 return new ScannerResource(this);
89 }
90
91 @Path("schema")
92 public SchemaResource getSchemaResource() throws IOException {
93 return new SchemaResource(this);
94 }
95
96 @Path("multiget")
97 public MultiRowResource getMultipleRowResource(
98 final @QueryParam("v") String versions) throws IOException {
99 return new MultiRowResource(this, versions);
100 }
101
102 @Path("{rowspec: [^*]+}")
103 public RowResource getRowResource(
104
105
106 final @PathParam("rowspec") @Encoded String rowspec,
107 final @QueryParam("v") String versions,
108 final @QueryParam("check") String check) throws IOException {
109 return new RowResource(this, rowspec, versions, check);
110 }
111
112 @Path("{suffixglobbingspec: .*\\*/.+}")
113 public RowResource getRowResourceWithSuffixGlobbing(
114
115
116 final @PathParam("suffixglobbingspec") @Encoded String suffixglobbingspec,
117 final @QueryParam("v") String versions,
118 final @QueryParam("check") String check) throws IOException {
119 return new RowResource(this, suffixglobbingspec, versions, check);
120 }
121
122 @Path("{scanspec: .*[*]$}")
123 public TableScanResource getScanResource(
124 final @Context UriInfo uriInfo,
125 final @PathParam("scanspec") String scanSpec,
126 final @HeaderParam("Accept") String contentType,
127 @DefaultValue(Integer.MAX_VALUE + "")
128 @QueryParam(Constants.SCAN_LIMIT) int userRequestedLimit,
129 @DefaultValue("") @QueryParam(Constants.SCAN_START_ROW) String startRow,
130 @DefaultValue("") @QueryParam(Constants.SCAN_END_ROW) String endRow,
131 @DefaultValue("") @QueryParam(Constants.SCAN_COLUMN) List<String> column,
132 @DefaultValue("1") @QueryParam(Constants.SCAN_MAX_VERSIONS) int maxVersions,
133 @DefaultValue("-1") @QueryParam(Constants.SCAN_BATCH_SIZE) int batchSize,
134 @DefaultValue("0") @QueryParam(Constants.SCAN_START_TIME) long startTime,
135 @DefaultValue(Long.MAX_VALUE + "") @QueryParam(Constants.SCAN_END_TIME) long endTime,
136 @DefaultValue("true") @QueryParam(Constants.SCAN_BATCH_SIZE) boolean cacheBlocks,
137 @DefaultValue("false") @QueryParam(Constants.SCAN_REVERSED) boolean reversed,
138 @DefaultValue("") @QueryParam(Constants.SCAN_FILTER) String filters) {
139 try {
140 Filter filter = null;
141 Scan tableScan = new Scan();
142 if (scanSpec.indexOf('*') > 0) {
143 String prefix = scanSpec.substring(0, scanSpec.indexOf('*'));
144 byte[] prefixBytes = Bytes.toBytes(prefix);
145 filter = new PrefixFilter(Bytes.toBytes(prefix));
146 if (startRow.isEmpty()) {
147 tableScan.setStartRow(prefixBytes);
148 }
149 }
150 LOG.debug("Query parameters : Table Name = > " + this.table + " Start Row => " + startRow
151 + " End Row => " + endRow + " Columns => " + column + " Start Time => " + startTime
152 + " End Time => " + endTime + " Cache Blocks => " + cacheBlocks + " Max Versions => "
153 + maxVersions + " Batch Size => " + batchSize);
154 Table hTable = RESTServlet.getInstance().getTable(this.table);
155 tableScan.setBatch(batchSize);
156 tableScan.setMaxVersions(maxVersions);
157 tableScan.setTimeRange(startTime, endTime);
158 if (!startRow.isEmpty()) {
159 tableScan.setStartRow(Bytes.toBytes(startRow));
160 }
161 tableScan.setStopRow(Bytes.toBytes(endRow));
162 for (String csplit : column) {
163 String[] familysplit = csplit.trim().split(":");
164 if (familysplit.length == 2) {
165 if (familysplit[1].length() > 0) {
166 LOG.debug("Scan family and column : " + familysplit[0] + " " + familysplit[1]);
167 tableScan.addColumn(Bytes.toBytes(familysplit[0]), Bytes.toBytes(familysplit[1]));
168 } else {
169 tableScan.addFamily(Bytes.toBytes(familysplit[0]));
170 LOG.debug("Scan family : " + familysplit[0] + " and empty qualifier.");
171 tableScan.addColumn(Bytes.toBytes(familysplit[0]), null);
172 }
173 } else if (StringUtils.isNotEmpty(familysplit[0])){
174 LOG.debug("Scan family : " + familysplit[0]);
175 tableScan.addFamily(Bytes.toBytes(familysplit[0]));
176 }
177 }
178 FilterList filterList = null;
179 if (StringUtils.isNotEmpty(filters)) {
180 ParseFilter pf = new ParseFilter();
181 Filter filterParam = pf.parseFilterString(filters);
182 if (filter != null) {
183 filterList = new FilterList(filter, filterParam);
184 }
185 else {
186 filter = filterParam;
187 }
188 }
189 if (filterList != null) {
190 tableScan.setFilter(filterList);
191 } else if (filter != null) {
192 tableScan.setFilter(filter);
193 }
194 int fetchSize = this.servlet.getConfiguration().getInt(Constants.SCAN_FETCH_SIZE, 10);
195 tableScan.setCaching(fetchSize);
196 tableScan.setReversed(reversed);
197 return new TableScanResource(hTable.getScanner(tableScan), userRequestedLimit);
198 } catch (Exception exp) {
199 servlet.getMetrics().incrementFailedScanRequests(1);
200 processException(exp);
201 LOG.warn(exp);
202 return null;
203 }
204 }
205 }