1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.hadoop.hbase.rest;
22
23 import java.io.IOException;
24 import java.util.Iterator;
25 import java.util.NoSuchElementException;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 import org.apache.hadoop.util.StringUtils;
31 import org.apache.hadoop.hbase.DoNotRetryIOException;
32 import org.apache.hadoop.hbase.KeyValue;
33 import org.apache.hadoop.hbase.client.Get;
34 import org.apache.hadoop.hbase.client.HTableInterface;
35 import org.apache.hadoop.hbase.client.HTablePool;
36 import org.apache.hadoop.hbase.client.Result;
37 import org.apache.hadoop.hbase.filter.Filter;
38
39 public class RowResultGenerator extends ResultGenerator {
40 private static final Log LOG = LogFactory.getLog(RowResultGenerator.class);
41
42 private Iterator<KeyValue> valuesI;
43 private KeyValue cache;
44
45 public RowResultGenerator(final String tableName, final RowSpec rowspec,
46 final Filter filter) throws IllegalArgumentException, IOException {
47 HTablePool pool = RESTServlet.getInstance().getTablePool();
48 HTableInterface table = pool.getTable(tableName);
49 try {
50 Get get = new Get(rowspec.getRow());
51 if (rowspec.hasColumns()) {
52 for (byte[] col: rowspec.getColumns()) {
53 byte[][] split = KeyValue.parseColumn(col);
54 if (split.length == 2 && split[1].length != 0) {
55 get.addColumn(split[0], split[1]);
56 } else {
57 get.addFamily(split[0]);
58 }
59 }
60 }
61 get.setTimeRange(rowspec.getStartTime(), rowspec.getEndTime());
62 get.setMaxVersions(rowspec.getMaxVersions());
63 if (filter != null) {
64 get.setFilter(filter);
65 }
66 Result result = table.get(get);
67 if (result != null && !result.isEmpty()) {
68 valuesI = result.list().iterator();
69 }
70 } catch (DoNotRetryIOException e) {
71
72
73
74
75
76
77 LOG.warn(StringUtils.stringifyException(e));
78 } finally {
79 table.close();
80 }
81 }
82
83 public void close() {
84 }
85
86 public boolean hasNext() {
87 if (cache != null) {
88 return true;
89 }
90 if (valuesI == null) {
91 return false;
92 }
93 return valuesI.hasNext();
94 }
95
96 public KeyValue next() {
97 if (cache != null) {
98 KeyValue kv = cache;
99 cache = null;
100 return kv;
101 }
102 if (valuesI == null) {
103 return null;
104 }
105 try {
106 return valuesI.next();
107 } catch (NoSuchElementException e) {
108 return null;
109 }
110 }
111
112 public void putBack(KeyValue kv) {
113 this.cache = kv;
114 }
115
116 public void remove() {
117 throw new UnsupportedOperationException("remove not supported");
118 }
119 }