1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.rest.model;
21
22 import java.io.IOException;
23 import java.io.Serializable;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import javax.xml.bind.annotation.XmlRootElement;
28 import javax.xml.bind.annotation.XmlElement;
29
30 import org.apache.hadoop.classification.InterfaceAudience;
31 import org.apache.hadoop.hbase.HConstants;
32 import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
33 import org.apache.hadoop.hbase.rest.protobuf.generated.CellMessage.Cell;
34 import org.apache.hadoop.hbase.rest.protobuf.generated.CellSetMessage.CellSet;
35
36 import com.google.protobuf.ByteString;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 @XmlRootElement(name="CellSet")
72 @InterfaceAudience.Private
73 public class CellSetModel implements Serializable, ProtobufMessageHandler {
74
75 private static final long serialVersionUID = 1L;
76
77 private List<RowModel> rows;
78
79
80
81
82 public CellSetModel() {
83 this.rows = new ArrayList<RowModel>();
84 }
85
86
87
88
89 public CellSetModel(List<RowModel> rows) {
90 super();
91 this.rows = rows;
92 }
93
94
95
96
97
98 public void addRow(RowModel row) {
99 rows.add(row);
100 }
101
102
103
104
105 @XmlElement(name="Row")
106 public List<RowModel> getRows() {
107 return rows;
108 }
109
110 @Override
111 public byte[] createProtobufOutput() {
112 CellSet.Builder builder = CellSet.newBuilder();
113 for (RowModel row: getRows()) {
114 CellSet.Row.Builder rowBuilder = CellSet.Row.newBuilder();
115 rowBuilder.setKey(ByteString.copyFrom(row.getKey()));
116 for (CellModel cell: row.getCells()) {
117 Cell.Builder cellBuilder = Cell.newBuilder();
118 cellBuilder.setColumn(ByteString.copyFrom(cell.getColumn()));
119 cellBuilder.setData(ByteString.copyFrom(cell.getValue()));
120 if (cell.hasUserTimestamp()) {
121 cellBuilder.setTimestamp(cell.getTimestamp());
122 }
123 rowBuilder.addValues(cellBuilder);
124 }
125 builder.addRows(rowBuilder);
126 }
127 return builder.build().toByteArray();
128 }
129
130 @Override
131 public ProtobufMessageHandler getObjectFromMessage(byte[] message)
132 throws IOException {
133 CellSet.Builder builder = CellSet.newBuilder();
134 builder.mergeFrom(message);
135 for (CellSet.Row row: builder.getRowsList()) {
136 RowModel rowModel = new RowModel(row.getKey().toByteArray());
137 for (Cell cell: row.getValuesList()) {
138 long timestamp = HConstants.LATEST_TIMESTAMP;
139 if (cell.hasTimestamp()) {
140 timestamp = cell.getTimestamp();
141 }
142 rowModel.addCell(
143 new CellModel(cell.getColumn().toByteArray(), timestamp,
144 cell.getData().toByteArray()));
145 }
146 addRow(rowModel);
147 }
148 return this;
149 }
150 }