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.hadoop.classification.InterfaceAudience;
33 import org.apache.hadoop.classification.InterfaceStability;
34 import org.apache.hadoop.hbase.HConstants;
35 import org.apache.hadoop.hbase.filter.Filter;
36 import org.apache.hadoop.hbase.io.TimeRange;
37 import org.apache.hadoop.hbase.util.Bytes;
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 @InterfaceAudience.Public
63 @InterfaceStability.Stable
64 public class Get extends OperationWithAttributes
65 implements Row, Comparable<Row> {
66
67 private byte [] row = null;
68 private int maxVersions = 1;
69 private boolean cacheBlocks = true;
70 private int storeLimit = -1;
71 private int storeOffset = 0;
72 private Filter filter = null;
73 private TimeRange tr = new TimeRange();
74 private boolean checkExistenceOnly = false;
75 private boolean closestRowBefore = false;
76 private Map<byte [], NavigableSet<byte []>> familyMap =
77 new TreeMap<byte [], NavigableSet<byte []>>(Bytes.BYTES_COMPARATOR);
78
79
80
81
82
83
84
85
86 public Get(byte [] row) {
87 Mutation.checkRow(row);
88 this.row = row;
89 }
90
91 public boolean isCheckExistenceOnly() {
92 return checkExistenceOnly;
93 }
94
95 public void setCheckExistenceOnly(boolean checkExistenceOnly) {
96 this.checkExistenceOnly = checkExistenceOnly;
97 }
98
99 public boolean isClosestRowBefore() {
100 return closestRowBefore;
101 }
102
103 public void setClosestRowBefore(boolean closestRowBefore) {
104 this.closestRowBefore = closestRowBefore;
105 }
106
107
108
109
110
111
112
113
114 public Get addFamily(byte [] family) {
115 familyMap.remove(family);
116 familyMap.put(family, null);
117 return this;
118 }
119
120
121
122
123
124
125
126
127
128 public Get addColumn(byte [] family, byte [] qualifier) {
129 NavigableSet<byte []> set = familyMap.get(family);
130 if(set == null) {
131 set = new TreeSet<byte []>(Bytes.BYTES_COMPARATOR);
132 }
133 if (qualifier == null) {
134 qualifier = HConstants.EMPTY_BYTE_ARRAY;
135 }
136 set.add(qualifier);
137 familyMap.put(family, set);
138 return this;
139 }
140
141
142
143
144
145
146
147
148
149 public Get setTimeRange(long minStamp, long maxStamp)
150 throws IOException {
151 tr = new TimeRange(minStamp, maxStamp);
152 return this;
153 }
154
155
156
157
158
159
160 public Get setTimeStamp(long timestamp) {
161 try {
162 tr = new TimeRange(timestamp, timestamp+1);
163 } catch(IOException e) {
164
165 }
166 return this;
167 }
168
169
170
171
172
173 public Get setMaxVersions() {
174 this.maxVersions = Integer.MAX_VALUE;
175 return this;
176 }
177
178
179
180
181
182
183
184 public Get setMaxVersions(int maxVersions) throws IOException {
185 if(maxVersions <= 0) {
186 throw new IOException("maxVersions must be positive");
187 }
188 this.maxVersions = maxVersions;
189 return this;
190 }
191
192
193
194
195
196
197 public Get setMaxResultsPerColumnFamily(int limit) {
198 this.storeLimit = limit;
199 return this;
200 }
201
202
203
204
205
206
207
208 public Get setRowOffsetPerColumnFamily(int offset) {
209 this.storeOffset = offset;
210 return this;
211 }
212
213
214
215
216
217
218
219
220 public Get setFilter(Filter filter) {
221 this.filter = filter;
222 return this;
223 }
224
225
226
227
228
229
230 public Filter getFilter() {
231 return this.filter;
232 }
233
234
235
236
237
238
239
240
241
242
243
244 public void setCacheBlocks(boolean cacheBlocks) {
245 this.cacheBlocks = cacheBlocks;
246 }
247
248
249
250
251
252
253 public boolean getCacheBlocks() {
254 return cacheBlocks;
255 }
256
257
258
259
260
261 public byte [] getRow() {
262 return this.row;
263 }
264
265
266
267
268
269 public int getMaxVersions() {
270 return this.maxVersions;
271 }
272
273
274
275
276
277
278 public int getMaxResultsPerColumnFamily() {
279 return this.storeLimit;
280 }
281
282
283
284
285
286
287 public int getRowOffsetPerColumnFamily() {
288 return this.storeOffset;
289 }
290
291
292
293
294
295 public TimeRange getTimeRange() {
296 return this.tr;
297 }
298
299
300
301
302
303 public Set<byte[]> familySet() {
304 return this.familyMap.keySet();
305 }
306
307
308
309
310
311 public int numFamilies() {
312 return this.familyMap.size();
313 }
314
315
316
317
318
319 public boolean hasFamilies() {
320 return !this.familyMap.isEmpty();
321 }
322
323
324
325
326
327 public Map<byte[],NavigableSet<byte[]>> getFamilyMap() {
328 return this.familyMap;
329 }
330
331
332
333
334
335
336
337 @Override
338 public Map<String, Object> getFingerprint() {
339 Map<String, Object> map = new HashMap<String, Object>();
340 List<String> families = new ArrayList<String>();
341 map.put("families", families);
342 for (Map.Entry<byte [], NavigableSet<byte[]>> entry :
343 this.familyMap.entrySet()) {
344 families.add(Bytes.toStringBinary(entry.getKey()));
345 }
346 return map;
347 }
348
349
350
351
352
353
354
355
356 @Override
357 public Map<String, Object> toMap(int maxCols) {
358
359 Map<String, Object> map = getFingerprint();
360
361
362 Map<String, List<String>> columns = new HashMap<String, List<String>>();
363 map.put("families", columns);
364
365 map.put("row", Bytes.toStringBinary(this.row));
366 map.put("maxVersions", this.maxVersions);
367 map.put("cacheBlocks", this.cacheBlocks);
368 List<Long> timeRange = new ArrayList<Long>();
369 timeRange.add(this.tr.getMin());
370 timeRange.add(this.tr.getMax());
371 map.put("timeRange", timeRange);
372 int colCount = 0;
373
374 for (Map.Entry<byte [], NavigableSet<byte[]>> entry :
375 this.familyMap.entrySet()) {
376 List<String> familyList = new ArrayList<String>();
377 columns.put(Bytes.toStringBinary(entry.getKey()), familyList);
378 if(entry.getValue() == null) {
379 colCount++;
380 --maxCols;
381 familyList.add("ALL");
382 } else {
383 colCount += entry.getValue().size();
384 if (maxCols <= 0) {
385 continue;
386 }
387 for (byte [] column : entry.getValue()) {
388 if (--maxCols <= 0) {
389 continue;
390 }
391 familyList.add(Bytes.toStringBinary(column));
392 }
393 }
394 }
395 map.put("totalColumns", colCount);
396 if (this.filter != null) {
397 map.put("filter", this.filter.toString());
398 }
399
400 if (getId() != null) {
401 map.put("id", getId());
402 }
403 return map;
404 }
405
406
407 @Override
408 public int compareTo(Row other) {
409
410 return Bytes.compareTo(this.getRow(), other.getRow());
411 }
412
413 @Override
414 public int hashCode() {
415
416
417 return Bytes.hashCode(this.getRow());
418 }
419
420 @Override
421 public boolean equals(Object obj) {
422 if (this == obj) {
423 return true;
424 }
425 if (obj == null || getClass() != obj.getClass()) {
426 return false;
427 }
428 Row other = (Row) obj;
429
430 return compareTo(other) == 0;
431 }
432 }