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