1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.client;
20
21
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.NavigableSet;
28 import java.util.Set;
29 import java.util.TreeMap;
30 import java.util.TreeSet;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.hadoop.hbase.classification.InterfaceAudience;
35 import org.apache.hadoop.hbase.classification.InterfaceStability;
36 import org.apache.hadoop.hbase.HConstants;
37 import org.apache.hadoop.hbase.filter.Filter;
38 import org.apache.hadoop.hbase.io.TimeRange;
39 import org.apache.hadoop.hbase.security.access.Permission;
40 import org.apache.hadoop.hbase.security.visibility.Authorizations;
41 import org.apache.hadoop.hbase.util.Bytes;
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 @InterfaceAudience.Public
67 @InterfaceStability.Stable
68 public class Get extends Query
69 implements Row, Comparable<Row> {
70 private static final Log LOG = LogFactory.getLog(Get.class);
71
72 private byte [] row = null;
73 private int maxVersions = 1;
74 private boolean cacheBlocks = true;
75 private int storeLimit = -1;
76 private int storeOffset = 0;
77 private TimeRange tr = new TimeRange();
78 private boolean checkExistenceOnly = false;
79 private boolean closestRowBefore = false;
80 private Map<byte [], NavigableSet<byte []>> familyMap =
81 new TreeMap<byte [], NavigableSet<byte []>>(Bytes.BYTES_COMPARATOR);
82
83
84
85
86
87
88
89
90 public Get(byte [] row) {
91 Mutation.checkRow(row);
92 this.row = row;
93 }
94
95
96
97
98
99
100 public Get(Get get) {
101 this(get.getRow());
102
103 this.setFilter(get.getFilter());
104 this.setReplicaId(get.getReplicaId());
105 this.setConsistency(get.getConsistency());
106
107 this.cacheBlocks = get.getCacheBlocks();
108 this.maxVersions = get.getMaxVersions();
109 this.storeLimit = get.getMaxResultsPerColumnFamily();
110 this.storeOffset = get.getRowOffsetPerColumnFamily();
111 this.tr = get.getTimeRange();
112 this.checkExistenceOnly = get.isCheckExistenceOnly();
113 this.closestRowBefore = get.isClosestRowBefore();
114 Map<byte[], NavigableSet<byte[]>> fams = get.getFamilyMap();
115 for (Map.Entry<byte[],NavigableSet<byte[]>> entry : fams.entrySet()) {
116 byte [] fam = entry.getKey();
117 NavigableSet<byte[]> cols = entry.getValue();
118 if (cols != null && cols.size() > 0) {
119 for (byte[] col : cols) {
120 addColumn(fam, col);
121 }
122 } else {
123 addFamily(fam);
124 }
125 }
126 for (Map.Entry<String, byte[]> attr : get.getAttributesMap().entrySet()) {
127 setAttribute(attr.getKey(), attr.getValue());
128 }
129 for (Map.Entry<byte[], TimeRange> entry : get.getColumnFamilyTimeRange().entrySet()) {
130 TimeRange tr = entry.getValue();
131 setColumnFamilyTimeRange(entry.getKey(), tr.getMin(), tr.getMax());
132 }
133 }
134
135 public boolean isCheckExistenceOnly() {
136 return checkExistenceOnly;
137 }
138
139 public Get setCheckExistenceOnly(boolean checkExistenceOnly) {
140 this.checkExistenceOnly = checkExistenceOnly;
141 return this;
142 }
143
144 public boolean isClosestRowBefore() {
145 return closestRowBefore;
146 }
147
148 public Get setClosestRowBefore(boolean closestRowBefore) {
149 this.closestRowBefore = closestRowBefore;
150 return this;
151 }
152
153
154
155
156
157
158
159
160 public Get addFamily(byte [] family) {
161 familyMap.remove(family);
162 familyMap.put(family, null);
163 return this;
164 }
165
166
167
168
169
170
171
172
173
174 public Get addColumn(byte [] family, byte [] qualifier) {
175 NavigableSet<byte []> set = familyMap.get(family);
176 if(set == null) {
177 set = new TreeSet<byte []>(Bytes.BYTES_COMPARATOR);
178 }
179 if (qualifier == null) {
180 qualifier = HConstants.EMPTY_BYTE_ARRAY;
181 }
182 set.add(qualifier);
183 familyMap.put(family, set);
184 return this;
185 }
186
187
188
189
190
191
192
193
194
195 public Get setTimeRange(long minStamp, long maxStamp) throws IOException {
196 tr = new TimeRange(minStamp, maxStamp);
197 return this;
198 }
199
200
201
202
203
204
205 public Get setTimeStamp(long timestamp)
206 throws IOException {
207 try {
208 tr = new TimeRange(timestamp, timestamp+1);
209 } catch(Exception e) {
210
211 LOG.error("TimeRange failed, likely caused by integer overflow. ", e);
212 throw e;
213 }
214 return this;
215 }
216
217 @Override public Get setColumnFamilyTimeRange(byte[] cf, long minStamp, long maxStamp) {
218 return (Get) super.setColumnFamilyTimeRange(cf, minStamp, maxStamp);
219 }
220
221
222
223
224
225 public Get setMaxVersions() {
226 this.maxVersions = Integer.MAX_VALUE;
227 return this;
228 }
229
230
231
232
233
234
235
236 public Get setMaxVersions(int maxVersions) throws IOException {
237 if(maxVersions <= 0) {
238 throw new IOException("maxVersions must be positive");
239 }
240 this.maxVersions = maxVersions;
241 return this;
242 }
243
244
245
246
247
248
249 public Get setMaxResultsPerColumnFamily(int limit) {
250 this.storeLimit = limit;
251 return this;
252 }
253
254
255
256
257
258
259
260 public Get setRowOffsetPerColumnFamily(int offset) {
261 this.storeOffset = offset;
262 return this;
263 }
264
265 @Override
266 public Get setFilter(Filter filter) {
267 super.setFilter(filter);
268 return this;
269 }
270
271
272
273
274
275
276
277
278
279
280
281
282
283 public Get setCacheBlocks(boolean cacheBlocks) {
284 this.cacheBlocks = cacheBlocks;
285 return this;
286 }
287
288
289
290
291
292
293 public boolean getCacheBlocks() {
294 return cacheBlocks;
295 }
296
297
298
299
300
301 @Override
302 public byte [] getRow() {
303 return this.row;
304 }
305
306
307
308
309
310 public int getMaxVersions() {
311 return this.maxVersions;
312 }
313
314
315
316
317
318
319 public int getMaxResultsPerColumnFamily() {
320 return this.storeLimit;
321 }
322
323
324
325
326
327
328 public int getRowOffsetPerColumnFamily() {
329 return this.storeOffset;
330 }
331
332
333
334
335
336 public TimeRange getTimeRange() {
337 return this.tr;
338 }
339
340
341
342
343
344 public Set<byte[]> familySet() {
345 return this.familyMap.keySet();
346 }
347
348
349
350
351
352 public int numFamilies() {
353 return this.familyMap.size();
354 }
355
356
357
358
359
360 public boolean hasFamilies() {
361 return !this.familyMap.isEmpty();
362 }
363
364
365
366
367
368 public Map<byte[],NavigableSet<byte[]>> getFamilyMap() {
369 return this.familyMap;
370 }
371
372
373
374
375
376
377
378 @Override
379 public Map<String, Object> getFingerprint() {
380 Map<String, Object> map = new HashMap<String, Object>();
381 List<String> families = new ArrayList<String>();
382 map.put("families", families);
383 for (Map.Entry<byte [], NavigableSet<byte[]>> entry :
384 this.familyMap.entrySet()) {
385 families.add(Bytes.toStringBinary(entry.getKey()));
386 }
387 return map;
388 }
389
390
391
392
393
394
395
396
397 @Override
398 public Map<String, Object> toMap(int maxCols) {
399
400 Map<String, Object> map = getFingerprint();
401
402
403 Map<String, List<String>> columns = new HashMap<String, List<String>>();
404 map.put("families", columns);
405
406 map.put("row", Bytes.toStringBinary(this.row));
407 map.put("maxVersions", this.maxVersions);
408 map.put("cacheBlocks", this.cacheBlocks);
409 List<Long> timeRange = new ArrayList<Long>();
410 timeRange.add(this.tr.getMin());
411 timeRange.add(this.tr.getMax());
412 map.put("timeRange", timeRange);
413 int colCount = 0;
414
415 for (Map.Entry<byte [], NavigableSet<byte[]>> entry :
416 this.familyMap.entrySet()) {
417 List<String> familyList = new ArrayList<String>();
418 columns.put(Bytes.toStringBinary(entry.getKey()), familyList);
419 if(entry.getValue() == null) {
420 colCount++;
421 --maxCols;
422 familyList.add("ALL");
423 } else {
424 colCount += entry.getValue().size();
425 if (maxCols <= 0) {
426 continue;
427 }
428 for (byte [] column : entry.getValue()) {
429 if (--maxCols <= 0) {
430 continue;
431 }
432 familyList.add(Bytes.toStringBinary(column));
433 }
434 }
435 }
436 map.put("totalColumns", colCount);
437 if (this.filter != null) {
438 map.put("filter", this.filter.toString());
439 }
440
441 if (getId() != null) {
442 map.put("id", getId());
443 }
444 return map;
445 }
446
447
448 @Override
449 public int compareTo(Row other) {
450
451 return Bytes.compareTo(this.getRow(), other.getRow());
452 }
453
454 @Override
455 public int hashCode() {
456
457
458 return Bytes.hashCode(this.getRow());
459 }
460
461 @Override
462 public boolean equals(Object obj) {
463 if (this == obj) {
464 return true;
465 }
466 if (obj == null || getClass() != obj.getClass()) {
467 return false;
468 }
469 Row other = (Row) obj;
470
471 return compareTo(other) == 0;
472 }
473
474 @Override
475 public Get setAttribute(String name, byte[] value) {
476 return (Get) super.setAttribute(name, value);
477 }
478
479 @Override
480 public Get setId(String id) {
481 return (Get) super.setId(id);
482 }
483
484 @Override
485 public Get setAuthorizations(Authorizations authorizations) {
486 return (Get) super.setAuthorizations(authorizations);
487 }
488
489 @Override
490 public Get setACL(Map<String, Permission> perms) {
491 return (Get) super.setACL(perms);
492 }
493
494 @Override
495 public Get setACL(String user, Permission perms) {
496 return (Get) super.setACL(user, perms);
497 }
498
499 @Override
500 public Get setConsistency(Consistency consistency) {
501 return (Get) super.setConsistency(consistency);
502 }
503
504 @Override
505 public Get setReplicaId(int Id) {
506 return (Get) super.setReplicaId(Id);
507 }
508
509 @Override
510 public Get setIsolationLevel(IsolationLevel level) {
511 return (Get) super.setIsolationLevel(level);
512 }
513
514 }