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.Produces; 033import org.apache.hbase.thirdparty.javax.ws.rs.core.Context; 034import org.apache.hbase.thirdparty.javax.ws.rs.core.MultivaluedMap; 035import org.apache.hbase.thirdparty.javax.ws.rs.core.Response; 036import org.apache.hbase.thirdparty.javax.ws.rs.core.UriInfo; 037 038@InterfaceAudience.Private 039public class MultiRowResource extends ResourceBase implements Constants { 040 private static final Logger LOG = LoggerFactory.getLogger(MultiRowResource.class); 041 042 TableResource tableResource; 043 Integer versions = null; 044 String[] columns = null; 045 046 /** 047 * Constructor nn * @throws java.io.IOException 048 */ 049 public MultiRowResource(TableResource tableResource, String versions, String columnsStr) 050 throws IOException { 051 super(); 052 this.tableResource = tableResource; 053 054 if (columnsStr != null && !columnsStr.equals("")) { 055 this.columns = columnsStr.split(","); 056 } 057 058 if (versions != null) { 059 this.versions = Integer.valueOf(versions); 060 061 } 062 } 063 064 @GET 065 @Produces({ MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF, MIMETYPE_PROTOBUF_IETF }) 066 public Response get(final @Context UriInfo uriInfo) { 067 MultivaluedMap<String, String> params = uriInfo.getQueryParameters(); 068 069 servlet.getMetrics().incrementRequests(1); 070 try { 071 CellSetModel model = new CellSetModel(); 072 for (String rk : params.get(ROW_KEYS_PARAM_NAME)) { 073 RowSpec rowSpec = new RowSpec(rk); 074 075 if (this.versions != null) { 076 rowSpec.setMaxVersions(this.versions); 077 } 078 079 if (this.columns != null) { 080 for (int i = 0; i < this.columns.length; i++) { 081 rowSpec.addColumn(Bytes.toBytes(this.columns[i])); 082 } 083 } 084 085 ResultGenerator generator = ResultGenerator.fromRowSpec(this.tableResource.getName(), 086 rowSpec, null, !params.containsKey(NOCACHE_PARAM_NAME)); 087 Cell value = null; 088 RowModel rowModel = new RowModel(rowSpec.getRow()); 089 if (generator.hasNext()) { 090 while ((value = generator.next()) != null) { 091 rowModel.addCell(new CellModel(CellUtil.cloneFamily(value), 092 CellUtil.cloneQualifier(value), value.getTimestamp(), CellUtil.cloneValue(value))); 093 } 094 model.addRow(rowModel); 095 } else { 096 if (LOG.isTraceEnabled()) { 097 LOG.trace("The row : " + rk + " not found in the table."); 098 } 099 } 100 } 101 102 if (model.getRows().isEmpty()) { 103 // If no rows found. 104 servlet.getMetrics().incrementFailedGetRequests(1); 105 return Response.status(Response.Status.NOT_FOUND).type(MIMETYPE_TEXT) 106 .entity("No rows found." + CRLF).build(); 107 } else { 108 servlet.getMetrics().incrementSucessfulGetRequests(1); 109 return Response.ok(model).build(); 110 } 111 } catch (IOException e) { 112 servlet.getMetrics().incrementFailedGetRequests(1); 113 return processException(e); 114 } 115 } 116}