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 com.fasterxml.jackson.annotation.JsonProperty;
23
24 import java.io.IOException;
25 import java.io.Serializable;
26
27 import javax.xml.bind.annotation.XmlAccessType;
28 import javax.xml.bind.annotation.XmlAccessorType;
29 import javax.xml.bind.annotation.XmlAttribute;
30 import javax.xml.bind.annotation.XmlRootElement;
31 import javax.xml.bind.annotation.XmlValue;
32
33 import org.apache.hadoop.hbase.util.ByteStringer;
34 import org.apache.hadoop.hbase.classification.InterfaceAudience;
35 import org.apache.hadoop.hbase.CellUtil;
36 import org.apache.hadoop.hbase.HConstants;
37 import org.apache.hadoop.hbase.KeyValue;
38 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
39 import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
40 import org.apache.hadoop.hbase.rest.protobuf.generated.CellMessage.Cell;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 @XmlRootElement(name="Cell")
62 @XmlAccessorType(XmlAccessType.FIELD)
63 @InterfaceAudience.Private
64 public class CellModel implements ProtobufMessageHandler, Serializable {
65 private static final long serialVersionUID = 1L;
66
67 @JsonProperty("column")
68 @XmlAttribute
69 private byte[] column;
70
71 @JsonProperty("timestamp")
72 @XmlAttribute
73 private long timestamp = HConstants.LATEST_TIMESTAMP;
74
75 @JsonProperty("$")
76 @XmlValue
77 private byte[] value;
78
79
80
81
82 public CellModel() {}
83
84
85
86
87
88
89 public CellModel(byte[] column, byte[] value) {
90 this(column, HConstants.LATEST_TIMESTAMP, value);
91 }
92
93
94
95
96
97
98
99 public CellModel(byte[] column, byte[] qualifier, byte[] value) {
100 this(column, qualifier, HConstants.LATEST_TIMESTAMP, value);
101 }
102
103
104
105
106
107 public CellModel(org.apache.hadoop.hbase.Cell cell) {
108 this(CellUtil.cloneFamily(cell), CellUtil.cloneQualifier(cell), cell.getTimestamp(), CellUtil
109 .cloneValue(cell));
110 }
111
112
113
114
115
116
117
118 public CellModel(byte[] column, long timestamp, byte[] value) {
119 this.column = column;
120 this.timestamp = timestamp;
121 this.value = value;
122 }
123
124
125
126
127
128
129
130
131 public CellModel(byte[] column, byte[] qualifier, long timestamp,
132 byte[] value) {
133 this.column = KeyValue.makeColumn(column, qualifier);
134 this.timestamp = timestamp;
135 this.value = value;
136 }
137
138
139
140
141 public byte[] getColumn() {
142 return column;
143 }
144
145
146
147
148 public void setColumn(byte[] column) {
149 this.column = column;
150 }
151
152
153
154
155
156 public boolean hasUserTimestamp() {
157 return timestamp != HConstants.LATEST_TIMESTAMP;
158 }
159
160
161
162
163 public long getTimestamp() {
164 return timestamp;
165 }
166
167
168
169
170 public void setTimestamp(long timestamp) {
171 this.timestamp = timestamp;
172 }
173
174
175
176
177 public byte[] getValue() {
178 return value;
179 }
180
181
182
183
184 public void setValue(byte[] value) {
185 this.value = value;
186 }
187
188 @Override
189 public byte[] createProtobufOutput() {
190 Cell.Builder builder = Cell.newBuilder();
191 builder.setColumn(ByteStringer.wrap(getColumn()));
192 builder.setData(ByteStringer.wrap(getValue()));
193 if (hasUserTimestamp()) {
194 builder.setTimestamp(getTimestamp());
195 }
196 return builder.build().toByteArray();
197 }
198
199 @Override
200 public ProtobufMessageHandler getObjectFromMessage(byte[] message)
201 throws IOException {
202 Cell.Builder builder = Cell.newBuilder();
203 ProtobufUtil.mergeFrom(builder, message);
204 setColumn(builder.getColumn().toByteArray());
205 setValue(builder.getData().toByteArray());
206 if (builder.hasTimestamp()) {
207 setTimestamp(builder.getTimestamp());
208 }
209 return this;
210 }
211 }