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.thrift2;
21
22 import org.apache.hadoop.hbase.HConstants;
23 import org.apache.hadoop.hbase.KeyValue;
24 import org.apache.hadoop.hbase.client.*;
25 import org.apache.hadoop.hbase.thrift2.generated.*;
26
27 import java.io.IOException;
28 import java.nio.ByteBuffer;
29 import java.util.*;
30
31 public class ThriftUtilities {
32
33 private ThriftUtilities() {
34 throw new UnsupportedOperationException("Can't initialize class");
35 }
36
37
38
39
40
41
42
43
44
45
46
47
48 public static Get getFromThrift(TGet in) throws IOException {
49 Get out = new Get(in.getRow());
50
51
52 if (in.isSetTimestamp()) {
53 out.setTimeStamp(in.getTimestamp());
54 } else if (in.isSetTimeRange()) {
55 out.setTimeRange(in.getTimeRange().getMinStamp(), in.getTimeRange().getMaxStamp());
56 }
57
58 if (in.isSetMaxVersions()) {
59 out.setMaxVersions(in.getMaxVersions());
60 }
61
62 if (!in.isSetColumns()) {
63 return out;
64 }
65
66 for (TColumn column : in.getColumns()) {
67 if (column.isSetQualifier()) {
68 out.addColumn(column.getFamily(), column.getQualifier());
69 } else {
70 out.addFamily(column.getFamily());
71 }
72 }
73
74 return out;
75 }
76
77
78
79
80
81
82
83
84
85
86
87 public static List<Get> getsFromThrift(List<TGet> in) throws IOException {
88 List<Get> out = new ArrayList<Get>(in.size());
89 for (TGet get : in) {
90 out.add(getFromThrift(get));
91 }
92 return out;
93 }
94
95
96
97
98
99
100
101
102 public static TResult resultFromHBase(Result in) {
103 KeyValue[] raw = in.raw();
104 TResult out = new TResult();
105 byte[] row = in.getRow();
106 if (row != null) {
107 out.setRow(in.getRow());
108 }
109 List<TColumnValue> columnValues = new ArrayList<TColumnValue>();
110 for (KeyValue kv : raw) {
111 TColumnValue col = new TColumnValue();
112 col.setFamily(kv.getFamily());
113 col.setQualifier(kv.getQualifier());
114 col.setTimestamp(kv.getTimestamp());
115 col.setValue(kv.getValue());
116 columnValues.add(col);
117 }
118 out.setColumnValues(columnValues);
119 return out;
120 }
121
122
123
124
125
126
127
128
129
130
131 public static List<TResult> resultsFromHBase(Result[] in) {
132 List<TResult> out = new ArrayList<TResult>(in.length);
133 for (Result result : in) {
134 out.add(resultFromHBase(result));
135 }
136 return out;
137 }
138
139
140
141
142
143
144
145
146 public static Put putFromThrift(TPut in) {
147 Put out;
148
149 if (in.isSetTimestamp()) {
150 out = new Put(in.getRow(), in.getTimestamp(), null);
151 } else {
152 out = new Put(in.getRow());
153 }
154
155 out.setWriteToWAL(in.isWriteToWal());
156
157 for (TColumnValue columnValue : in.getColumnValues()) {
158 if (columnValue.isSetTimestamp()) {
159 out.add(columnValue.getFamily(), columnValue.getQualifier(), columnValue.getTimestamp(),
160 columnValue.getValue());
161 } else {
162 out.add(columnValue.getFamily(), columnValue.getQualifier(), columnValue.getValue());
163 }
164 }
165
166 return out;
167 }
168
169
170
171
172
173
174
175
176
177
178 public static List<Put> putsFromThrift(List<TPut> in) {
179 List<Put> out = new ArrayList<Put>(in.size());
180 for (TPut put : in) {
181 out.add(putFromThrift(put));
182 }
183 return out;
184 }
185
186
187
188
189
190
191
192
193 public static Delete deleteFromThrift(TDelete in) {
194 Delete out;
195
196 if (in.isSetColumns()) {
197 out = new Delete(in.getRow());
198 for (TColumn column : in.getColumns()) {
199 if (column.isSetQualifier()) {
200 if (column.isSetTimestamp()) {
201 if (in.isSetDeleteType() &&
202 in.getDeleteType().equals(TDeleteType.DELETE_COLUMNS))
203 out.deleteColumns(column.getFamily(), column.getQualifier(), column.getTimestamp());
204 else
205 out.deleteColumn(column.getFamily(), column.getQualifier(), column.getTimestamp());
206 } else {
207 if (in.isSetDeleteType() &&
208 in.getDeleteType().equals(TDeleteType.DELETE_COLUMNS))
209 out.deleteColumns(column.getFamily(), column.getQualifier());
210 else
211 out.deleteColumn(column.getFamily(), column.getQualifier());
212 }
213
214 } else {
215 if (column.isSetTimestamp()) {
216 out.deleteFamily(column.getFamily(), column.getTimestamp());
217 } else {
218 out.deleteFamily(column.getFamily());
219 }
220 }
221 }
222 } else {
223 if (in.isSetTimestamp()) {
224 out = new Delete(in.getRow(), in.getTimestamp(), null);
225 } else {
226 out = new Delete(in.getRow());
227 }
228 }
229 out.setWriteToWAL(in.isWriteToWal());
230 return out;
231 }
232
233
234
235
236
237
238
239
240
241
242
243 public static List<Delete> deletesFromThrift(List<TDelete> in) {
244 List<Delete> out = new ArrayList<Delete>(in.size());
245 for (TDelete delete : in) {
246 out.add(deleteFromThrift(delete));
247 }
248 return out;
249 }
250
251 public static TDelete deleteFromHBase(Delete in) {
252 TDelete out = new TDelete(ByteBuffer.wrap(in.getRow()));
253
254 List<TColumn> columns = new ArrayList<TColumn>();
255 long rowTimestamp = in.getTimeStamp();
256 if (rowTimestamp != HConstants.LATEST_TIMESTAMP) {
257 out.setTimestamp(rowTimestamp);
258 }
259
260
261 for (Map.Entry<byte[], List<KeyValue>> familyEntry : in.getFamilyMap().entrySet()) {
262 TColumn column = new TColumn(ByteBuffer.wrap(familyEntry.getKey()));
263 for (KeyValue keyValue : familyEntry.getValue()) {
264 byte[] family = keyValue.getFamily();
265 byte[] qualifier = keyValue.getQualifier();
266 long timestamp = keyValue.getTimestamp();
267 if (family != null) {
268 column.setFamily(family);
269 }
270 if (qualifier != null) {
271 column.setQualifier(qualifier);
272 }
273 if (timestamp != HConstants.LATEST_TIMESTAMP) {
274 column.setTimestamp(keyValue.getTimestamp());
275 }
276 }
277 columns.add(column);
278 }
279 out.setColumns(columns);
280
281 return out;
282 }
283
284 public static List<TDelete> deletesFromHBase(List<Delete> in) {
285 List<TDelete> out = new ArrayList<TDelete>(in.size());
286 for (Delete delete : in) {
287 if (delete == null) {
288 out.add(null);
289 } else {
290 out.add(deleteFromHBase(delete));
291 }
292 }
293 return out;
294 }
295
296 public static Scan scanFromThrift(TScan in) throws IOException {
297 Scan out = new Scan();
298
299 if (in.isSetStartRow())
300 out.setStartRow(in.getStartRow());
301 if (in.isSetStopRow())
302 out.setStopRow(in.getStopRow());
303 if (in.isSetCaching())
304 out.setCaching(in.getCaching());
305 if (in.isSetMaxVersions()) {
306 out.setMaxVersions(in.getMaxVersions());
307 }
308
309 if (in.isSetColumns()) {
310 for (TColumn column : in.getColumns()) {
311 if (column.isSetQualifier()) {
312 out.addColumn(column.getFamily(), column.getQualifier());
313 } else {
314 out.addFamily(column.getFamily());
315 }
316 }
317 }
318
319 TTimeRange timeRange = in.getTimeRange();
320 if (timeRange != null &&
321 timeRange.isSetMinStamp() && timeRange.isSetMaxStamp()) {
322 out.setTimeRange(timeRange.getMinStamp(), timeRange.getMaxStamp());
323 }
324
325 return out;
326 }
327
328 public static Increment incrementFromThrift(TIncrement in) throws IOException {
329 Increment out = new Increment(in.getRow());
330 for (TColumnIncrement column : in.getColumns()) {
331 out.addColumn(column.getFamily(), column.getQualifier(), column.getAmount());
332 }
333 out.setWriteToWAL(in.isWriteToWal());
334 return out;
335 }
336 }