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