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.
20 package org
.apache
.hadoop
.hbase
.rest
;
22 import com
.fasterxml
.jackson
.core
.JsonParseException
;
23 import com
.fasterxml
.jackson
.databind
.JsonMappingException
;
24 import java
.io
.IOException
;
26 import java
.util
.Collections
;
27 import java
.util
.HashMap
;
29 import org
.apache
.hadoop
.hbase
.TableNotFoundException
;
30 import org
.apache
.hadoop
.hbase
.filter
.Filter
;
31 import org
.apache
.hadoop
.hbase
.rest
.model
.ScannerModel
;
32 import org
.apache
.yetus
.audience
.InterfaceAudience
;
33 import org
.slf4j
.Logger
;
34 import org
.slf4j
.LoggerFactory
;
36 import org
.apache
.hbase
.thirdparty
.javax
.ws
.rs
.Consumes
;
37 import org
.apache
.hbase
.thirdparty
.javax
.ws
.rs
.POST
;
38 import org
.apache
.hbase
.thirdparty
.javax
.ws
.rs
.PUT
;
39 import org
.apache
.hbase
.thirdparty
.javax
.ws
.rs
.Path
;
40 import org
.apache
.hbase
.thirdparty
.javax
.ws
.rs
.PathParam
;
41 import org
.apache
.hbase
.thirdparty
.javax
.ws
.rs
.core
.Context
;
42 import org
.apache
.hbase
.thirdparty
.javax
.ws
.rs
.core
.Response
;
43 import org
.apache
.hbase
.thirdparty
.javax
.ws
.rs
.core
.UriBuilder
;
44 import org
.apache
.hbase
.thirdparty
.javax
.ws
.rs
.core
.UriInfo
;
46 @InterfaceAudience.Private
47 public class ScannerResource
extends ResourceBase
{
49 private static final Logger LOG
= LoggerFactory
.getLogger(ScannerResource
.class);
51 static final Map
<String
,ScannerInstanceResource
> scanners
=
52 Collections
.synchronizedMap(new HashMap
<String
,ScannerInstanceResource
>());
54 TableResource tableResource
;
58 * @param tableResource
61 public ScannerResource(TableResource tableResource
)throws IOException
{
63 this.tableResource
= tableResource
;
66 static boolean delete(final String id
) {
67 ScannerInstanceResource instance
= scanners
.remove(id
);
68 if (instance
!= null) {
69 instance
.generator
.close();
76 Response
update(final ScannerModel model
, final boolean replace
,
77 final UriInfo uriInfo
) {
78 servlet
.getMetrics().incrementRequests(1);
79 if (servlet
.isReadOnly()) {
80 return Response
.status(Response
.Status
.FORBIDDEN
)
81 .type(MIMETYPE_TEXT
).entity("Forbidden" + CRLF
)
84 byte[] endRow
= model
.hasEndRow() ? model
.getEndRow() : null;
86 if (model
.getLabels() != null) {
87 spec
= new RowSpec(model
.getStartRow(), endRow
, model
.getColumns(), model
.getStartTime(),
88 model
.getEndTime(), model
.getMaxVersions(), model
.getLabels());
90 spec
= new RowSpec(model
.getStartRow(), endRow
, model
.getColumns(), model
.getStartTime(),
91 model
.getEndTime(), model
.getMaxVersions());
95 Filter filter
= ScannerResultGenerator
.buildFilterFromModel(model
);
96 String tableName
= tableResource
.getName();
97 ScannerResultGenerator gen
=
98 new ScannerResultGenerator(tableName
, spec
, filter
, model
.getCaching(),
99 model
.getCacheBlocks(), model
.getLimit());
100 String id
= gen
.getID();
101 ScannerInstanceResource instance
=
102 new ScannerInstanceResource(tableName
, id
, gen
, model
.getBatch());
103 scanners
.put(id
, instance
);
104 if (LOG
.isTraceEnabled()) {
105 LOG
.trace("new scanner: " + id
);
107 UriBuilder builder
= uriInfo
.getAbsolutePathBuilder();
108 URI uri
= builder
.path(id
).build();
109 servlet
.getMetrics().incrementSucessfulPutRequests(1);
110 return Response
.created(uri
).build();
111 } catch (Exception e
) {
112 LOG
.error("Exception occurred while processing " + uriInfo
.getAbsolutePath() + " : ", e
);
113 servlet
.getMetrics().incrementFailedPutRequests(1);
114 if (e
instanceof TableNotFoundException
) {
115 return Response
.status(Response
.Status
.NOT_FOUND
)
116 .type(MIMETYPE_TEXT
).entity("Not found" + CRLF
)
118 } else if (e
instanceof RuntimeException
119 || e
instanceof JsonMappingException
| e
instanceof JsonParseException
) {
120 return Response
.status(Response
.Status
.BAD_REQUEST
)
121 .type(MIMETYPE_TEXT
).entity("Bad request" + CRLF
)
124 return Response
.status(Response
.Status
.SERVICE_UNAVAILABLE
)
125 .type(MIMETYPE_TEXT
).entity("Unavailable" + CRLF
)
131 @Consumes({MIMETYPE_XML
, MIMETYPE_JSON
, MIMETYPE_PROTOBUF
,
132 MIMETYPE_PROTOBUF_IETF
})
133 public Response
put(final ScannerModel model
,
134 final @Context UriInfo uriInfo
) {
135 if (LOG
.isTraceEnabled()) {
136 LOG
.trace("PUT " + uriInfo
.getAbsolutePath());
138 return update(model
, true, uriInfo
);
142 @Consumes({MIMETYPE_XML
, MIMETYPE_JSON
, MIMETYPE_PROTOBUF
,
143 MIMETYPE_PROTOBUF_IETF
})
144 public Response
post(final ScannerModel model
,
145 final @Context UriInfo uriInfo
) {
146 if (LOG
.isTraceEnabled()) {
147 LOG
.trace("POST " + uriInfo
.getAbsolutePath());
149 return update(model
, false, uriInfo
);
152 @Path("{scanner: .+}")
153 public ScannerInstanceResource
getScannerInstanceResource(
154 final @PathParam("scanner") String id
) throws IOException
{
155 ScannerInstanceResource instance
= scanners
.get(id
);
156 if (instance
== null) {
157 servlet
.getMetrics().incrementFailedGetRequests(1);
158 return new ScannerInstanceResource();
160 servlet
.getMetrics().incrementSucessfulGetRequests(1);