View Javadoc

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  
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      if (LOG.isTraceEnabled()) {
53        LOG.trace("Created ScanStreamingUtil with content type = " + this.contentType
54          + " user limit : " + this.limit + " scan fetch size : " + this.fetchSize);
55      }
56    }
57  
58    @Override
59    public void write(OutputStream outStream) throws IOException, WebApplicationException {
60      Result[] rowsToSend;
61      if(limit < fetchSize){
62        rowsToSend = this.resultScanner.next(limit);
63        writeToStream(createModelFromResults(rowsToSend), this.contentType, outStream);
64      } else {
65        int count = limit;
66        while (count > 0) {
67          if (count < fetchSize) {
68            rowsToSend = this.resultScanner.next(count);
69          } else {
70            rowsToSend = this.resultScanner.next(this.fetchSize);
71          }
72          if(rowsToSend.length == 0){
73            break;
74          }
75          count = count - rowsToSend.length;
76          writeToStream(createModelFromResults(rowsToSend), this.contentType, outStream);
77        }
78      }
79    }
80  
81    private void writeToStream(CellSetModel model, String contentType, OutputStream outStream)
82        throws IOException {
83      byte[] objectBytes = model.createProtobufOutput();
84      outStream.write(Bytes.toBytes((short)objectBytes.length));
85      outStream.write(objectBytes);
86      outStream.flush();
87      if (LOG.isTraceEnabled()) {
88        LOG.trace("Wrote " + model.getRows().size() + " rows to stream successfully.");
89      }
90    }
91  
92    private CellSetModel createModelFromResults(Result[] results) {
93      CellSetModel cellSetModel = new CellSetModel();
94      for (Result rs : results) {
95        byte[] rowKey = rs.getRow();
96        RowModel rModel = new RowModel(rowKey);
97        List<Cell> kvs = rs.listCells();
98        for (Cell kv : kvs) {
99          rModel.addCell(new CellModel(CellUtil.cloneFamily(kv), CellUtil.cloneQualifier(kv), kv
100             .getTimestamp(), CellUtil.cloneValue(kv)));
101       }
102       cellSetModel.addRow(rModel);
103     }
104     return cellSetModel;
105   }
106 }