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    String[] columns = 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, String columnsStr)
55        throws IOException {
56      super();
57      this.tableResource = tableResource;
58  
59      if (columnsStr != null && !columnsStr.equals("")) {
60        this.columns = columnsStr.split(",");
61      }
62  
63      if (versions != null) {
64        this.versions = Integer.valueOf(versions);
65  
66      }
67    }
68  
69    @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="REC_CATCH_EXCEPTION")
70    @GET
71    @Produces({ MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF, MIMETYPE_PROTOBUF_IETF })
72    public Response get(final @Context UriInfo uriInfo) {
73      MultivaluedMap<String, String> params = uriInfo.getQueryParameters();
74  
75      servlet.getMetrics().incrementRequests(1);
76      try {
77        CellSetModel model = new CellSetModel();
78        for (String rk : params.get(ROW_KEYS_PARAM_NAME)) {
79          RowSpec rowSpec = new RowSpec(rk);
80  
81          if (this.versions != null) {
82            rowSpec.setMaxVersions(this.versions);
83          }
84  
85          if (this.columns != null) {
86            for (int i = 0; i < this.columns.length; i++) {
87              rowSpec.addColumn(this.columns[i].getBytes());
88            }
89          }
90  
91          ResultGenerator generator =
92            ResultGenerator.fromRowSpec(this.tableResource.getName(), rowSpec, null,
93              !params.containsKey(NOCACHE_PARAM_NAME));
94          Cell value = null;
95          RowModel rowModel = new RowModel(rowSpec.getRow());
96          if (generator.hasNext()) {
97            while ((value = generator.next()) != null) {
98              rowModel.addCell(new CellModel(CellUtil.cloneFamily(value), CellUtil
99                  .cloneQualifier(value), value.getTimestamp(), CellUtil.cloneValue(value)));
100           }
101           model.addRow(rowModel);
102         } else {
103           if (LOG.isTraceEnabled()) {
104             LOG.trace("The row : " + rk + " not found in the table.");
105           }
106         }
107       }
108 
109       if (model.getRows().size() == 0) {
110       //If no rows found.
111         servlet.getMetrics().incrementFailedGetRequests(1);
112         return Response.status(Response.Status.NOT_FOUND)
113             .type(MIMETYPE_TEXT).entity("No rows found." + CRLF)
114             .build();
115       } else {
116         servlet.getMetrics().incrementSucessfulGetRequests(1);
117         return Response.ok(model).build();
118       }
119     } catch (Exception e) {
120       servlet.getMetrics().incrementFailedGetRequests(1);
121       return processException(e);
122     }
123   }
124 }