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 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 }