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