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