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.TRowResult;
38 import org.apache.hadoop.hbase.util.Bytes;
39
40 @InterfaceAudience.Private
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 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 static public List<TRowResult> rowResultFromHBase(Result[] in) {
137 List<TRowResult> results = new ArrayList<TRowResult>();
138 for ( Result result_ : in) {
139 if(result_ == null || result_.isEmpty()) {
140 continue;
141 }
142 TRowResult result = new TRowResult();
143 result.row = ByteBuffer.wrap(result_.getRow());
144 result.columns = new TreeMap<ByteBuffer, TCell>();
145 for(KeyValue kv : result_.raw()) {
146 result.columns.put(
147 ByteBuffer.wrap(KeyValue.makeColumn(kv.getFamily(),
148 kv.getQualifier())),
149 new TCell(ByteBuffer.wrap(kv.getValue()), kv.getTimestamp()));
150 }
151 results.add(result);
152 }
153 return results;
154 }
155
156 static public List<TRowResult> rowResultFromHBase(Result in) {
157 Result [] result = { in };
158 return rowResultFromHBase(result);
159 }
160
161
162
163
164
165
166 public static Increment incrementFromThrift(TIncrement tincrement) {
167 Increment inc = new Increment(tincrement.getRow());
168 byte[][] famAndQf = KeyValue.parseColumn(tincrement.getColumn());
169 if (famAndQf.length <1 ) return null;
170 byte[] qual = famAndQf.length == 1 ? new byte[0]: famAndQf[1];
171 inc.addColumn(famAndQf[0], qual, tincrement.getAmmount());
172 return inc;
173 }
174 }