1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.thrift;
20
21 import java.nio.ByteBuffer;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.TreeMap;
25
26 import org.apache.hadoop.hbase.HColumnDescriptor;
27 import org.apache.hadoop.hbase.KeyValue;
28 import org.apache.hadoop.hbase.client.Increment;
29 import org.apache.hadoop.hbase.client.Result;
30 import org.apache.hadoop.hbase.io.hfile.Compression;
31 import org.apache.hadoop.hbase.regionserver.StoreFile;
32 import org.apache.hadoop.hbase.regionserver.StoreFile.BloomType;
33 import org.apache.hadoop.hbase.thrift.generated.ColumnDescriptor;
34 import org.apache.hadoop.hbase.thrift.generated.IllegalArgument;
35 import org.apache.hadoop.hbase.thrift.generated.TCell;
36 import org.apache.hadoop.hbase.thrift.generated.TIncrement;
37 import org.apache.hadoop.hbase.thrift.generated.TColumn;
38 import org.apache.hadoop.hbase.thrift.generated.TRowResult;
39 import org.apache.hadoop.hbase.util.Bytes;
40
41 public class ThriftUtilities {
42
43
44
45
46
47
48
49
50
51
52 static public HColumnDescriptor colDescFromThrift(ColumnDescriptor in)
53 throws IllegalArgument {
54 Compression.Algorithm comp =
55 Compression.getCompressionAlgorithmByName(in.compression.toLowerCase());
56 StoreFile.BloomType bt =
57 BloomType.valueOf(in.bloomFilterType);
58
59 if (in.name == null || !in.name.hasRemaining()) {
60 throw new IllegalArgument("column name is empty");
61 }
62 byte [] parsedName = KeyValue.parseColumn(Bytes.getBytes(in.name))[0];
63 HColumnDescriptor col = new HColumnDescriptor(parsedName)
64 .setMaxVersions(in.maxVersions)
65 .setCompressionType(comp)
66 .setInMemory(in.inMemory)
67 .setBlockCacheEnabled(in.blockCacheEnabled)
68 .setTimeToLive(in.timeToLive)
69 .setBloomFilterType(bt);
70 return col;
71 }
72
73
74
75
76
77
78
79
80
81 static public ColumnDescriptor colDescFromHbase(HColumnDescriptor in) {
82 ColumnDescriptor col = new ColumnDescriptor();
83 col.name = ByteBuffer.wrap(Bytes.add(in.getName(), KeyValue.COLUMN_FAMILY_DELIM_ARRAY));
84 col.maxVersions = in.getMaxVersions();
85 col.compression = in.getCompression().toString();
86 col.inMemory = in.isInMemory();
87 col.blockCacheEnabled = in.isBlockCacheEnabled();
88 col.bloomFilterType = in.getBloomFilterType().toString();
89 return col;
90 }
91
92
93
94
95
96
97
98
99
100 static public List<TCell> cellFromHBase(KeyValue in) {
101 List<TCell> list = new ArrayList<TCell>(1);
102 if (in != null) {
103 list.add(new TCell(ByteBuffer.wrap(in.getValue()), in.getTimestamp()));
104 }
105 return list;
106 }
107
108
109
110
111
112
113
114 static public List<TCell> cellFromHBase(KeyValue[] in) {
115 List<TCell> list = null;
116 if (in != null) {
117 list = new ArrayList<TCell>(in.length);
118 for (int i = 0; i < in.length; i++) {
119 list.add(new TCell(ByteBuffer.wrap(in[i].getValue()), in[i].getTimestamp()));
120 }
121 } else {
122 list = new ArrayList<TCell>(0);
123 }
124 return list;
125 }
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142 static public List<TRowResult> rowResultFromHBase(Result[] in, boolean sortColumns) {
143 List<TRowResult> results = new ArrayList<TRowResult>();
144 for ( Result result_ : in) {
145 if(result_ == null || result_.isEmpty()) {
146 continue;
147 }
148 TRowResult result = new TRowResult();
149 result.row = ByteBuffer.wrap(result_.getRow());
150 if (sortColumns) {
151 result.sortedColumns = new ArrayList<TColumn>();
152 for (KeyValue kv : result_.raw()) {
153 result.sortedColumns.add(new TColumn(
154 ByteBuffer.wrap(KeyValue.makeColumn(kv.getFamily(),
155 kv.getQualifier())),
156 new TCell(ByteBuffer.wrap(kv.getValue()), kv.getTimestamp())));
157 }
158 } else {
159 result.columns = new TreeMap<ByteBuffer, TCell>();
160 for( KeyValue kv : result_.raw()) {
161 result.columns.put(
162 ByteBuffer.wrap(KeyValue.makeColumn(kv.getFamily(),
163 kv.getQualifier())),
164 new TCell(ByteBuffer.wrap(kv.getValue()), kv.getTimestamp()));
165 }
166 }
167 results.add(result);
168 }
169 return results;
170 }
171
172
173
174
175
176
177
178
179
180
181 static public List<TRowResult> rowResultFromHBase(Result[] in) {
182 return rowResultFromHBase(in, false);
183 }
184
185 static public List<TRowResult> rowResultFromHBase(Result in) {
186 Result [] result = { in };
187 return rowResultFromHBase(result);
188 }
189
190
191
192
193
194
195 public static Increment incrementFromThrift(TIncrement tincrement) {
196 Increment inc = new Increment(tincrement.getRow());
197 byte[][] famAndQf = KeyValue.parseColumn(tincrement.getColumn());
198 if (famAndQf.length <1 ) return null;
199 byte[] qual = famAndQf.length == 1 ? new byte[0]: famAndQf[1];
200 inc.addColumn(famAndQf[0], qual, tincrement.getAmmount());
201 return inc;
202 }
203 }