View Javadoc

1   /**
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.rest;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.hbase.KeyValue;
25  import org.apache.hadoop.hbase.rest.ResourceBase;
26  import org.apache.hadoop.hbase.rest.RowSpec;
27  import org.apache.hadoop.hbase.rest.TableResource;
28  import org.apache.hadoop.hbase.rest.model.CellModel;
29  import org.apache.hadoop.hbase.rest.model.CellSetModel;
30  import org.apache.hadoop.hbase.rest.model.RowModel;
31  import org.apache.hadoop.hbase.rest.transform.Transform;
32  
33  import javax.ws.rs.GET;
34  import javax.ws.rs.Produces;
35  import javax.ws.rs.WebApplicationException;
36  import javax.ws.rs.core.Context;
37  import javax.ws.rs.core.MultivaluedMap;
38  import javax.ws.rs.core.Response;
39  import javax.ws.rs.core.UriInfo;
40  import java.io.IOException;
41  import java.util.ArrayList;
42  
43  public class MultiRowResource extends ResourceBase {
44    private static final Log LOG = LogFactory.getLog(MultiRowResource.class);
45    public static final String ROW_KEYS_PARAM_NAME = "row";
46  
47    TableResource tableResource;
48    Integer versions = null;
49  
50    /**
51     * Constructor
52     *
53     * @param tableResource
54     * @param versions
55     * @throws java.io.IOException
56     */
57    public MultiRowResource(TableResource tableResource, String versions) throws IOException {
58      super();
59      this.tableResource = tableResource;
60  
61      if (versions != null) {
62        this.versions = Integer.valueOf(versions);
63  
64      }
65    }
66  
67    @GET
68    @Produces({MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF})
69    public Response get(final @Context UriInfo uriInfo) {
70      MultivaluedMap<String, String> params = uriInfo.getQueryParameters();
71  
72      servlet.getMetrics().incrementRequests(1);
73      try {
74        CellSetModel model = new CellSetModel();
75        for (String rk : params.get(ROW_KEYS_PARAM_NAME)) {
76          RowSpec rowSpec = new RowSpec(rk);
77  
78          if (this.versions != null) {
79            rowSpec.setMaxVersions(this.versions);
80          }
81  
82          ResultGenerator generator = ResultGenerator.fromRowSpec(this.tableResource.getName(), rowSpec, null);
83          if (!generator.hasNext()) {
84            throw new WebApplicationException(Response.Status.NOT_FOUND);
85          }
86  
87          KeyValue value = null;
88          RowModel rowModel = new RowModel(rk);
89  
90  
91          while ((value = generator.next()) != null) {
92            byte[] family = value.getFamily();
93            byte[] qualifier = value.getQualifier();
94            byte[] data = tableResource.transform(family, qualifier, value.getValue(), Transform.Direction.OUT);
95            rowModel.addCell(new CellModel(family, qualifier, value.getTimestamp(), data));
96          }
97  
98          model.addRow(rowModel);
99        }
100       return Response.ok(model).build();
101     } catch (IOException e) {
102       throw new WebApplicationException(e,
103               Response.Status.SERVICE_UNAVAILABLE);
104     }
105 
106   }
107 }