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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 package org
.apache
.hadoop
.hbase
.rest
;
21 import java
.io
.IOException
;
22 import java
.util
.ArrayList
;
23 import java
.util
.Iterator
;
24 import java
.util
.List
;
26 import javax
.ws
.rs
.GET
;
27 import javax
.ws
.rs
.HeaderParam
;
28 import javax
.ws
.rs
.Produces
;
29 import javax
.ws
.rs
.core
.Context
;
30 import javax
.ws
.rs
.core
.Response
;
31 import javax
.ws
.rs
.core
.Response
.ResponseBuilder
;
32 import javax
.ws
.rs
.core
.StreamingOutput
;
33 import javax
.ws
.rs
.core
.UriInfo
;
34 import javax
.xml
.bind
.annotation
.XmlAccessType
;
35 import javax
.xml
.bind
.annotation
.XmlAccessorType
;
36 import javax
.xml
.bind
.annotation
.XmlElement
;
37 import javax
.xml
.bind
.annotation
.XmlRootElement
;
39 import org
.apache
.hadoop
.hbase
.Cell
;
40 import org
.apache
.hadoop
.hbase
.CellUtil
;
41 import org
.apache
.hadoop
.hbase
.client
.Result
;
42 import org
.apache
.hadoop
.hbase
.client
.ResultScanner
;
43 import org
.apache
.hadoop
.hbase
.rest
.model
.CellModel
;
44 import org
.apache
.hadoop
.hbase
.rest
.model
.RowModel
;
45 import org
.apache
.yetus
.audience
.InterfaceAudience
;
46 import org
.slf4j
.Logger
;
47 import org
.slf4j
.LoggerFactory
;
49 import com
.fasterxml
.jackson
.annotation
.JsonIgnore
;
50 import com
.fasterxml
.jackson
.annotation
.JsonProperty
;
52 @InterfaceAudience.Private
53 public class TableScanResource
extends ResourceBase
{
54 private static final Logger LOG
= LoggerFactory
.getLogger(TableScanResource
.class);
56 TableResource tableResource
;
57 ResultScanner results
;
58 int userRequestedLimit
;
60 public TableScanResource(ResultScanner scanner
, int userRequestedLimit
) throws IOException
{
62 this.results
= scanner
;
63 this.userRequestedLimit
= userRequestedLimit
;
67 @Produces({ Constants
.MIMETYPE_XML
, Constants
.MIMETYPE_JSON
})
68 public CellSetModelStream
get(final @Context UriInfo uriInfo
) {
69 if (LOG
.isTraceEnabled()) {
70 LOG
.trace("GET " + uriInfo
.getAbsolutePath());
72 servlet
.getMetrics().incrementRequests(1);
73 final int rowsToSend
= userRequestedLimit
;
74 servlet
.getMetrics().incrementSucessfulScanRequests(1);
75 final Iterator
<Result
> itr
= results
.iterator();
76 return new CellSetModelStream(new ArrayList
<RowModel
>() {
78 public Iterator
<RowModel
> iterator() {
79 return new Iterator
<RowModel
>() {
80 int count
= rowsToSend
;
83 public boolean hasNext() {
84 return count
> 0 && itr
.hasNext();
88 public RowModel
next() {
89 Result rs
= itr
.next();
90 if ((rs
== null) || (count
<= 0)) {
93 byte[] rowKey
= rs
.getRow();
94 RowModel rModel
= new RowModel(rowKey
);
95 List
<Cell
> kvs
= rs
.listCells();
97 rModel
.addCell(new CellModel(CellUtil
.cloneFamily(kv
), CellUtil
.cloneQualifier(kv
),
98 kv
.getTimestamp(), CellUtil
.cloneValue(kv
)));
112 @Produces({ Constants
.MIMETYPE_PROTOBUF
, Constants
.MIMETYPE_PROTOBUF_IETF
})
113 public Response
getProtobuf(
114 final @Context UriInfo uriInfo
,
115 final @HeaderParam("Accept") String contentType
) {
116 if (LOG
.isTraceEnabled()) {
117 LOG
.trace("GET " + uriInfo
.getAbsolutePath() + " as " +
120 servlet
.getMetrics().incrementRequests(1);
122 int fetchSize
= this.servlet
.getConfiguration().getInt(Constants
.SCAN_FETCH_SIZE
, 10);
123 StreamingOutput stream
= new ProtobufStreamingOutput(this.results
, contentType
,
124 userRequestedLimit
, fetchSize
);
125 servlet
.getMetrics().incrementSucessfulScanRequests(1);
126 ResponseBuilder response
= Response
.ok(stream
);
127 response
.header("content-type", contentType
);
128 return response
.build();
129 } catch (Exception exp
) {
130 servlet
.getMetrics().incrementFailedScanRequests(1);
131 processException(exp
);
132 LOG
.warn(exp
.toString(), exp
);
137 @XmlRootElement(name
= "CellSet")
138 @XmlAccessorType(XmlAccessType
.FIELD
)
139 public static class CellSetModelStream
{
140 // JAXB needs an arraylist for streaming
141 @XmlElement(name
= "Row")
143 private ArrayList
<RowModel
> Row
;
145 public CellSetModelStream() {
148 public CellSetModelStream(final ArrayList
<RowModel
> rowList
) {
152 // jackson needs an iterator for streaming
154 public Iterator
<RowModel
> getIterator() {
155 return Row
.iterator();