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.client;
21
22 import org.apache.hadoop.conf.Configuration;
23 import org.apache.hadoop.hbase.KeyValue;
24 import org.apache.hadoop.hbase.filter.Filter;
25 import org.apache.hadoop.hbase.io.TimeRange;
26 import org.apache.hadoop.hbase.util.Bytes;
27 import org.apache.hadoop.io.Writable;
28 import org.apache.hadoop.io.WritableFactories;
29
30 import java.io.DataInput;
31 import java.io.DataOutput;
32 import java.io.IOException;
33 import java.util.Map;
34 import java.util.NavigableSet;
35 import java.util.Set;
36 import java.util.TreeMap;
37 import java.util.TreeSet;
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 public class Get implements Writable {
64 private static final byte GET_VERSION = (byte)1;
65
66 private byte [] row = null;
67 private long lockId = -1L;
68 private int maxVersions = 1;
69 private Filter filter = null;
70 private TimeRange tr = new TimeRange();
71 private Map<byte [], NavigableSet<byte []>> familyMap =
72 new TreeMap<byte [], NavigableSet<byte []>>(Bytes.BYTES_COMPARATOR);
73
74
75 public Get() {}
76
77
78
79
80
81
82
83
84 public Get(byte [] row) {
85 this(row, null);
86 }
87
88
89
90
91
92
93
94
95
96 public Get(byte [] row, RowLock rowLock) {
97 this.row = row;
98 if(rowLock != null) {
99 this.lockId = rowLock.getLockId();
100 }
101 }
102
103
104
105
106
107
108
109
110 public Get addFamily(byte [] family) {
111 familyMap.remove(family);
112 familyMap.put(family, null);
113 return this;
114 }
115
116
117
118
119
120
121
122
123
124 public Get addColumn(byte [] family, byte [] qualifier) {
125 NavigableSet<byte []> set = familyMap.get(family);
126 if(set == null) {
127 set = new TreeSet<byte []>(Bytes.BYTES_COMPARATOR);
128 }
129 set.add(qualifier);
130 familyMap.put(family, set);
131 return this;
132 }
133
134
135
136
137
138
139
140
141
142 public Get setTimeRange(long minStamp, long maxStamp)
143 throws IOException {
144 tr = new TimeRange(minStamp, maxStamp);
145 return this;
146 }
147
148
149
150
151
152
153 public Get setTimeStamp(long timestamp) {
154 try {
155 tr = new TimeRange(timestamp, timestamp+1);
156 } catch(IOException e) {
157
158 }
159 return this;
160 }
161
162
163
164
165
166 public Get setMaxVersions() {
167 this.maxVersions = Integer.MAX_VALUE;
168 return this;
169 }
170
171
172
173
174
175
176
177 public Get setMaxVersions(int maxVersions) throws IOException {
178 if(maxVersions <= 0) {
179 throw new IOException("maxVersions must be positive");
180 }
181 this.maxVersions = maxVersions;
182 return this;
183 }
184
185
186
187
188
189
190
191
192 public Get setFilter(Filter filter) {
193 this.filter = filter;
194 return this;
195 }
196
197
198
199
200
201
202 public Filter getFilter() {
203 return this.filter;
204 }
205
206
207
208
209
210 public byte [] getRow() {
211 return this.row;
212 }
213
214
215
216
217
218 public RowLock getRowLock() {
219 return new RowLock(this.row, this.lockId);
220 }
221
222
223
224
225
226 public long getLockId() {
227 return this.lockId;
228 }
229
230
231
232
233
234 public int getMaxVersions() {
235 return this.maxVersions;
236 }
237
238
239
240
241
242 public TimeRange getTimeRange() {
243 return this.tr;
244 }
245
246
247
248
249
250 public Set<byte[]> familySet() {
251 return this.familyMap.keySet();
252 }
253
254
255
256
257
258 public int numFamilies() {
259 return this.familyMap.size();
260 }
261
262
263
264
265
266 public boolean hasFamilies() {
267 return !this.familyMap.isEmpty();
268 }
269
270
271
272
273
274 public Map<byte[],NavigableSet<byte[]>> getFamilyMap() {
275 return this.familyMap;
276 }
277
278
279
280
281 @Override
282 public String toString() {
283 StringBuilder sb = new StringBuilder();
284 sb.append("row=");
285 sb.append(Bytes.toString(this.row));
286 sb.append(", maxVersions=");
287 sb.append("").append(this.maxVersions);
288 sb.append(", timeRange=");
289 sb.append("[").append(this.tr.getMin()).append(",");
290 sb.append(this.tr.getMax()).append(")");
291 sb.append(", families=");
292 if(this.familyMap.size() == 0) {
293 sb.append("ALL");
294 return sb.toString();
295 }
296 boolean moreThanOne = false;
297 for(Map.Entry<byte [], NavigableSet<byte[]>> entry :
298 this.familyMap.entrySet()) {
299 if(moreThanOne) {
300 sb.append("), ");
301 } else {
302 moreThanOne = true;
303 sb.append("{");
304 }
305 sb.append("(family=");
306 sb.append(Bytes.toString(entry.getKey()));
307 sb.append(", columns=");
308 if(entry.getValue() == null) {
309 sb.append("ALL");
310 } else {
311 sb.append("{");
312 boolean moreThanOneB = false;
313 for(byte [] column : entry.getValue()) {
314 if(moreThanOneB) {
315 sb.append(", ");
316 } else {
317 moreThanOneB = true;
318 }
319 sb.append(Bytes.toString(column));
320 }
321 sb.append("}");
322 }
323 }
324 sb.append("}");
325 return sb.toString();
326 }
327
328
329 public void readFields(final DataInput in)
330 throws IOException {
331 int version = in.readByte();
332 if (version > GET_VERSION) {
333 throw new IOException("unsupported version");
334 }
335 this.row = Bytes.readByteArray(in);
336 this.lockId = in.readLong();
337 this.maxVersions = in.readInt();
338 boolean hasFilter = in.readBoolean();
339 if (hasFilter) {
340 this.filter = (Filter)createForName(Bytes.toString(Bytes.readByteArray(in)));
341 this.filter.readFields(in);
342 }
343 this.tr = new TimeRange();
344 tr.readFields(in);
345 int numFamilies = in.readInt();
346 this.familyMap =
347 new TreeMap<byte [],NavigableSet<byte []>>(Bytes.BYTES_COMPARATOR);
348 for(int i=0; i<numFamilies; i++) {
349 byte [] family = Bytes.readByteArray(in);
350 boolean hasColumns = in.readBoolean();
351 NavigableSet<byte []> set = null;
352 if(hasColumns) {
353 int numColumns = in.readInt();
354 set = new TreeSet<byte []>(Bytes.BYTES_COMPARATOR);
355 for(int j=0; j<numColumns; j++) {
356 byte [] qualifier = Bytes.readByteArray(in);
357 set.add(qualifier);
358 }
359 }
360 this.familyMap.put(family, set);
361 }
362 }
363
364 public void write(final DataOutput out)
365 throws IOException {
366 out.writeByte(GET_VERSION);
367 Bytes.writeByteArray(out, this.row);
368 out.writeLong(this.lockId);
369 out.writeInt(this.maxVersions);
370 if(this.filter == null) {
371 out.writeBoolean(false);
372 } else {
373 out.writeBoolean(true);
374 Bytes.writeByteArray(out, Bytes.toBytes(filter.getClass().getName()));
375 filter.write(out);
376 }
377 tr.write(out);
378 out.writeInt(familyMap.size());
379 for(Map.Entry<byte [], NavigableSet<byte []>> entry :
380 familyMap.entrySet()) {
381 Bytes.writeByteArray(out, entry.getKey());
382 NavigableSet<byte []> columnSet = entry.getValue();
383 if(columnSet == null) {
384 out.writeBoolean(false);
385 } else {
386 out.writeBoolean(true);
387 out.writeInt(columnSet.size());
388 for(byte [] qualifier : columnSet) {
389 Bytes.writeByteArray(out, qualifier);
390 }
391 }
392 }
393 }
394
395 @SuppressWarnings("unchecked")
396 private Writable createForName(String className) {
397 try {
398 Class<? extends Writable> clazz =
399 (Class<? extends Writable>) Class.forName(className);
400 return WritableFactories.newInstance(clazz, new Configuration());
401 } catch (ClassNotFoundException e) {
402 throw new RuntimeException("Can't find class " + className);
403 }
404 }
405
406
407
408
409
410
411
412
413
414 @SuppressWarnings({"deprecation"})
415 public Get addColumns(byte [][] columns) {
416 if (columns == null) return this;
417 for (byte[] column : columns) {
418 try {
419 addColumn(column);
420 } catch (Exception ignored) {
421 }
422 }
423 return this;
424 }
425
426
427
428
429
430
431
432 public Get addColumn(final byte [] column) {
433 if (column == null) return this;
434 byte [][] split = KeyValue.parseColumn(column);
435 if (split.length > 1 && split[1] != null && split[1].length > 0) {
436 addColumn(split[0], split[1]);
437 } else {
438 addFamily(split[0]);
439 }
440 return this;
441 }
442 }