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