View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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     * Constructor
55     * @param table
56     * @throws IOException
57     */
58    public TableResource(String table) throws IOException {
59      super();
60      this.table = table;
61    }
62  
63    /** @return the table name */
64    String getName() {
65      return table;
66    }
67  
68    /**
69     * @return true if the table exists
70     * @throws IOException
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       // We need the @Encoded decorator so Jersey won't urldecode before
105       // the RowSpec constructor has a chance to parse
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       // We need the @Encoded decorator so Jersey won't urldecode before
115       // the RowSpec constructor has a chance to parse
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       LOG.debug("Query parameters  : Table Name = > " + this.table + " Start Row => " + startRow
150           + " End Row => " + endRow + " Columns => " + column + " Start Time => " + startTime
151           + " End Time => " + endTime + " Cache Blocks => " + cacheBlocks + " Max Versions => "
152           + maxVersions + " Batch Size => " + batchSize);
153       Table hTable = RESTServlet.getInstance().getTable(this.table);
154       tableScan.setBatch(batchSize);
155       tableScan.setMaxVersions(maxVersions);
156       tableScan.setTimeRange(startTime, endTime);
157       if (!startRow.isEmpty()) {
158         tableScan.setStartRow(Bytes.toBytes(startRow));
159       }
160       tableScan.setStopRow(Bytes.toBytes(endRow));
161       for (String csplit : column) {
162         String[] familysplit = csplit.trim().split(":");
163         if (familysplit.length == 2) {
164           if (familysplit[1].length() > 0) {
165             LOG.debug("Scan family and column : " + familysplit[0] + "  " + familysplit[1]);
166             tableScan.addColumn(Bytes.toBytes(familysplit[0]), Bytes.toBytes(familysplit[1]));
167           } else {
168             tableScan.addFamily(Bytes.toBytes(familysplit[0]));
169             LOG.debug("Scan family : " + familysplit[0] + " and empty qualifier.");
170             tableScan.addColumn(Bytes.toBytes(familysplit[0]), null);
171           }
172         } else if (StringUtils.isNotEmpty(familysplit[0])){
173           LOG.debug("Scan family : " + familysplit[0]);
174           tableScan.addFamily(Bytes.toBytes(familysplit[0]));
175         }
176       }
177       FilterList filterList = null;
178       if (StringUtils.isNotEmpty(filters)) {
179           ParseFilter pf = new ParseFilter();
180           Filter filterParam = pf.parseFilterString(filters);
181           if (filter != null) {
182             filterList = new FilterList(filter, filterParam);
183           }
184           else {
185             filter = filterParam;
186           }
187       }
188       if (filterList != null) {
189         tableScan.setFilter(filterList);
190       } else if (filter != null) {
191         tableScan.setFilter(filter);
192       }
193       int fetchSize = this.servlet.getConfiguration().getInt(Constants.SCAN_FETCH_SIZE, 10);
194       tableScan.setCaching(fetchSize);
195      return new TableScanResource(hTable.getScanner(tableScan), userRequestedLimit);
196     } catch (Exception exp) {
197       servlet.getMetrics().incrementFailedScanRequests(1);
198       processException(exp);
199       LOG.warn(exp);
200       return null;
201     }
202   }
203 }