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 org.apache.hadoop.classification.InterfaceAudience;
22  import org.apache.hadoop.hbase.KeyValue;
23  import org.apache.hadoop.hbase.rest.ResourceBase;
24  import org.apache.hadoop.hbase.rest.RowSpec;
25  import org.apache.hadoop.hbase.rest.TableResource;
26  import org.apache.hadoop.hbase.rest.model.CellModel;
27  import org.apache.hadoop.hbase.rest.model.CellSetModel;
28  import org.apache.hadoop.hbase.rest.model.RowModel;
29  
30  import javax.ws.rs.GET;
31  import javax.ws.rs.Produces;
32  import javax.ws.rs.core.Context;
33  import javax.ws.rs.core.MultivaluedMap;
34  import javax.ws.rs.core.Response;
35  import javax.ws.rs.core.UriInfo;
36  import java.io.IOException;
37  
38  @InterfaceAudience.Private
39  public class MultiRowResource extends ResourceBase {
40    public static final String ROW_KEYS_PARAM_NAME = "row";
41  
42    TableResource tableResource;
43    Integer versions = null;
44  
45    /**
46     * Constructor
47     *
48     * @param tableResource
49     * @param versions
50     * @throws java.io.IOException
51     */
52    public MultiRowResource(TableResource tableResource, String versions) throws IOException {
53      super();
54      this.tableResource = tableResource;
55  
56      if (versions != null) {
57        this.versions = Integer.valueOf(versions);
58  
59      }
60    }
61  
62    @GET
63    @Produces({MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
64      MIMETYPE_PROTOBUF_IETF})
65    public Response get(final @Context UriInfo uriInfo) {
66      MultivaluedMap<String, String> params = uriInfo.getQueryParameters();
67  
68      servlet.getMetrics().incrementRequests(1);
69      try {
70        CellSetModel model = new CellSetModel();
71        for (String rk : params.get(ROW_KEYS_PARAM_NAME)) {
72          RowSpec rowSpec = new RowSpec(rk);
73  
74          if (this.versions != null) {
75            rowSpec.setMaxVersions(this.versions);
76          }
77  
78          ResultGenerator generator =
79            ResultGenerator.fromRowSpec(this.tableResource.getName(), rowSpec, null);
80          if (!generator.hasNext()) {
81            return Response.status(Response.Status.NOT_FOUND)
82              .type(MIMETYPE_TEXT).entity("Not found" + CRLF)
83              .build();
84          }
85  
86          KeyValue value = null;
87          RowModel rowModel = new RowModel(rk);
88  
89          while ((value = generator.next()) != null) {
90            rowModel.addCell(new CellModel(value.getFamily(), value.getQualifier(),
91              value.getTimestamp(), value.getValue()));
92          }
93  
94          model.addRow(rowModel);
95        }
96        servlet.getMetrics().incrementSucessfulGetRequests(1);
97        return Response.ok(model).build();
98      } catch (IOException e) {
99        servlet.getMetrics().incrementFailedGetRequests(1);
100       return Response.status(Response.Status.SERVICE_UNAVAILABLE)
101         .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
102         .build();
103     }
104 
105   }
106 }