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