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