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.XmlAccessType;
28 import javax.xml.bind.annotation.XmlAccessorType;
29 import javax.xml.bind.annotation.XmlElement;
30 import javax.xml.bind.annotation.XmlRootElement;
31
32 import org.apache.hadoop.hbase.util.ByteStringer;
33 import org.apache.hadoop.hbase.classification.InterfaceAudience;
34 import org.apache.hadoop.hbase.HConstants;
35 import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
36 import org.apache.hadoop.hbase.rest.protobuf.generated.CellMessage.Cell;
37 import org.apache.hadoop.hbase.rest.protobuf.generated.CellSetMessage.CellSet;
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
72 @XmlRootElement(name="CellSet")
73 @XmlAccessorType(XmlAccessType.FIELD)
74 @InterfaceAudience.Private
75 public class CellSetModel implements Serializable, ProtobufMessageHandler {
76
77 private static final long serialVersionUID = 1L;
78
79 @XmlElement(name="Row")
80 private List<RowModel> rows;
81
82
83
84
85 public CellSetModel() {
86 this.rows = new ArrayList<RowModel>();
87 }
88
89
90
91
92 public CellSetModel(List<RowModel> rows) {
93 super();
94 this.rows = rows;
95 }
96
97
98
99
100
101 public void addRow(RowModel row) {
102 rows.add(row);
103 }
104
105
106
107
108 public List<RowModel> getRows() {
109 return rows;
110 }
111
112 @Override
113 public byte[] createProtobufOutput() {
114 CellSet.Builder builder = CellSet.newBuilder();
115 for (RowModel row: getRows()) {
116 CellSet.Row.Builder rowBuilder = CellSet.Row.newBuilder();
117 rowBuilder.setKey(ByteStringer.wrap(row.getKey()));
118 for (CellModel cell: row.getCells()) {
119 Cell.Builder cellBuilder = Cell.newBuilder();
120 cellBuilder.setColumn(ByteStringer.wrap(cell.getColumn()));
121 cellBuilder.setData(ByteStringer.wrap(cell.getValue()));
122 if (cell.hasUserTimestamp()) {
123 cellBuilder.setTimestamp(cell.getTimestamp());
124 }
125 rowBuilder.addValues(cellBuilder);
126 }
127 builder.addRows(rowBuilder);
128 }
129 return builder.build().toByteArray();
130 }
131
132 @Override
133 public ProtobufMessageHandler getObjectFromMessage(byte[] message)
134 throws IOException {
135 CellSet.Builder builder = CellSet.newBuilder();
136 builder.mergeFrom(message);
137 for (CellSet.Row row: builder.getRowsList()) {
138 RowModel rowModel = new RowModel(row.getKey().toByteArray());
139 for (Cell cell: row.getValuesList()) {
140 long timestamp = HConstants.LATEST_TIMESTAMP;
141 if (cell.hasTimestamp()) {
142 timestamp = cell.getTimestamp();
143 }
144 rowModel.addCell(
145 new CellModel(cell.getColumn().toByteArray(), timestamp,
146 cell.getData().toByteArray()));
147 }
148 addRow(rowModel);
149 }
150 return this;
151 }
152 }