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 import org.apache.hadoop.hbase.classification.InterfaceAudience;
29 import org.apache.hadoop.hbase.Cell;
30 import org.apache.hadoop.hbase.DoNotRetryIOException;
31 import org.apache.hadoop.hbase.KeyValue;
32 import org.apache.hadoop.hbase.client.Get;
33 import org.apache.hadoop.hbase.client.NeedUnmanagedConnectionException;
34 import org.apache.hadoop.hbase.client.Result;
35 import org.apache.hadoop.hbase.client.Table;
36 import org.apache.hadoop.hbase.filter.Filter;
37 import org.apache.hadoop.util.StringUtils;
38
39 @InterfaceAudience.Private
40 public class RowResultGenerator extends ResultGenerator {
41 private static final Log LOG = LogFactory.getLog(RowResultGenerator.class);
42
43 private Iterator<Cell> valuesI;
44 private Cell cache;
45
46 public RowResultGenerator(final String tableName, final RowSpec rowspec,
47 final Filter filter, final boolean cacheBlocks)
48 throws IllegalArgumentException, IOException {
49 Table table = RESTServlet.getInstance().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 == 1) {
56 get.addFamily(split[0]);
57 } else if (split.length == 2) {
58 get.addColumn(split[0], split[1]);
59 } else {
60 throw new IllegalArgumentException("Invalid column specifier.");
61 }
62 }
63 }
64 get.setTimeRange(rowspec.getStartTime(), rowspec.getEndTime());
65 get.setMaxVersions(rowspec.getMaxVersions());
66 if (filter != null) {
67 get.setFilter(filter);
68 }
69 get.setCacheBlocks(cacheBlocks);
70 Result result = table.get(get);
71 if (result != null && !result.isEmpty()) {
72 valuesI = result.listCells().iterator();
73 }
74 } catch (DoNotRetryIOException | NeedUnmanagedConnectionException e) {
75
76
77
78
79
80
81 LOG.warn(StringUtils.stringifyException(e));
82 } finally {
83 table.close();
84 }
85 }
86
87 public void close() {
88 }
89
90 public boolean hasNext() {
91 if (cache != null) {
92 return true;
93 }
94 if (valuesI == null) {
95 return false;
96 }
97 return valuesI.hasNext();
98 }
99
100 public Cell next() {
101 if (cache != null) {
102 Cell kv = cache;
103 cache = null;
104 return kv;
105 }
106 if (valuesI == null) {
107 return null;
108 }
109 try {
110 return valuesI.next();
111 } catch (NoSuchElementException e) {
112 return null;
113 }
114 }
115
116 public void putBack(Cell kv) {
117 this.cache = kv;
118 }
119
120 public void remove() {
121 throw new UnsupportedOperationException("remove not supported");
122 }
123 }