001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.rest;
019
020import java.io.IOException;
021import org.apache.hadoop.hbase.Cell;
022import org.apache.hadoop.hbase.CellUtil;
023import org.apache.hadoop.hbase.rest.model.CellModel;
024import org.apache.hadoop.hbase.rest.model.CellSetModel;
025import org.apache.hadoop.hbase.rest.model.RowModel;
026import org.apache.hadoop.hbase.util.Bytes;
027import org.apache.yetus.audience.InterfaceAudience;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031import org.apache.hbase.thirdparty.javax.ws.rs.GET;
032import org.apache.hbase.thirdparty.javax.ws.rs.HeaderParam;
033import org.apache.hbase.thirdparty.javax.ws.rs.Produces;
034import org.apache.hbase.thirdparty.javax.ws.rs.core.Context;
035import org.apache.hbase.thirdparty.javax.ws.rs.core.MultivaluedMap;
036import org.apache.hbase.thirdparty.javax.ws.rs.core.Response;
037import org.apache.hbase.thirdparty.javax.ws.rs.core.UriInfo;
038
039@InterfaceAudience.Private
040public class MultiRowResource extends ResourceBase implements Constants {
041  private static final Logger LOG = LoggerFactory.getLogger(MultiRowResource.class);
042
043  TableResource tableResource;
044  Integer versions = null;
045  String[] columns = null;
046
047  /**
048   * Constructor
049   */
050  public MultiRowResource(TableResource tableResource, String versions, String columnsStr)
051    throws IOException {
052    super();
053    this.tableResource = tableResource;
054
055    if (columnsStr != null && !columnsStr.equals("")) {
056      this.columns = columnsStr.split(",");
057    }
058
059    if (versions != null) {
060      this.versions = Integer.valueOf(versions);
061
062    }
063  }
064
065  @GET
066  @Produces({ MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF, MIMETYPE_PROTOBUF_IETF })
067  public Response get(final @Context UriInfo uriInfo,
068    final @HeaderParam("Encoding") String keyEncodingHeader) {
069    MultivaluedMap<String, String> params = uriInfo.getQueryParameters();
070    String keyEncoding = (keyEncodingHeader != null)
071      ? keyEncodingHeader
072      : params.getFirst(KEY_ENCODING_QUERY_PARAM_NAME);
073
074    servlet.getMetrics().incrementRequests(1);
075    try {
076      CellSetModel model = new CellSetModel();
077      for (String rk : params.get(ROW_KEYS_PARAM_NAME)) {
078        RowSpec rowSpec = new RowSpec(rk, keyEncoding);
079
080        if (this.versions != null) {
081          rowSpec.setMaxVersions(this.versions);
082        }
083
084        if (this.columns != null) {
085          for (int i = 0; i < this.columns.length; i++) {
086            rowSpec.addColumn(Bytes.toBytes(this.columns[i]));
087          }
088        }
089
090        ResultGenerator generator = ResultGenerator.fromRowSpec(this.tableResource.getName(),
091          rowSpec, null, !params.containsKey(NOCACHE_PARAM_NAME));
092        Cell value = null;
093        RowModel rowModel = new RowModel(rowSpec.getRow());
094        if (generator.hasNext()) {
095          while ((value = generator.next()) != null) {
096            rowModel.addCell(new CellModel(CellUtil.cloneFamily(value),
097              CellUtil.cloneQualifier(value), value.getTimestamp(), CellUtil.cloneValue(value)));
098          }
099          model.addRow(rowModel);
100        } else {
101          if (LOG.isTraceEnabled()) {
102            LOG.trace("The row : " + rk + " not found in the table.");
103          }
104        }
105      }
106
107      if (model.getRows().isEmpty()) {
108        // If no rows found.
109        servlet.getMetrics().incrementFailedGetRequests(1);
110        return Response.status(Response.Status.NOT_FOUND).type(MIMETYPE_TEXT)
111          .entity("No rows found." + CRLF).build();
112      } else {
113        servlet.getMetrics().incrementSucessfulGetRequests(1);
114        return Response.ok(model).build();
115      }
116    } catch (IOException e) {
117      servlet.getMetrics().incrementFailedGetRequests(1);
118      return processException(e);
119    }
120  }
121}