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.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     * This utility method creates a new Hbase HColumnDescriptor object based on a
44     * Thrift ColumnDescriptor "struct".
45     *
46     * @param in
47     *          Thrift ColumnDescriptor object
48     * @return HColumnDescriptor
49     * @throws IllegalArgument
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     * This utility method creates a new Thrift ColumnDescriptor "struct" based on
74     * an Hbase HColumnDescriptor object.
75     *
76     * @param in
77     *          Hbase HColumnDescriptor object
78     * @return Thrift ColumnDescriptor
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     * This utility method creates a list of Thrift TCell "struct" based on
93     * an Hbase Cell object. The empty list is returned if the input is null.
94     *
95     * @param in
96     *          Hbase Cell object
97     * @return Thrift TCell array
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    * This utility method creates a list of Thrift TCell "struct" based on
109    * an Hbase Cell array. The empty list is returned if the input is null.
110    * @param in Hbase Cell array
111    * @return Thrift TCell array
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    * This utility method creates a list of Thrift TRowResult "struct" based on
128    * an Hbase RowResult object. The empty list is returned if the input is
129    * null.
130    *
131    * @param in
132    *          Hbase RowResult object
133    * @return Thrift TRowResult array
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    * From a {@link TIncrement} create an {@link Increment}.
162    * @param tincrement the Thrift version of an increment
163    * @return an increment that the {@link TIncrement} represented.
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 }