PageRenderTime 32ms CodeModel.GetById 10ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/hbase-handler/src/java/org/apache/hadoop/hive/hbase/LazyHBaseRow.java

#
Java | 194 lines | 114 code | 19 blank | 61 comment | 24 complexity | 84c46331fc2184315abef12777d43644 MD5 | raw file
  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
 19package org.apache.hadoop.hive.hbase;
 20
 21import java.util.ArrayList;
 22import java.util.Arrays;
 23import java.util.List;
 24
 25import org.apache.hadoop.hbase.client.Result;
 26import org.apache.hadoop.hive.serde2.lazy.ByteArrayRef;
 27import org.apache.hadoop.hive.serde2.lazy.LazyFactory;
 28import org.apache.hadoop.hive.serde2.lazy.LazyObject;
 29import org.apache.hadoop.hive.serde2.lazy.LazyStruct;
 30import org.apache.hadoop.hive.serde2.lazy.objectinspector.LazyMapObjectInspector;
 31import org.apache.hadoop.hive.serde2.lazy.objectinspector.LazySimpleStructObjectInspector;
 32import org.apache.hadoop.hive.serde2.objectinspector.StructField;
 33import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
 34
 35/**
 36 * LazyObject for storing an HBase row.  The field of an HBase row can be
 37 * primitive or non-primitive.
 38 */
 39public class LazyHBaseRow extends LazyStruct {
 40
 41  /**
 42   * The HBase columns mapping of the row.
 43   */
 44  private Result result;
 45  private List<String> hbaseColumnFamilies;
 46  private List<byte []> hbaseColumnFamiliesBytes;
 47  private List<String> hbaseColumnQualifiers;
 48  private List<byte []> hbaseColumnQualifiersBytes;
 49  private ArrayList<Object> cachedList;
 50
 51  /**
 52   * Construct a LazyHBaseRow object with the ObjectInspector.
 53   */
 54  public LazyHBaseRow(LazySimpleStructObjectInspector oi) {
 55    super(oi);
 56  }
 57
 58  /**
 59   * Set the HBase row data(a Result writable) for this LazyStruct.
 60   * @see LazyHBaseRow#init(Result)
 61   */
 62  public void init(
 63      Result r,
 64      List<String> hbaseColumnFamilies,
 65      List<byte []> hbaseColumnFamiliesBytes,
 66      List<String> hbaseColumnQualifiers,
 67      List<byte []> hbaseColumnQualifiersBytes) {
 68
 69    result = r;
 70    this.hbaseColumnFamilies = hbaseColumnFamilies;
 71    this.hbaseColumnFamiliesBytes = hbaseColumnFamiliesBytes;
 72    this.hbaseColumnQualifiers = hbaseColumnQualifiers;
 73    this.hbaseColumnQualifiersBytes = hbaseColumnQualifiersBytes;
 74    setParsed(false);
 75  }
 76
 77  /**
 78   * Parse the Result and fill each field.
 79   * @see LazyStruct#parse()
 80   */
 81  private void parse() {
 82    if (getFields() == null) {
 83      List<? extends StructField> fieldRefs =
 84        ((StructObjectInspector)getInspector()).getAllStructFieldRefs();
 85      setFields(new LazyObject[fieldRefs.size()]);
 86      for (int i = 0; i < getFields().length; i++) {
 87        String hbaseColumnFamily = hbaseColumnFamilies.get(i);
 88        String hbaseColumnQualifier = hbaseColumnQualifiers.get(i);
 89
 90        if (hbaseColumnQualifier == null && !HBaseSerDe.isSpecialColumn(hbaseColumnFamily)) {
 91          // a column family
 92          getFields()[i] = new LazyHBaseCellMap(
 93              (LazyMapObjectInspector) fieldRefs.get(i).getFieldObjectInspector());
 94          continue;
 95        }
 96
 97        getFields()[i] = LazyFactory.createLazyObject(fieldRefs.get(i).getFieldObjectInspector());
 98      }
 99      setFieldInited(new boolean[getFields().length]);
100    }
101    Arrays.fill(getFieldInited(), false);
102    setParsed(true);
103  }
104
105  /**
106   * Get one field out of the HBase row.
107   *
108   * If the field is a primitive field, return the actual object.
109   * Otherwise return the LazyObject.  This is because PrimitiveObjectInspector
110   * does not have control over the object used by the user - the user simply
111   * directly uses the Object instead of going through
112   * Object PrimitiveObjectInspector.get(Object).
113   *
114   * @param fieldID  The field ID
115   * @return         The field as a LazyObject
116   */
117  @Override
118  public Object getField(int fieldID) {
119    if (!getParsed()) {
120      parse();
121    }
122    return uncheckedGetField(fieldID);
123  }
124
125  /**
126   * Get the field out of the row without checking whether parsing is needed.
127   * This is called by both getField and getFieldsAsList.
128   * @param fieldID  The id of the field starting from 0.
129   * @param nullSequence  The sequence representing NULL value.
130   * @return  The value of the field
131   */
132  private Object uncheckedGetField(int fieldID) {
133    if (!getFieldInited()[fieldID]) {
134      getFieldInited()[fieldID] = true;
135      ByteArrayRef ref = null;
136      String columnFamily = hbaseColumnFamilies.get(fieldID);
137      String columnQualifier = hbaseColumnQualifiers.get(fieldID);
138      byte [] columnFamilyBytes = hbaseColumnFamiliesBytes.get(fieldID);
139      byte [] columnQualifierBytes = hbaseColumnQualifiersBytes.get(fieldID);
140
141      if (HBaseSerDe.isSpecialColumn(columnFamily)) {
142        assert(columnQualifier == null);
143        ref = new ByteArrayRef();
144        ref.setData(result.getRow());
145      } else {
146        if (columnQualifier == null) {
147          // it is a column family
148          ((LazyHBaseCellMap) getFields()[fieldID]).init(result, columnFamilyBytes);
149        } else {
150          // it is a column i.e. a column-family with column-qualifier
151          byte [] res = result.getValue(columnFamilyBytes, columnQualifierBytes);
152
153          if (res == null) {
154            return null;
155          } else {
156            ref = new ByteArrayRef();
157            ref.setData(res);
158          }
159        }
160      }
161
162      if (ref != null) {
163        getFields()[fieldID].init(ref, 0, ref.getData().length);
164      }
165    }
166
167    return getFields()[fieldID].getObject();
168  }
169
170  /**
171   * Get the values of the fields as an ArrayList.
172   * @return The values of the fields as an ArrayList.
173   */
174  @Override
175  public ArrayList<Object> getFieldsAsList() {
176    if (!getParsed()) {
177      parse();
178    }
179    if (cachedList == null) {
180      cachedList = new ArrayList<Object>();
181    } else {
182      cachedList.clear();
183    }
184    for (int i = 0; i < getFields().length; i++) {
185      cachedList.add(uncheckedGetField(i));
186    }
187    return cachedList;
188  }
189
190  @Override
191  public Object getObject() {
192    return this;
193  }
194}