View Javadoc

1   /**
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.thrift2;
21  
22  import org.apache.hadoop.hbase.HConstants;
23  import org.apache.hadoop.hbase.KeyValue;
24  import org.apache.hadoop.hbase.client.*;
25  import org.apache.hadoop.hbase.thrift2.generated.*;
26  
27  import java.io.IOException;
28  import java.nio.ByteBuffer;
29  import java.util.*;
30  
31  public class ThriftUtilities {
32  
33    private ThriftUtilities() {
34      throw new UnsupportedOperationException("Can't initialize class");
35    }
36  
37    /**
38     * Creates a {@link Get} (HBase) from a {@link TGet} (Thrift).
39     * 
40     * This ignores any timestamps set on {@link TColumn} objects.
41     * 
42     * @param in the <code>TGet</code> to convert
43     * 
44     * @return <code>Get</code> object
45     * 
46     * @throws IOException if an invalid time range or max version parameter is given
47     */
48    public static Get getFromThrift(TGet in) throws IOException {
49      Get out = new Get(in.getRow());
50  
51      // Timestamp overwrites time range if both are set
52      if (in.isSetTimestamp()) {
53        out.setTimeStamp(in.getTimestamp());
54      } else if (in.isSetTimeRange()) {
55        out.setTimeRange(in.getTimeRange().getMinStamp(), in.getTimeRange().getMaxStamp());
56      }
57  
58      if (in.isSetMaxVersions()) {
59        out.setMaxVersions(in.getMaxVersions());
60      }
61  
62      if (!in.isSetColumns()) {
63        return out;
64      }
65  
66      for (TColumn column : in.getColumns()) {
67        if (column.isSetQualifier()) {
68          out.addColumn(column.getFamily(), column.getQualifier());
69        } else {
70          out.addFamily(column.getFamily());
71        }
72      }
73  
74      return out;
75    }
76  
77    /**
78     * Converts multiple {@link TGet}s (Thrift) into a list of {@link Get}s (HBase).
79     * 
80     * @param in list of <code>TGet</code>s to convert
81     * 
82     * @return list of <code>Get</code> objects
83     * 
84     * @throws IOException if an invalid time range or max version parameter is given
85     * @see #getFromThrift(TGet)
86     */
87    public static List<Get> getsFromThrift(List<TGet> in) throws IOException {
88      List<Get> out = new ArrayList<Get>(in.size());
89      for (TGet get : in) {
90        out.add(getFromThrift(get));
91      }
92      return out;
93    }
94  
95    /**
96     * Creates a {@link TResult} (Thrift) from a {@link Result} (HBase).
97     * 
98     * @param in the <code>Result</code> to convert
99     * 
100    * @return converted result, returns an empty result if the input is <code>null</code>
101    */
102   public static TResult resultFromHBase(Result in) {
103     KeyValue[] raw = in.raw();
104     TResult out = new TResult();
105     byte[] row = in.getRow();
106     if (row != null) {
107       out.setRow(in.getRow());
108     }
109     List<TColumnValue> columnValues = new ArrayList<TColumnValue>();
110     for (KeyValue kv : raw) {
111       TColumnValue col = new TColumnValue();
112       col.setFamily(kv.getFamily());
113       col.setQualifier(kv.getQualifier());
114       col.setTimestamp(kv.getTimestamp());
115       col.setValue(kv.getValue());
116       columnValues.add(col);
117     }
118     out.setColumnValues(columnValues);
119     return out;
120   }
121 
122   /**
123    * Converts multiple {@link Result}s (HBase) into a list of {@link TResult}s (Thrift).
124    * 
125    * @param in array of <code>Result</code>s to convert
126    * 
127    * @return list of converted <code>TResult</code>s
128    * 
129    * @see #resultFromHBase(Result)
130    */
131   public static List<TResult> resultsFromHBase(Result[] in) {
132     List<TResult> out = new ArrayList<TResult>(in.length);
133     for (Result result : in) {
134       out.add(resultFromHBase(result));
135     }
136     return out;
137   }
138 
139   /**
140    * Creates a {@link Put} (HBase) from a {@link TPut} (Thrift)
141    * 
142    * @param in the <code>TPut</code> to convert
143    * 
144    * @return converted <code>Put</code>
145    */
146   public static Put putFromThrift(TPut in) {
147     Put out;
148 
149     if (in.isSetTimestamp()) {
150       out = new Put(in.getRow(), in.getTimestamp(), null);
151     } else {
152       out = new Put(in.getRow());
153     }
154 
155     out.setWriteToWAL(in.isWriteToWal());
156 
157     for (TColumnValue columnValue : in.getColumnValues()) {
158       if (columnValue.isSetTimestamp()) {
159         out.add(columnValue.getFamily(), columnValue.getQualifier(), columnValue.getTimestamp(),
160             columnValue.getValue());
161       } else {
162         out.add(columnValue.getFamily(), columnValue.getQualifier(), columnValue.getValue());
163       }
164     }
165 
166     return out;
167   }
168 
169   /**
170    * Converts multiple {@link TPut}s (Thrift) into a list of {@link Put}s (HBase).
171    * 
172    * @param in list of <code>TPut</code>s to convert
173    * 
174    * @return list of converted <code>Put</code>s
175    * 
176    * @see #putFromThrift(TPut)
177    */
178   public static List<Put> putsFromThrift(List<TPut> in) {
179     List<Put> out = new ArrayList<Put>(in.size());
180     for (TPut put : in) {
181       out.add(putFromThrift(put));
182     }
183     return out;
184   }
185 
186   /**
187    * Creates a {@link Delete} (HBase) from a {@link TDelete} (Thrift).
188    * 
189    * @param in the <code>TDelete</code> to convert
190    * 
191    * @return converted <code>Delete</code>
192    */
193   public static Delete deleteFromThrift(TDelete in) {
194     Delete out;
195 
196     if (in.isSetColumns()) {
197       out = new Delete(in.getRow());
198       for (TColumn column : in.getColumns()) {
199         if (column.isSetQualifier()) {
200           if (column.isSetTimestamp()) {
201             if (in.isSetDeleteType() &&
202                 in.getDeleteType().equals(TDeleteType.DELETE_COLUMNS))
203               out.deleteColumns(column.getFamily(), column.getQualifier(), column.getTimestamp());
204             else
205               out.deleteColumn(column.getFamily(), column.getQualifier(), column.getTimestamp());
206           } else {
207             if (in.isSetDeleteType() &&
208                 in.getDeleteType().equals(TDeleteType.DELETE_COLUMNS))
209               out.deleteColumns(column.getFamily(), column.getQualifier());
210             else
211               out.deleteColumn(column.getFamily(), column.getQualifier());
212           }
213 
214         } else {
215           if (column.isSetTimestamp()) {
216             out.deleteFamily(column.getFamily(), column.getTimestamp());
217           } else {
218             out.deleteFamily(column.getFamily());
219           }
220         }
221       }
222     } else {
223       if (in.isSetTimestamp()) {
224         out = new Delete(in.getRow(), in.getTimestamp(), null);
225       } else {
226         out = new Delete(in.getRow());
227       }
228     }
229     out.setWriteToWAL(in.isWriteToWal());
230     return out;
231   }
232 
233   /**
234    * Converts multiple {@link TDelete}s (Thrift) into a list of {@link Delete}s (HBase).
235    * 
236    * @param in list of <code>TDelete</code>s to convert
237    * 
238    * @return list of converted <code>Delete</code>s
239    * 
240    * @see #deleteFromThrift(TDelete)
241    */
242 
243   public static List<Delete> deletesFromThrift(List<TDelete> in) {
244     List<Delete> out = new ArrayList<Delete>(in.size());
245     for (TDelete delete : in) {
246       out.add(deleteFromThrift(delete));
247     }
248     return out;
249   }
250 
251   public static TDelete deleteFromHBase(Delete in) {
252     TDelete out = new TDelete(ByteBuffer.wrap(in.getRow()));
253 
254     List<TColumn> columns = new ArrayList<TColumn>();
255     long rowTimestamp = in.getTimeStamp();
256     if (rowTimestamp != HConstants.LATEST_TIMESTAMP) {
257       out.setTimestamp(rowTimestamp);
258     }
259 
260     // Map<family, List<KeyValue>>
261     for (Map.Entry<byte[], List<KeyValue>> familyEntry : in.getFamilyMap().entrySet()) {
262       TColumn column = new TColumn(ByteBuffer.wrap(familyEntry.getKey()));
263       for (KeyValue keyValue : familyEntry.getValue()) {
264         byte[] family = keyValue.getFamily();
265         byte[] qualifier = keyValue.getQualifier();
266         long timestamp = keyValue.getTimestamp();
267         if (family != null) {
268           column.setFamily(family);
269         }
270         if (qualifier != null) {
271           column.setQualifier(qualifier);
272         }
273         if (timestamp != HConstants.LATEST_TIMESTAMP) {
274           column.setTimestamp(keyValue.getTimestamp());
275         }
276       }
277       columns.add(column);
278     }
279     out.setColumns(columns);
280 
281     return out;
282   }
283 
284   public static List<TDelete> deletesFromHBase(List<Delete> in) {
285     List<TDelete> out = new ArrayList<TDelete>(in.size());
286     for (Delete delete : in) {
287       if (delete == null) {
288         out.add(null);
289       } else {
290         out.add(deleteFromHBase(delete));
291       }
292     }
293     return out;
294   }
295 
296   public static Scan scanFromThrift(TScan in) throws IOException {
297     Scan out = new Scan();
298 
299     if (in.isSetStartRow())
300       out.setStartRow(in.getStartRow());
301     if (in.isSetStopRow())
302       out.setStopRow(in.getStopRow());
303     if (in.isSetCaching())
304       out.setCaching(in.getCaching());
305     if (in.isSetMaxVersions()) {
306       out.setMaxVersions(in.getMaxVersions());
307     }
308 
309     if (in.isSetColumns()) {
310       for (TColumn column : in.getColumns()) {
311         if (column.isSetQualifier()) {
312           out.addColumn(column.getFamily(), column.getQualifier());
313         } else {
314           out.addFamily(column.getFamily());
315         }
316       }
317     }
318 
319     TTimeRange timeRange = in.getTimeRange();
320     if (timeRange != null &&
321         timeRange.isSetMinStamp() && timeRange.isSetMaxStamp()) {
322       out.setTimeRange(timeRange.getMinStamp(), timeRange.getMaxStamp());
323     }
324 
325     return out;
326   }
327 
328   public static Increment incrementFromThrift(TIncrement in) throws IOException {
329     Increment out = new Increment(in.getRow());
330     for (TColumnIncrement column : in.getColumns()) {
331       out.addColumn(column.getFamily(), column.getQualifier(), column.getAmount());
332     }
333     out.setWriteToWAL(in.isWriteToWal());
334     return out;
335   }
336 }