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       if (scanSpec.indexOf('*') > 0) {
141         String prefix = scanSpec.substring(0, scanSpec.indexOf('*'));
142         filter = new PrefixFilter(Bytes.toBytes(prefix));
143       }
144       LOG.debug("Query parameters  : Table Name = > " + this.table + " Start Row => " + startRow
145           + " End Row => " + endRow + " Columns => " + column + " Start Time => " + startTime
146           + " End Time => " + endTime + " Cache Blocks => " + cacheBlocks + " Max Versions => "
147           + maxVersions + " Batch Size => " + batchSize);
148       Table hTable = RESTServlet.getInstance().getTable(this.table);
149       Scan tableScan = new Scan();
150       tableScan.setBatch(batchSize);
151       tableScan.setMaxVersions(maxVersions);
152       tableScan.setTimeRange(startTime, endTime);
153       tableScan.setStartRow(Bytes.toBytes(startRow));
154       tableScan.setStopRow(Bytes.toBytes(endRow));
155       for (String csplit : column) {
156         String[] familysplit = csplit.trim().split(":");
157         if (familysplit.length == 2) {
158           if (familysplit[1].length() > 0) {
159             LOG.debug("Scan family and column : " + familysplit[0] + "  " + familysplit[1]);
160             tableScan.addColumn(Bytes.toBytes(familysplit[0]), Bytes.toBytes(familysplit[1]));
161           } else {
162             tableScan.addFamily(Bytes.toBytes(familysplit[0]));
163             LOG.debug("Scan family : " + familysplit[0] + " and empty qualifier.");
164             tableScan.addColumn(Bytes.toBytes(familysplit[0]), null);
165           }
166         } else if (StringUtils.isNotEmpty(familysplit[0])){
167           LOG.debug("Scan family : " + familysplit[0]);
168           tableScan.addFamily(Bytes.toBytes(familysplit[0]));
169         }
170       }
171       FilterList filterList = null;
172       if (StringUtils.isNotEmpty(filters)) {
173           ParseFilter pf = new ParseFilter();
174           Filter filterParam = pf.parseFilterString(filters);
175           if (filter != null) {
176             filterList = new FilterList(filter, filterParam);
177           }
178           else {
179             filter = filterParam;
180           }
181       }
182       if (filterList != null) {
183         tableScan.setFilter(filterList);
184       } else if (filter != null) {
185         tableScan.setFilter(filter);
186       }
187       int fetchSize = this.servlet.getConfiguration().getInt(Constants.SCAN_FETCH_SIZE, 10);
188       tableScan.setCaching(fetchSize);
189      return new TableScanResource(hTable.getScanner(tableScan), userRequestedLimit);
190     } catch (Exception exp) {
191       servlet.getMetrics().incrementFailedScanRequests(1);
192       processException(exp);
193       LOG.warn(exp);
194       return null;
195     }
196   }
197 }