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