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