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.util.ArrayList;
22 import java.util.List;
23 import java.util.TreeMap;
24
25 import org.apache.hadoop.hbase.HColumnDescriptor;
26 import org.apache.hadoop.hbase.KeyValue;
27 import org.apache.hadoop.hbase.client.Result;
28 import org.apache.hadoop.hbase.io.hfile.Compression;
29 import org.apache.hadoop.hbase.regionserver.StoreFile;
30 import org.apache.hadoop.hbase.regionserver.StoreFile.BloomType;
31 import org.apache.hadoop.hbase.thrift.generated.ColumnDescriptor;
32 import org.apache.hadoop.hbase.thrift.generated.IllegalArgument;
33 import org.apache.hadoop.hbase.thrift.generated.TCell;
34 import org.apache.hadoop.hbase.thrift.generated.TRowResult;
35 import org.apache.hadoop.hbase.util.Bytes;
36
37 public class ThriftUtilities {
38
39
40
41
42
43
44
45
46
47
48 static public HColumnDescriptor colDescFromThrift(ColumnDescriptor in)
49 throws IllegalArgument {
50 Compression.Algorithm comp =
51 Compression.getCompressionAlgorithmByName(in.compression.toLowerCase());
52 StoreFile.BloomType bt =
53 BloomType.valueOf(in.bloomFilterType);
54
55 if (in.name == null || in.name.length <= 0) {
56 throw new IllegalArgument("column name is empty");
57 }
58 byte [] parsedName = KeyValue.parseColumn(in.name)[0];
59 HColumnDescriptor col = new HColumnDescriptor(parsedName,
60 in.maxVersions, comp.getName(), in.inMemory, in.blockCacheEnabled,
61 in.timeToLive, bt.toString());
62 return col;
63 }
64
65
66
67
68
69
70
71
72
73 static public ColumnDescriptor colDescFromHbase(HColumnDescriptor in) {
74 ColumnDescriptor col = new ColumnDescriptor();
75 col.name = Bytes.add(in.getName(), KeyValue.COLUMN_FAMILY_DELIM_ARRAY);
76 col.maxVersions = in.getMaxVersions();
77 col.compression = in.getCompression().toString();
78 col.inMemory = in.isInMemory();
79 col.blockCacheEnabled = in.isBlockCacheEnabled();
80 col.bloomFilterType = in.getBloomFilterType().toString();
81 return col;
82 }
83
84
85
86
87
88
89
90
91
92 static public List<TCell> cellFromHBase(KeyValue in) {
93 List<TCell> list = new ArrayList<TCell>(1);
94 if (in != null) {
95 list.add(new TCell(in.getValue(), in.getTimestamp()));
96 }
97 return list;
98 }
99
100
101
102
103
104
105
106 static public List<TCell> cellFromHBase(KeyValue[] in) {
107 List<TCell> list = null;
108 if (in != null) {
109 list = new ArrayList<TCell>(in.length);
110 for (int i = 0; i < in.length; i++) {
111 list.add(new TCell(in[i].getValue(), in[i].getTimestamp()));
112 }
113 } else {
114 list = new ArrayList<TCell>(0);
115 }
116 return list;
117 }
118
119
120
121
122
123
124
125
126
127
128 static public List<TRowResult> rowResultFromHBase(Result[] in) {
129 List<TRowResult> results = new ArrayList<TRowResult>();
130 for ( Result result_ : in) {
131 if(result_ == null || result_.isEmpty()) {
132 continue;
133 }
134 TRowResult result = new TRowResult();
135 result.row = result_.getRow();
136 result.columns = new TreeMap<byte[], TCell>(Bytes.BYTES_COMPARATOR);
137 for(KeyValue kv : result_.sorted()) {
138 result.columns.put(KeyValue.makeColumn(kv.getFamily(),
139 kv.getQualifier()), new TCell(kv.getValue(), kv.getTimestamp()));
140 }
141 results.add(result);
142 }
143 return results;
144 }
145
146 static public List<TRowResult> rowResultFromHBase(Result in) {
147 Result [] result = { in };
148 return rowResultFromHBase(result);
149 }
150 }