1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.rest;
20
21 import java.io.IOException;
22 import java.util.Iterator;
23 import java.util.NoSuchElementException;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.hbase.Cell;
28 import org.apache.hadoop.hbase.DoNotRetryIOException;
29 import org.apache.hadoop.hbase.KeyValue;
30 import org.apache.hadoop.hbase.classification.InterfaceAudience;
31 import org.apache.hadoop.hbase.client.Get;
32 import org.apache.hadoop.hbase.client.NeedUnmanagedConnectionException;
33 import org.apache.hadoop.hbase.client.Result;
34 import org.apache.hadoop.hbase.client.Table;
35 import org.apache.hadoop.hbase.filter.Filter;
36 import org.apache.hadoop.util.StringUtils;
37
38 @InterfaceAudience.Private
39 public class RowResultGenerator extends ResultGenerator {
40 private static final Log LOG = LogFactory.getLog(RowResultGenerator.class);
41
42 private Iterator<Cell> valuesI;
43 private Cell cache;
44
45 public RowResultGenerator(final String tableName, final RowSpec rowspec,
46 final Filter filter, final boolean cacheBlocks)
47 throws IllegalArgumentException, IOException {
48 try (Table table = RESTServlet.getInstance().getTable(tableName)) {
49 Get get = new Get(rowspec.getRow());
50 if (rowspec.hasColumns()) {
51 for (byte[] col : rowspec.getColumns()) {
52 byte[][] split = KeyValue.parseColumn(col);
53 if (split.length == 1) {
54 get.addFamily(split[0]);
55 } else if (split.length == 2) {
56 get.addColumn(split[0], split[1]);
57 } else {
58 throw new IllegalArgumentException("Invalid column specifier.");
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 get.setCacheBlocks(cacheBlocks);
68 Result result = table.get(get);
69 if (result != null && !result.isEmpty()) {
70 valuesI = result.listCells().iterator();
71 }
72 } catch (DoNotRetryIOException | NeedUnmanagedConnectionException e) {
73
74
75
76
77
78
79 LOG.warn(StringUtils.stringifyException(e));
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 Cell next() {
97 if (cache != null) {
98 Cell 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(Cell kv) {
113 this.cache = kv;
114 }
115
116 public void remove() {
117 throw new UnsupportedOperationException("remove not supported");
118 }
119 }