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("") @QueryParam(Constants.SCAN_FILTER) String filters) {
138 try {
139 Filter filter = null;
140 Scan tableScan = new Scan();
141 if (scanSpec.indexOf('*') > 0) {
142 String prefix = scanSpec.substring(0, scanSpec.indexOf('*'));
143 byte[] prefixBytes = Bytes.toBytes(prefix);
144 filter = new PrefixFilter(Bytes.toBytes(prefix));
145 if (startRow.isEmpty()) {
146 tableScan.setStartRow(prefixBytes);
147 }
148 }
149 if (LOG.isTraceEnabled()) {
150 LOG.trace("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 }
155 Table hTable = RESTServlet.getInstance().getTable(this.table);
156 tableScan.setBatch(batchSize);
157 tableScan.setMaxVersions(maxVersions);
158 tableScan.setTimeRange(startTime, endTime);
159 if (!startRow.isEmpty()) {
160 tableScan.setStartRow(Bytes.toBytes(startRow));
161 }
162 tableScan.setStopRow(Bytes.toBytes(endRow));
163 for (String csplit : column) {
164 String[] familysplit = csplit.trim().split(":");
165 if (familysplit.length == 2) {
166 if (familysplit[1].length() > 0) {
167 if (LOG.isTraceEnabled()) {
168 LOG.trace("Scan family and column : " + familysplit[0] + " " + familysplit[1]);
169 }
170 tableScan.addColumn(Bytes.toBytes(familysplit[0]), Bytes.toBytes(familysplit[1]));
171 } else {
172 tableScan.addFamily(Bytes.toBytes(familysplit[0]));
173 if (LOG.isTraceEnabled()) {
174 LOG.trace("Scan family : " + familysplit[0] + " and empty qualifier.");
175 }
176 tableScan.addColumn(Bytes.toBytes(familysplit[0]), null);
177 }
178 } else if (StringUtils.isNotEmpty(familysplit[0])) {
179 if (LOG.isTraceEnabled()) {
180 LOG.trace("Scan family : " + familysplit[0]);
181 }
182 tableScan.addFamily(Bytes.toBytes(familysplit[0]));
183 }
184 }
185 FilterList filterList = null;
186 if (StringUtils.isNotEmpty(filters)) {
187 ParseFilter pf = new ParseFilter();
188 Filter filterParam = pf.parseFilterString(filters);
189 if (filter != null) {
190 filterList = new FilterList(filter, filterParam);
191 }
192 else {
193 filter = filterParam;
194 }
195 }
196 if (filterList != null) {
197 tableScan.setFilter(filterList);
198 } else if (filter != null) {
199 tableScan.setFilter(filter);
200 }
201 int fetchSize = this.servlet.getConfiguration().getInt(Constants.SCAN_FETCH_SIZE, 10);
202 tableScan.setCaching(fetchSize);
203 return new TableScanResource(hTable.getScanner(tableScan), userRequestedLimit);
204 } catch (Exception exp) {
205 servlet.getMetrics().incrementFailedScanRequests(1);
206 processException(exp);
207 LOG.warn(exp);
208 return null;
209 }
210 }
211 }