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