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  package org.apache.hadoop.hbase.rest;
20  
21  import com.fasterxml.jackson.annotation.JsonIgnore;
22  import com.fasterxml.jackson.annotation.JsonProperty;
23  
24  import java.io.IOException;
25  import java.util.ArrayList;
26  import java.util.Iterator;
27  import java.util.List;
28  
29  import javax.ws.rs.DefaultValue;
30  import javax.ws.rs.GET;
31  import javax.ws.rs.HeaderParam;
32  import javax.ws.rs.PathParam;
33  import javax.ws.rs.Produces;
34  import javax.ws.rs.QueryParam;
35  import javax.ws.rs.core.Context;
36  import javax.ws.rs.core.Response;
37  import javax.ws.rs.core.Response.ResponseBuilder;
38  import javax.ws.rs.core.UriInfo;
39  import javax.xml.bind.annotation.XmlAccessType;
40  import javax.xml.bind.annotation.XmlAccessorType;
41  import javax.xml.bind.annotation.XmlElement;
42  import javax.xml.bind.annotation.XmlRootElement;
43  
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  import org.apache.hadoop.hbase.classification.InterfaceAudience;
47  import org.apache.hadoop.hbase.Cell;
48  import org.apache.hadoop.hbase.CellUtil;
49  import org.apache.hadoop.hbase.client.Result;
50  import org.apache.hadoop.hbase.client.ResultScanner;
51  import org.apache.hadoop.hbase.rest.model.CellModel;
52  import org.apache.hadoop.hbase.rest.model.RowModel;
53  
54  @InterfaceAudience.Private
55  public class TableScanResource  extends ResourceBase {
56  
57    private static final Log LOG = LogFactory.getLog(TableScanResource.class);
58    TableResource tableResource;
59    ResultScanner results;
60    int userRequestedLimit;
61  
62    public TableScanResource(ResultScanner scanner, int userRequestedLimit) throws IOException {
63      super();
64      this.results = scanner;
65      this.userRequestedLimit = userRequestedLimit;
66    }
67  
68    @GET
69    @Produces({ Constants.MIMETYPE_XML, Constants.MIMETYPE_JSON })
70    public CellSetModelStream get(final @Context UriInfo uriInfo) {
71      servlet.getMetrics().incrementRequests(1);
72      final int rowsToSend = userRequestedLimit;
73      servlet.getMetrics().incrementSucessfulScanRequests(1);
74      final Iterator<Result> itr = results.iterator();
75      return new CellSetModelStream(new ArrayList<RowModel>() {
76        public Iterator<RowModel> iterator() {
77          return new Iterator<RowModel>() {
78            int count = rowsToSend;
79  
80            @Override
81            public boolean hasNext() {
82              if (count > 0) {
83                return itr.hasNext();
84              } else {
85                return false;
86              }
87            }
88  
89            @Override
90            public void remove() {
91              throw new UnsupportedOperationException(
92                  "Remove method cannot be used in CellSetModelStream");
93            }
94  
95            @Override
96            public RowModel next() {
97              Result rs = itr.next();
98              if ((rs == null) || (count <= 0)) {
99                return null;
100             }
101             byte[] rowKey = rs.getRow();
102             RowModel rModel = new RowModel(rowKey);
103             List<Cell> kvs = rs.listCells();
104             for (Cell kv : kvs) {
105               rModel.addCell(new CellModel(CellUtil.cloneFamily(kv), CellUtil.cloneQualifier(kv),
106                   kv.getTimestamp(), CellUtil.cloneValue(kv)));
107             }
108             count--;
109             if (count == 0) {
110               results.close();
111             }
112             return rModel;
113           }
114         };
115       }
116     });
117   }
118 
119   @GET
120   @Produces({ Constants.MIMETYPE_PROTOBUF, Constants.MIMETYPE_PROTOBUF_IETF })
121   public Response getProtobuf(
122       final @Context UriInfo uriInfo,
123       final @PathParam("scanspec") String scanSpec,
124       final @HeaderParam("Accept") String contentType,
125       @DefaultValue(Integer.MAX_VALUE + "") @QueryParam(Constants.SCAN_LIMIT) int userRequestedLimit,
126       @DefaultValue("") @QueryParam(Constants.SCAN_START_ROW) String startRow,
127       @DefaultValue("") @QueryParam(Constants.SCAN_END_ROW) String endRow,
128       @DefaultValue("column") @QueryParam(Constants.SCAN_COLUMN) List<String> column,
129       @DefaultValue("1") @QueryParam(Constants.SCAN_MAX_VERSIONS) int maxVersions,
130       @DefaultValue("-1") @QueryParam(Constants.SCAN_BATCH_SIZE) int batchSize,
131       @DefaultValue("0") @QueryParam(Constants.SCAN_START_TIME) long startTime,
132       @DefaultValue(Long.MAX_VALUE + "") @QueryParam(Constants.SCAN_END_TIME) long endTime,
133       @DefaultValue("true") @QueryParam(Constants.SCAN_BATCH_SIZE) boolean cacheBlocks) {
134     servlet.getMetrics().incrementRequests(1);
135     try {
136       int fetchSize = this.servlet.getConfiguration().getInt(Constants.SCAN_FETCH_SIZE, 10);
137       ProtobufStreamingUtil stream = new ProtobufStreamingUtil(this.results, contentType,
138           userRequestedLimit, fetchSize);
139       servlet.getMetrics().incrementSucessfulScanRequests(1);
140       ResponseBuilder response = Response.ok(stream);
141       response.header("content-type", contentType);
142       return response.build();
143     } catch (Exception exp) {
144       servlet.getMetrics().incrementFailedScanRequests(1);
145       processException(exp);
146       LOG.warn(exp);
147       return null;
148     }
149   }
150 
151   @XmlRootElement(name = "CellSet")
152   @XmlAccessorType(XmlAccessType.FIELD)
153   public static class CellSetModelStream {
154     // JAXB needs an arraylist for streaming
155     @XmlElement(name = "Row")
156     @JsonIgnore
157     private ArrayList<RowModel> Row;
158 
159     public CellSetModelStream() {
160     }
161 
162     public CellSetModelStream(final ArrayList<RowModel> rowList) {
163       this.Row = rowList;
164     }
165 
166     // jackson needs an iterator for streaming
167     @JsonProperty("Row")
168     public Iterator<RowModel> getIterator() {
169       return Row.iterator();
170     }
171   }
172 }