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 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    @GET
64    @Produces({ MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF, 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          ResultGenerator generator =
78            ResultGenerator.fromRowSpec(this.tableResource.getName(), rowSpec, null,
79              !params.containsKey(NOCACHE_PARAM_NAME));
80          Cell value = null;
81          RowModel rowModel = new RowModel(rk);
82          if (generator.hasNext()) {
83            while ((value = generator.next()) != null) {
84              rowModel.addCell(new CellModel(CellUtil.cloneFamily(value), CellUtil
85                  .cloneQualifier(value), value.getTimestamp(), CellUtil.cloneValue(value)));
86            }
87            model.addRow(rowModel);
88          } else {
89            LOG.trace("The row : " + rk + " not found in the table.");
90          }
91        }
92  
93        if (model.getRows().size() == 0) {
94        //If no rows found.
95          servlet.getMetrics().incrementFailedGetRequests(1);
96          return Response.status(Response.Status.NOT_FOUND)
97              .type(MIMETYPE_TEXT).entity("No rows found." + CRLF)
98              .build();
99        } else {
100         servlet.getMetrics().incrementSucessfulGetRequests(1);
101         return Response.ok(model).build();
102       }
103     } catch (Exception e) {
104       servlet.getMetrics().incrementFailedGetRequests(1);
105       return processException(e);
106     }
107   }
108 }