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.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.TColumn;
38  import org.apache.hadoop.hbase.thrift.generated.TRowResult;
39  import org.apache.hadoop.hbase.util.Bytes;
40  
41  @InterfaceAudience.Private
42  public class ThriftUtilities {
43  
44    /**
45     * This utility method creates a new Hbase HColumnDescriptor object based on a
46     * Thrift ColumnDescriptor "struct".
47     *
48     * @param in
49     *          Thrift ColumnDescriptor object
50     * @return HColumnDescriptor
51     * @throws IllegalArgument
52     */
53    static public HColumnDescriptor colDescFromThrift(ColumnDescriptor in)
54        throws IllegalArgument {
55      Compression.Algorithm comp =
56        Compression.getCompressionAlgorithmByName(in.compression.toLowerCase());
57      BloomType bt =
58        BloomType.valueOf(in.bloomFilterType);
59  
60      if (in.name == null || !in.name.hasRemaining()) {
61        throw new IllegalArgument("column name is empty");
62      }
63      byte [] parsedName = KeyValue.parseColumn(Bytes.getBytes(in.name))[0];
64      HColumnDescriptor col = new HColumnDescriptor(parsedName)
65          .setMaxVersions(in.maxVersions)
66          .setCompressionType(comp)
67          .setInMemory(in.inMemory)
68          .setBlockCacheEnabled(in.blockCacheEnabled)
69          .setTimeToLive(in.timeToLive)
70          .setBloomFilterType(bt);
71      return col;
72    }
73  
74    /**
75     * This utility method creates a new Thrift ColumnDescriptor "struct" based on
76     * an Hbase HColumnDescriptor object.
77     *
78     * @param in
79     *          Hbase HColumnDescriptor object
80     * @return Thrift ColumnDescriptor
81     */
82    static public ColumnDescriptor colDescFromHbase(HColumnDescriptor in) {
83      ColumnDescriptor col = new ColumnDescriptor();
84      col.name = ByteBuffer.wrap(Bytes.add(in.getName(), KeyValue.COLUMN_FAMILY_DELIM_ARRAY));
85      col.maxVersions = in.getMaxVersions();
86      col.compression = in.getCompression().toString();
87      col.inMemory = in.isInMemory();
88      col.blockCacheEnabled = in.isBlockCacheEnabled();
89      col.bloomFilterType = in.getBloomFilterType().toString();
90      return col;
91    }
92  
93    /**
94     * This utility method creates a list of Thrift TCell "struct" based on
95     * an Hbase Cell object. The empty list is returned if the input is null.
96     *
97     * @param in
98     *          Hbase Cell object
99     * @return Thrift TCell array
100    */
101   static public List<TCell> cellFromHBase(KeyValue in) {
102     List<TCell> list = new ArrayList<TCell>(1);
103     if (in != null) {
104       list.add(new TCell(ByteBuffer.wrap(in.getValue()), in.getTimestamp()));
105     }
106     return list;
107   }
108 
109   /**
110    * This utility method creates a list of Thrift TCell "struct" based on
111    * an Hbase Cell array. The empty list is returned if the input is null.
112    * @param in Hbase Cell array
113    * @return Thrift TCell array
114    */
115   static public List<TCell> cellFromHBase(KeyValue[] in) {
116     List<TCell> list = null;
117     if (in != null) {
118       list = new ArrayList<TCell>(in.length);
119       for (int i = 0; i < in.length; i++) {
120         list.add(new TCell(ByteBuffer.wrap(in[i].getValue()), in[i].getTimestamp()));
121       }
122     } else {
123       list = new ArrayList<TCell>(0);
124     }
125     return list;
126   }
127 
128   /**
129    * This utility method creates a list of Thrift TRowResult "struct" based on
130    * an Hbase RowResult object. The empty list is returned if the input is
131    * null.
132    *
133    * @param in
134    *          Hbase RowResult object
135    * @param sortColumns
136    *          This boolean dictates if row data is returned in a sorted order
137    *          sortColumns = True will set TRowResult's sortedColumns member
138    *                        which is an ArrayList of TColumn struct
139    *          sortColumns = False will set TRowResult's columns member which is
140    *                        a map of columnName and TCell struct
141    * @return Thrift TRowResult array
142    */
143   static public List<TRowResult> rowResultFromHBase(Result[] in, boolean sortColumns) {
144     List<TRowResult> results = new ArrayList<TRowResult>();
145     for ( Result result_ : in) {
146         if(result_ == null || result_.isEmpty()) {
147             continue;
148         }
149         TRowResult result = new TRowResult();
150         result.row = ByteBuffer.wrap(result_.getRow());
151         if (sortColumns) {
152           result.sortedColumns = new ArrayList<TColumn>();
153           for (KeyValue kv : result_.raw()) {
154             result.sortedColumns.add(new TColumn(
155                 ByteBuffer.wrap(KeyValue.makeColumn(kv.getFamily(),
156                     kv.getQualifier())),
157                 new TCell(ByteBuffer.wrap(kv.getValue()), kv.getTimestamp())));
158           }
159         } else {
160           result.columns = new TreeMap<ByteBuffer, TCell>();
161           for (KeyValue kv : result_.raw()) {
162             result.columns.put(
163                 ByteBuffer.wrap(KeyValue.makeColumn(kv.getFamily(),
164                     kv.getQualifier())),
165                 new TCell(ByteBuffer.wrap(kv.getValue()), kv.getTimestamp()));
166           }
167         }
168       results.add(result);
169     }
170     return results;
171   }
172 
173   /**
174    * This utility method creates a list of Thrift TRowResult "struct" based on
175    * an array of Hbase RowResult objects. The empty list is returned if the input is
176    * null.
177    *
178    * @param in
179    *          Array of Hbase RowResult objects
180    * @return Thrift TRowResult array
181    */
182   static public List<TRowResult> rowResultFromHBase(Result[] in) {
183     return rowResultFromHBase(in, false);
184   }
185 
186   static public List<TRowResult> rowResultFromHBase(Result in) {
187     Result [] result = { in };
188     return rowResultFromHBase(result);
189   }
190 
191   /**
192    * From a {@link TIncrement} create an {@link Increment}.
193    * @param tincrement the Thrift version of an increment
194    * @return an increment that the {@link TIncrement} represented.
195    */
196   public static Increment incrementFromThrift(TIncrement tincrement) {
197     Increment inc = new Increment(tincrement.getRow());
198     byte[][] famAndQf = KeyValue.parseColumn(tincrement.getColumn());
199     if (famAndQf.length <1 ) return null;
200     byte[] qual = famAndQf.length == 1 ? new byte[0]: famAndQf[1];
201     inc.addColumn(famAndQf[0], qual, tincrement.getAmmount());
202     return inc;
203   }
204 }