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: multiget.*}")
97 public MultiRowResource getMultipleRowResource(final @QueryParam("v") String versions,
98 @PathParam("multiget") String path) throws IOException {
99 return new MultiRowResource(this, versions, path.replace("multiget", "").replace("/", ""));
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,
109 final @QueryParam("rr") String returnResult) throws IOException {
110 return new RowResource(this, rowspec, versions, check, returnResult);
111 }
112
113 @Path("{suffixglobbingspec: .*\\*/.+}")
114 public RowResource getRowResourceWithSuffixGlobbing(
115
116
117 final @PathParam("suffixglobbingspec") @Encoded String suffixglobbingspec,
118 final @QueryParam("v") String versions,
119 final @QueryParam("check") String check,
120 final @QueryParam("rr") String returnResult) throws IOException {
121 return new RowResource(this, suffixglobbingspec, versions, check, returnResult);
122 }
123
124 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="REC_CATCH_EXCEPTION")
125 @Path("{scanspec: .*[*]$}")
126 public TableScanResource getScanResource(
127 final @PathParam("scanspec") String scanSpec,
128 @DefaultValue(Integer.MAX_VALUE + "")
129 @QueryParam(Constants.SCAN_LIMIT) int userRequestedLimit,
130 @DefaultValue("") @QueryParam(Constants.SCAN_START_ROW) String startRow,
131 @DefaultValue("") @QueryParam(Constants.SCAN_END_ROW) String endRow,
132 @DefaultValue("") @QueryParam(Constants.SCAN_COLUMN) List<String> column,
133 @DefaultValue("1") @QueryParam(Constants.SCAN_MAX_VERSIONS) int maxVersions,
134 @DefaultValue("-1") @QueryParam(Constants.SCAN_BATCH_SIZE) int batchSize,
135 @DefaultValue("0") @QueryParam(Constants.SCAN_START_TIME) long startTime,
136 @DefaultValue(Long.MAX_VALUE + "") @QueryParam(Constants.SCAN_END_TIME) long endTime,
137 @DefaultValue("true") @QueryParam(Constants.SCAN_CACHE_BLOCKS) boolean cacheBlocks,
138 @DefaultValue("false") @QueryParam(Constants.SCAN_REVERSED) boolean reversed,
139 @DefaultValue("") @QueryParam(Constants.SCAN_FILTER) String paramFilter) {
140 try {
141 Filter prefixFilter = null;
142 Scan tableScan = new Scan();
143 if (scanSpec.indexOf('*') > 0) {
144 String prefix = scanSpec.substring(0, scanSpec.indexOf('*'));
145 byte[] prefixBytes = Bytes.toBytes(prefix);
146 prefixFilter = new PrefixFilter(Bytes.toBytes(prefix));
147 if (startRow.isEmpty()) {
148 tableScan.setStartRow(prefixBytes);
149 }
150 }
151 if (LOG.isTraceEnabled()) {
152 LOG.trace("Query parameters : Table Name = > " + this.table + " Start Row => " + startRow
153 + " End Row => " + endRow + " Columns => " + column + " Start Time => " + startTime
154 + " End Time => " + endTime + " Cache Blocks => " + cacheBlocks + " Max Versions => "
155 + maxVersions + " Batch Size => " + batchSize);
156 }
157 Table hTable = RESTServlet.getInstance().getTable(this.table);
158 tableScan.setBatch(batchSize);
159 tableScan.setMaxVersions(maxVersions);
160 tableScan.setTimeRange(startTime, endTime);
161 if (!startRow.isEmpty()) {
162 tableScan.setStartRow(Bytes.toBytes(startRow));
163 }
164 tableScan.setStopRow(Bytes.toBytes(endRow));
165 for (String csplit : column) {
166 String[] familysplit = csplit.trim().split(":");
167 if (familysplit.length == 2) {
168 if (familysplit[1].length() > 0) {
169 if (LOG.isTraceEnabled()) {
170 LOG.trace("Scan family and column : " + familysplit[0] + " " + familysplit[1]);
171 }
172 tableScan.addColumn(Bytes.toBytes(familysplit[0]), Bytes.toBytes(familysplit[1]));
173 } else {
174 tableScan.addFamily(Bytes.toBytes(familysplit[0]));
175 if (LOG.isTraceEnabled()) {
176 LOG.trace("Scan family : " + familysplit[0] + " and empty qualifier.");
177 }
178 tableScan.addColumn(Bytes.toBytes(familysplit[0]), null);
179 }
180 } else if (StringUtils.isNotEmpty(familysplit[0])) {
181 if (LOG.isTraceEnabled()) {
182 LOG.trace("Scan family : " + familysplit[0]);
183 }
184 tableScan.addFamily(Bytes.toBytes(familysplit[0]));
185 }
186 }
187
188 FilterList filterList = new FilterList();
189 if (StringUtils.isNotEmpty(paramFilter)) {
190 ParseFilter pf = new ParseFilter();
191 Filter parsedParamFilter = pf.parseFilterString(paramFilter);
192 if (parsedParamFilter != null) {
193 filterList.addFilter(parsedParamFilter);
194 }
195 if (prefixFilter != null) {
196 filterList.addFilter(prefixFilter);
197 }
198 }
199
200 if (filterList.getFilters().size() > 0) {
201 tableScan.setFilter(filterList);
202 }
203
204 int fetchSize = this.servlet.getConfiguration().getInt(Constants.SCAN_FETCH_SIZE, 10);
205 tableScan.setCaching(fetchSize);
206 tableScan.setReversed(reversed);
207 tableScan.setCacheBlocks(cacheBlocks);
208 return new TableScanResource(hTable.getScanner(tableScan), userRequestedLimit);
209 } catch (Exception exp) {
210 servlet.getMetrics().incrementFailedScanRequests(1);
211 processException(exp);
212 LOG.warn(exp);
213 return null;
214 }
215 }
216 }