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