1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 LOG.debug("Created ScanStreamingUtil with content type = " + this.contentType + " user limit : "
53 + this.limit + " scan fetch size : " + this.fetchSize);
54 }
55
56 @Override
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
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
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 }