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