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.HColumnDescriptor;
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 import org.apache.hadoop.hbase.regionserver.NoSuchColumnFamilyException;
39
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 } else {
62
63 for (HColumnDescriptor family:
64 table.getTableDescriptor().getFamilies()) {
65 get.addFamily(family.getName());
66 }
67 }
68 get.setTimeRange(rowspec.getStartTime(), rowspec.getEndTime());
69 get.setMaxVersions(rowspec.getMaxVersions());
70 if (filter != null) {
71 get.setFilter(filter);
72 }
73 Result result = table.get(get);
74 if (result != null && !result.isEmpty()) {
75 valuesI = result.list().iterator();
76 }
77 } catch (NoSuchColumnFamilyException e) {
78
79
80
81
82
83
84 LOG.warn(StringUtils.stringifyException(e));
85 } finally {
86 pool.putTable(table);
87 }
88 }
89
90 public void close() {
91 }
92
93 public boolean hasNext() {
94 if (cache != null) {
95 return true;
96 }
97 if (valuesI == null) {
98 return false;
99 }
100 return valuesI.hasNext();
101 }
102
103 public KeyValue next() {
104 if (cache != null) {
105 KeyValue kv = cache;
106 cache = null;
107 return kv;
108 }
109 if (valuesI == null) {
110 return null;
111 }
112 try {
113 return valuesI.next();
114 } catch (NoSuchElementException e) {
115 return null;
116 }
117 }
118
119 public void putBack(KeyValue kv) {
120 this.cache = kv;
121 }
122
123 public void remove() {
124 throw new UnsupportedOperationException("remove not supported");
125 }
126 }