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