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