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