View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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     * This utility method creates a new Hbase HColumnDescriptor object based on a
41     * Thrift ColumnDescriptor "struct".
42     *
43     * @param in
44     *          Thrift ColumnDescriptor object
45     * @return HColumnDescriptor
46     * @throws IllegalArgument
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     * This utility method creates a new Thrift ColumnDescriptor "struct" based on
67     * an Hbase HColumnDescriptor object.
68     *
69     * @param in
70     *          Hbase HColumnDescriptor object
71     * @return Thrift ColumnDescriptor
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     * This utility method creates a list of Thrift TCell "struct" based on
86     * an Hbase Cell object. The empty list is returned if the input is null.
87     *
88     * @param in
89     *          Hbase Cell object
90     * @return Thrift TCell array
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    * This utility method creates a list of Thrift TCell "struct" based on
102    * an Hbase Cell array. The empty list is returned if the input is null.
103    * @param in Hbase Cell array
104    * @return Thrift TCell array
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    * This utility method creates a list of Thrift TRowResult "struct" based on
121    * an Hbase RowResult object. The empty list is returned if the input is
122    * null.
123    *
124    * @param in
125    *          Hbase RowResult object
126    * @return Thrift TRowResult array
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 }