1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 package org.apache.hadoop.hbase.rest; 19 20 import java.io.IOException; 21 import java.io.OutputStream; 22 import java.util.List; 23 24 import javax.ws.rs.WebApplicationException; 25 import javax.ws.rs.core.StreamingOutput; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.apache.hadoop.hbase.Cell; 30 import org.apache.hadoop.hbase.CellUtil; 31 import org.apache.hadoop.hbase.client.Result; 32 import org.apache.hadoop.hbase.client.ResultScanner; 33 import org.apache.hadoop.hbase.rest.model.CellModel; 34 import org.apache.hadoop.hbase.rest.model.CellSetModel; 35 import org.apache.hadoop.hbase.rest.model.RowModel; 36 import org.apache.hadoop.hbase.util.Bytes; 37 38 39 public class ProtobufStreamingUtil implements StreamingOutput { 40 41 private static final Log LOG = LogFactory.getLog(ProtobufStreamingUtil.class); 42 private String contentType; 43 private ResultScanner resultScanner; 44 private int limit; 45 private int fetchSize; 46 ProtobufStreamingUtil(ResultScanner scanner, String type, int limit, int fetchSize)47 protected ProtobufStreamingUtil(ResultScanner scanner, String type, int limit, int fetchSize) { 48 this.resultScanner = scanner; 49 this.contentType = type; 50 this.limit = limit; 51 this.fetchSize = fetchSize; 52 LOG.debug("Created ScanStreamingUtil with content type = " + this.contentType + " user limit : " 53 + this.limit + " scan fetch size : " + this.fetchSize); 54 } 55 56 @Override write(OutputStream outStream)57 public void write(OutputStream outStream) throws IOException, WebApplicationException { 58 Result[] rowsToSend; 59 if(limit < fetchSize){ 60 rowsToSend = this.resultScanner.next(limit); 61 writeToStream(createModelFromResults(rowsToSend), this.contentType, outStream); 62 } else { 63 int count = limit; 64 while (count > 0) { 65 if (count < fetchSize) { 66 rowsToSend = this.resultScanner.next(count); 67 } else { 68 rowsToSend = this.resultScanner.next(this.fetchSize); 69 } 70 if(rowsToSend.length == 0){ 71 break; 72 } 73 count = count - rowsToSend.length; 74 writeToStream(createModelFromResults(rowsToSend), this.contentType, outStream); 75 } 76 } 77 } 78 writeToStream(CellSetModel model, String contentType, OutputStream outStream)79 private void writeToStream(CellSetModel model, String contentType, OutputStream outStream) 80 throws IOException { 81 byte[] objectBytes = model.createProtobufOutput(); 82 outStream.write(Bytes.toBytes((short)objectBytes.length)); 83 outStream.write(objectBytes); 84 outStream.flush(); 85 LOG.trace("Wrote " + model.getRows().size() + " rows to stream successfully."); 86 } 87 createModelFromResults(Result[] results)88 private CellSetModel createModelFromResults(Result[] results) { 89 CellSetModel cellSetModel = new CellSetModel(); 90 for (Result rs : results) { 91 byte[] rowKey = rs.getRow(); 92 RowModel rModel = new RowModel(rowKey); 93 List<Cell> kvs = rs.listCells(); 94 for (Cell kv : kvs) { 95 rModel.addCell(new CellModel(CellUtil.cloneFamily(kv), CellUtil.cloneQualifier(kv), kv 96 .getTimestamp(), CellUtil.cloneValue(kv))); 97 } 98 cellSetModel.addRow(rModel); 99 } 100 return cellSetModel; 101 } 102 } 103