Review Board 1.7.22


Add support to query composite/compound keys stored in HBase

Review Request #13007 - Created July 29, 2013 and updated

Swarnim Kulkarni
HIVE-2599
Reviewers
hive
hive-git
Added support to query composite keys stored in HBase.
Added additional unit tests to prove the functionality. Also ensured all existing unit tests pass.
hbase-handler/src/java/org/apache/hadoop/hive/hbase/HBaseCompositeKey.java
New File

    
   
1
package org.apache.hadoop.hive.hbase;

    
   
2

   

    
   
3
import java.util.ArrayList;

    
   
4
import java.util.List;

    
   
5

   

    
   
6
import org.apache.hadoop.hive.serde2.lazy.ByteArrayRef;

    
   
7
import org.apache.hadoop.hive.serde2.lazy.LazyFactory;

    
   
8
import org.apache.hadoop.hive.serde2.lazy.LazyObject;

    
   
9
import org.apache.hadoop.hive.serde2.lazy.LazyStruct;

    
   
10
import org.apache.hadoop.hive.serde2.lazy.objectinspector.LazySimpleStructObjectInspector;

    
   
11
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;

    
   
12
import org.apache.hadoop.hive.serde2.objectinspector.StructField;

    
   
13

   

    
   
14
/**

    
   
15
 * HBaseCompositeKey extension of LazyStruct. All complex composite keys should extend this class

    
   
16
 * and override the {@link LazyStruct#getField(int)} method where fieldID corresponds to the ID of a

    
   
17
 * key in the composite key.

    
   
18
 * <p>

    
   
19
 * For example, for a composite key <i>"/part1/part2/part3"</i>, <i>part1</i> will have an id

    
   
20
 * <i>0</i>, <i>part2</i> will have an id <i>1</i> and <i>part3</i> will have an id <i>2</i>. Custom

    
   
21
 * implementations of getField(fieldID) should return the value corresponding to that fieldID. So,

    
   
22
 * for the above example, the value returned for <i>getField(0)</i> should be </i>part1</i>,

    
   
23
 * <i>getField(1)</i> should be <i>part2</i> and <i>getField(2)</i> should be <i>part3</i>.

    
   
24
 * </p>

    
   
25
 * */

    
   
26
public class HBaseCompositeKey extends LazyStruct {

    
   
27

   

    
   
28
  public HBaseCompositeKey(LazySimpleStructObjectInspector oi) {

    
   
29
    super(oi);

    
   
30
  }

    
   
31

   

    
   
32
  @Override

    
   
33
  public ArrayList<Object> getFieldsAsList() {

    
   
34
    ArrayList<Object> allFields = new ArrayList<Object>();

    
   
35

   

    
   
36
    List<? extends StructField> fields = oi.getAllStructFieldRefs();

    
   
37

   

    
   
38
    for (int i = 0; i < fields.size(); i++) {

    
   
39
      allFields.add(getField(i));

    
   
40
    }

    
   
41

   

    
   
42
    return allFields;

    
   
43
  }

    
   
44

   

    
   
45
  /**

    
   
46
   * Create an initialize a {@link LazyObject} with the given bytes for the given fieldID.

    
   
47
   * 

    
   
48
   * @param fieldID

    
   
49
   *          field for which the object is to be created

    
   
50
   * @param bytes

    
   
51
   *          value with which the object is to be initialized with

    
   
52
   * @return initialized {@link LazyObject}

    
   
53
   * */

    
   
54
  public LazyObject<? extends ObjectInspector> toLazyObject(int fieldID, byte[] bytes) {

    
   
55
    ObjectInspector fieldOI = oi.getAllStructFieldRefs().get(fieldID).getFieldObjectInspector();

    
   
56

   

    
   
57
    LazyObject<? extends ObjectInspector> lazyObject = LazyFactory

    
   
58
        .createLazyObject(fieldOI);

    
   
59

   

    
   
60
    ByteArrayRef ref = new ByteArrayRef();

    
   
61

   

    
   
62
    ref.setData(bytes);

    
   
63

   

    
   
64
    // initialize the lazy object

    
   
65
    lazyObject.init(ref, 0, ref.getData().length);

    
   
66

   

    
   
67
    return lazyObject;

    
   
68
  }

    
   
69
}
hbase-handler/src/java/org/apache/hadoop/hive/hbase/HBaseSerDe.java
Revision 4900a41 New Change
 
hbase-handler/src/java/org/apache/hadoop/hive/hbase/LazyHBaseRow.java
Revision b254b0d New Change
 
hbase-handler/src/test/org/apache/hadoop/hive/hbase/HBaseTestCompositeKey.java
New File
 
hbase-handler/src/test/org/apache/hadoop/hive/hbase/TestHBaseSerDe.java
Revision d25c731 New Change
 
serde/src/java/org/apache/hadoop/hive/serde2/lazy/objectinspector/LazySimpleStructObjectInspector.java
Revision 08400f1 New Change
 
  1. hbase-handler/src/java/org/apache/hadoop/hive/hbase/HBaseCompositeKey.java: Loading...
  2. hbase-handler/src/java/org/apache/hadoop/hive/hbase/HBaseSerDe.java: Loading...
  3. hbase-handler/src/java/org/apache/hadoop/hive/hbase/LazyHBaseRow.java: Loading...
  4. hbase-handler/src/test/org/apache/hadoop/hive/hbase/HBaseTestCompositeKey.java: Loading...
  5. hbase-handler/src/test/org/apache/hadoop/hive/hbase/TestHBaseSerDe.java: Loading...
  6. serde/src/java/org/apache/hadoop/hive/serde2/lazy/objectinspector/LazySimpleStructObjectInspector.java: Loading...