View Javadoc

1   /**
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.rest;
21  
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  public class MultiRowResource extends ResourceBase {
39    public static final String ROW_KEYS_PARAM_NAME = "row";
40  
41    TableResource tableResource;
42    Integer versions = null;
43  
44    /**
45     * Constructor
46     *
47     * @param tableResource
48     * @param versions
49     * @throws java.io.IOException
50     */
51    public MultiRowResource(TableResource tableResource, String versions) throws IOException {
52      super();
53      this.tableResource = tableResource;
54  
55      if (versions != null) {
56        this.versions = Integer.valueOf(versions);
57  
58      }
59    }
60  
61    @GET
62    @Produces({MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
63      MIMETYPE_PROTOBUF_IETF})
64    public Response get(final @Context UriInfo uriInfo) {
65      MultivaluedMap<String, String> params = uriInfo.getQueryParameters();
66  
67      servlet.getMetrics().incrementRequests(1);
68      try {
69        CellSetModel model = new CellSetModel();
70        for (String rk : params.get(ROW_KEYS_PARAM_NAME)) {
71          RowSpec rowSpec = new RowSpec(rk);
72  
73          if (this.versions != null) {
74            rowSpec.setMaxVersions(this.versions);
75          }
76  
77          ResultGenerator generator =
78            ResultGenerator.fromRowSpec(this.tableResource.getName(), rowSpec, null);
79          if (!generator.hasNext()) {
80            return Response.status(Response.Status.NOT_FOUND)
81              .type(MIMETYPE_TEXT).entity("Not found" + CRLF)
82              .build();
83          }
84  
85          KeyValue value = null;
86          RowModel rowModel = new RowModel(rk);
87  
88          while ((value = generator.next()) != null) {
89            rowModel.addCell(new CellModel(value.getFamily(), value.getQualifier(),
90              value.getTimestamp(), value.getValue()));
91          }
92  
93          model.addRow(rowModel);
94        }
95        servlet.getMetrics().incrementSucessfulGetRequests(1);
96        return Response.ok(model).build();
97      } catch (IOException e) {
98        servlet.getMetrics().incrementFailedGetRequests(1);
99        return Response.status(Response.Status.SERVICE_UNAVAILABLE)
100         .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
101         .build();
102     }
103 
104   }
105 }