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.hbase.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 implements Constants {
41    private static final Log LOG = LogFactory.getLog(MultiRowResource.class);
42  
43    TableResource tableResource;
44    Integer versions = null;
45  
46    /**
47     * Constructor
48     *
49     * @param tableResource
50     * @param versions
51     * @throws java.io.IOException
52     */
53    public MultiRowResource(TableResource tableResource, String versions) throws IOException {
54      super();
55      this.tableResource = tableResource;
56  
57      if (versions != null) {
58        this.versions = Integer.valueOf(versions);
59  
60      }
61    }
62  
63    @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="REC_CATCH_EXCEPTION")
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          ResultGenerator generator =
79            ResultGenerator.fromRowSpec(this.tableResource.getName(), rowSpec, null,
80              !params.containsKey(NOCACHE_PARAM_NAME));
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            if (LOG.isTraceEnabled()) {
91              LOG.trace("The row : " + rk + " not found in the table.");
92            }
93          }
94        }
95  
96        if (model.getRows().size() == 0) {
97        //If no rows found.
98          servlet.getMetrics().incrementFailedGetRequests(1);
99          return Response.status(Response.Status.NOT_FOUND)
100             .type(MIMETYPE_TEXT).entity("No rows found." + CRLF)
101             .build();
102       } else {
103         servlet.getMetrics().incrementSucessfulGetRequests(1);
104         return Response.ok(model).build();
105       }
106     } catch (Exception e) {
107       servlet.getMetrics().incrementFailedGetRequests(1);
108       return processException(e);
109     }
110   }
111 }