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