PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/serde/src/java/org/apache/hadoop/hive/serde2/lazy/objectinspector/LazySimpleStructObjectInspector.java

#
Java | 199 lines | 132 code | 34 blank | 33 comment | 8 complexity | 2eb2fae782a3de20a064eaf23ae9ba11 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.serde2.lazy.objectinspector;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;
  23. import org.apache.hadoop.hive.serde2.lazy.LazyStruct;
  24. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
  25. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorUtils;
  26. import org.apache.hadoop.hive.serde2.objectinspector.StructField;
  27. import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
  28. import org.apache.hadoop.io.Text;
  29. /**
  30. * LazySimpleStructObjectInspector works on struct data that is stored in
  31. * LazyStruct.
  32. *
  33. * The names of the struct fields and the internal structure of the struct
  34. * fields are specified in the ctor of the LazySimpleStructObjectInspector.
  35. *
  36. * Always use the ObjectInspectorFactory to create new ObjectInspector objects,
  37. * instead of directly creating an instance of this class.
  38. */
  39. public class LazySimpleStructObjectInspector extends StructObjectInspector {
  40. public static final Log LOG = LogFactory
  41. .getLog(LazySimpleStructObjectInspector.class.getName());
  42. protected static class MyField implements StructField {
  43. protected int fieldID;
  44. protected String fieldName;
  45. protected ObjectInspector fieldObjectInspector;
  46. public MyField(int fieldID, String fieldName,
  47. ObjectInspector fieldObjectInspector) {
  48. this.fieldID = fieldID;
  49. this.fieldName = fieldName.toLowerCase();
  50. this.fieldObjectInspector = fieldObjectInspector;
  51. }
  52. public int getFieldID() {
  53. return fieldID;
  54. }
  55. public String getFieldName() {
  56. return fieldName;
  57. }
  58. public ObjectInspector getFieldObjectInspector() {
  59. return fieldObjectInspector;
  60. }
  61. @Override
  62. public String toString() {
  63. return "" + fieldID + ":" + fieldName;
  64. }
  65. }
  66. protected List<MyField> fields;
  67. @Override
  68. public String getTypeName() {
  69. return ObjectInspectorUtils.getStandardStructTypeName(this);
  70. }
  71. byte separator;
  72. Text nullSequence;
  73. boolean lastColumnTakesRest;
  74. boolean escaped;
  75. byte escapeChar;
  76. /**
  77. * Call ObjectInspectorFactory.getLazySimpleStructObjectInspector instead.
  78. */
  79. protected LazySimpleStructObjectInspector(List<String> structFieldNames,
  80. List<ObjectInspector> structFieldObjectInspectors, byte separator,
  81. Text nullSequence, boolean lastColumnTakesRest, boolean escaped,
  82. byte escapeChar) {
  83. init(structFieldNames, structFieldObjectInspectors, separator,
  84. nullSequence, lastColumnTakesRest, escaped, escapeChar);
  85. }
  86. protected void init(List<String> structFieldNames,
  87. List<ObjectInspector> structFieldObjectInspectors, byte separator,
  88. Text nullSequence, boolean lastColumnTakesRest, boolean escaped,
  89. byte escapeChar) {
  90. assert (structFieldNames.size() == structFieldObjectInspectors.size());
  91. this.separator = separator;
  92. this.nullSequence = nullSequence;
  93. this.lastColumnTakesRest = lastColumnTakesRest;
  94. this.escaped = escaped;
  95. this.escapeChar = escapeChar;
  96. fields = new ArrayList<MyField>(structFieldNames.size());
  97. for (int i = 0; i < structFieldNames.size(); i++) {
  98. fields.add(new MyField(i, structFieldNames.get(i),
  99. structFieldObjectInspectors.get(i)));
  100. }
  101. }
  102. protected LazySimpleStructObjectInspector(List<StructField> fields,
  103. byte separator, Text nullSequence) {
  104. init(fields, separator, nullSequence);
  105. }
  106. protected void init(List<StructField> fields, byte separator,
  107. Text nullSequence) {
  108. this.separator = separator;
  109. this.nullSequence = nullSequence;
  110. this.fields = new ArrayList<MyField>(fields.size());
  111. for (int i = 0; i < fields.size(); i++) {
  112. this.fields.add(new MyField(i, fields.get(i).getFieldName(), fields
  113. .get(i).getFieldObjectInspector()));
  114. }
  115. }
  116. @Override
  117. public final Category getCategory() {
  118. return Category.STRUCT;
  119. }
  120. // Without Data
  121. @Override
  122. public StructField getStructFieldRef(String fieldName) {
  123. return ObjectInspectorUtils.getStandardStructFieldRef(fieldName, fields);
  124. }
  125. @Override
  126. public List<? extends StructField> getAllStructFieldRefs() {
  127. return fields;
  128. }
  129. // With Data
  130. @Override
  131. public Object getStructFieldData(Object data, StructField fieldRef) {
  132. if (data == null) {
  133. return null;
  134. }
  135. LazyStruct struct = (LazyStruct) data;
  136. MyField f = (MyField) fieldRef;
  137. int fieldID = f.getFieldID();
  138. assert (fieldID >= 0 && fieldID < fields.size());
  139. return struct.getField(fieldID);
  140. }
  141. @Override
  142. public List<Object> getStructFieldsDataAsList(Object data) {
  143. if (data == null) {
  144. return null;
  145. }
  146. LazyStruct struct = (LazyStruct) data;
  147. return struct.getFieldsAsList();
  148. }
  149. // For LazyStruct
  150. public byte getSeparator() {
  151. return separator;
  152. }
  153. public Text getNullSequence() {
  154. return nullSequence;
  155. }
  156. public boolean getLastColumnTakesRest() {
  157. return lastColumnTakesRest;
  158. }
  159. public boolean isEscaped() {
  160. return escaped;
  161. }
  162. public byte getEscapeChar() {
  163. return escapeChar;
  164. }
  165. }