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   @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="REC_CATCH_EXCEPTION")
123   @Path("{scanspec: .*[*]$}")
124   public TableScanResource  getScanResource(
125       final @Context UriInfo uriInfo,
126       final @PathParam("scanspec") String scanSpec,
127       final @HeaderParam("Accept") String contentType,
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_BATCH_SIZE) boolean cacheBlocks,
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       if (LOG.isTraceEnabled()) {
151         LOG.trace("Query parameters  : Table Name = > " + this.table + " Start Row => " + startRow
152             + " End Row => " + endRow + " Columns => " + column + " Start Time => " + startTime
153             + " End Time => " + endTime + " Cache Blocks => " + cacheBlocks + " Max Versions => "
154             + maxVersions + " Batch Size => " + batchSize);
155       }
156       Table hTable = RESTServlet.getInstance().getTable(this.table);
157       tableScan.setBatch(batchSize);
158       tableScan.setMaxVersions(maxVersions);
159       tableScan.setTimeRange(startTime, endTime);
160       if (!startRow.isEmpty()) {
161         tableScan.setStartRow(Bytes.toBytes(startRow));
162       }
163       tableScan.setStopRow(Bytes.toBytes(endRow));
164       for (String csplit : column) {
165         String[] familysplit = csplit.trim().split(":");
166         if (familysplit.length == 2) {
167           if (familysplit[1].length() > 0) {
168             if (LOG.isTraceEnabled()) {
169               LOG.trace("Scan family and column : " + familysplit[0] + "  " + familysplit[1]);
170             }
171             tableScan.addColumn(Bytes.toBytes(familysplit[0]), Bytes.toBytes(familysplit[1]));
172           } else {
173             tableScan.addFamily(Bytes.toBytes(familysplit[0]));
174             if (LOG.isTraceEnabled()) {
175               LOG.trace("Scan family : " + familysplit[0] + " and empty qualifier.");
176             }
177             tableScan.addColumn(Bytes.toBytes(familysplit[0]), null);
178           }
179         } else if (StringUtils.isNotEmpty(familysplit[0])) {
180           if (LOG.isTraceEnabled()) {
181             LOG.trace("Scan family : " + familysplit[0]);
182           }
183           tableScan.addFamily(Bytes.toBytes(familysplit[0]));
184         }
185       }
186       FilterList filterList = null;
187       if (StringUtils.isNotEmpty(filters)) {
188           ParseFilter pf = new ParseFilter();
189           Filter filterParam = pf.parseFilterString(filters);
190           if (filter != null) {
191             filterList = new FilterList(filter, filterParam);
192           }
193           else {
194             filter = filterParam;
195           }
196       }
197       if (filterList != null) {
198         tableScan.setFilter(filterList);
199       } else if (filter != null) {
200         tableScan.setFilter(filter);
201       }
202       int fetchSize = this.servlet.getConfiguration().getInt(Constants.SCAN_FETCH_SIZE, 10);
203       tableScan.setCaching(fetchSize);
204      return new TableScanResource(hTable.getScanner(tableScan), userRequestedLimit);
205     } catch (Exception exp) {
206       servlet.getMetrics().incrementFailedScanRequests(1);
207       processException(exp);
208       LOG.warn(exp);
209       return null;
210     }
211   }
212 }