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.classification.InterfaceAudience;
38  import org.apache.hadoop.hbase.client.HTableInterface;
39  import org.apache.hadoop.hbase.client.Scan;
40  import org.apache.hadoop.hbase.filter.Filter;
41  import org.apache.hadoop.hbase.filter.FilterList;
42  import org.apache.hadoop.hbase.filter.ParseFilter;
43  import org.apache.hadoop.hbase.filter.PrefixFilter;
44  import org.apache.hadoop.hbase.util.Bytes;
45  
46  @InterfaceAudience.Private
47  public class TableResource extends ResourceBase {
48  
49    String table;
50    private static final Log LOG = LogFactory.getLog(TableResource.class);
51  
52    /**
53     * Constructor
54     * @param table
55     * @throws IOException
56     */
57    public TableResource(String table) throws IOException {
58      super();
59      this.table = table;
60    }
61  
62    /** @return the table name */
63    String getName() {
64      return table;
65    }
66  
67    /**
68     * @return true if the table exists
69     * @throws IOException
70     */
71    boolean exists() throws IOException {
72      return servlet.getAdmin().tableExists(table);
73    }
74  
75    @Path("exists")
76    public ExistsResource getExistsResource() throws IOException {
77      return new ExistsResource(this);
78    }
79  
80    @Path("regions")
81    public RegionsResource getRegionsResource() throws IOException {
82      return new RegionsResource(this);
83    }
84  
85    @Path("scanner")
86    public ScannerResource getScannerResource() throws IOException {
87      return new ScannerResource(this);
88    }
89  
90    @Path("schema")
91    public SchemaResource getSchemaResource() throws IOException {
92      return new SchemaResource(this);
93    }
94  
95    @Path("multiget")
96    public MultiRowResource getMultipleRowResource(
97            final @QueryParam("v") String versions) throws IOException {
98      return new MultiRowResource(this, versions);
99    }
100 
101   @Path("{rowspec: [^*]+}")
102   public RowResource getRowResource(
103       // We need the @Encoded decorator so Jersey won't urldecode before
104       // the RowSpec constructor has a chance to parse
105       final @PathParam("rowspec") @Encoded String rowspec,
106       final @QueryParam("v") String versions,
107       final @QueryParam("check") String check) throws IOException {
108     return new RowResource(this, rowspec, versions, check);
109   }
110 
111   @Path("{suffixglobbingspec: .*\\*/.+}")
112   public RowResource getRowResourceWithSuffixGlobbing(
113       // We need the @Encoded decorator so Jersey won't urldecode before
114       // the RowSpec constructor has a chance to parse
115       final @PathParam("suffixglobbingspec") @Encoded String suffixglobbingspec,
116       final @QueryParam("v") String versions,
117       final @QueryParam("check") String check) throws IOException {
118     return new RowResource(this, suffixglobbingspec, versions, check);
119   }
120 
121   @Path("{scanspec: .*[*]$}")
122   public TableScanResource  getScanResource(
123       final @Context UriInfo uriInfo,
124       final @PathParam("scanspec") String scanSpec,
125       final @HeaderParam("Accept") String contentType,
126       @DefaultValue(Integer.MAX_VALUE + "")
127       @QueryParam(Constants.SCAN_LIMIT) int userRequestedLimit,
128       @DefaultValue("") @QueryParam(Constants.SCAN_START_ROW) String startRow,
129       @DefaultValue("") @QueryParam(Constants.SCAN_END_ROW) String endRow,
130       @DefaultValue("") @QueryParam(Constants.SCAN_COLUMN) List<String> column,
131       @DefaultValue("1") @QueryParam(Constants.SCAN_MAX_VERSIONS) int maxVersions,
132       @DefaultValue("-1") @QueryParam(Constants.SCAN_BATCH_SIZE) int batchSize,
133       @DefaultValue("0") @QueryParam(Constants.SCAN_START_TIME) long startTime,
134       @DefaultValue(Long.MAX_VALUE + "") @QueryParam(Constants.SCAN_END_TIME) long endTime,
135       @DefaultValue("true") @QueryParam(Constants.SCAN_BATCH_SIZE) boolean cacheBlocks, 
136       @DefaultValue("") @QueryParam(Constants.SCAN_FILTER) String filters) {
137     try {
138       Filter filter = null;
139       if (scanSpec.indexOf('*') > 0) {
140         String prefix = scanSpec.substring(0, scanSpec.indexOf('*'));
141         filter = new PrefixFilter(Bytes.toBytes(prefix));
142       }
143       LOG.debug("Query parameters  : Table Name = > " + this.table + " Start Row => " + startRow
144           + " End Row => " + endRow + " Columns => " + column + " Start Time => " + startTime
145           + " End Time => " + endTime + " Cache Blocks => " + cacheBlocks + " Max Versions => "
146           + maxVersions + " Batch Size => " + batchSize);
147       HTableInterface hTable = RESTServlet.getInstance().getTable(this.table);
148       Scan tableScan = new Scan();
149       tableScan.setBatch(batchSize);
150       tableScan.setMaxVersions(maxVersions);
151       tableScan.setTimeRange(startTime, endTime);
152       tableScan.setStartRow(Bytes.toBytes(startRow));
153       tableScan.setStopRow(Bytes.toBytes(endRow));
154       for (String csplit : column) {
155         String[] familysplit = csplit.trim().split(":");
156         if (familysplit.length == 2) {
157           if (familysplit[1].length() > 0) {
158             LOG.debug("Scan family and column : " + familysplit[0] + "  " + familysplit[1]);
159             tableScan.addColumn(Bytes.toBytes(familysplit[0]), Bytes.toBytes(familysplit[1]));
160           } else {
161             tableScan.addFamily(Bytes.toBytes(familysplit[0]));
162             LOG.debug("Scan family : " + familysplit[0] + " and empty qualifier.");
163             tableScan.addColumn(Bytes.toBytes(familysplit[0]), null);
164           }
165         } else if (StringUtils.isNotEmpty(familysplit[0])){
166           LOG.debug("Scan family : " + familysplit[0]);
167           tableScan.addFamily(Bytes.toBytes(familysplit[0]));
168         }
169       }
170       FilterList filterList = null;
171       if (StringUtils.isNotEmpty(filters)) {
172           ParseFilter pf = new ParseFilter();
173           Filter filterParam = pf.parseFilterString(filters);
174           if (filter != null) {
175             filterList = new FilterList(filter, filterParam);
176           }
177           else {
178             filter = filterParam;
179           }
180       }
181       if (filterList != null) {
182         tableScan.setFilter(filterList);
183       } else if (filter != null) {
184         tableScan.setFilter(filter);
185       }
186       int fetchSize = this.servlet.getConfiguration().getInt(Constants.SCAN_FETCH_SIZE, 10);
187       tableScan.setCaching(fetchSize);
188      return new TableScanResource(hTable.getScanner(tableScan), userRequestedLimit);
189     } catch (Exception exp) {
190       servlet.getMetrics().incrementFailedScanRequests(1);
191       processException(exp);
192       LOG.warn(exp);
193       return null;
194     }
195   }
196 }