com.amazon.ionhiveserde.objectinspectors.IonStructToStructInspector Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ion-hive3-serde Show documentation
Show all versions of ion-hive3-serde Show documentation
An Apache Hive SerDe (short for serializer/deserializer) for the Ion file format.
/*
* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazon.ionhiveserde.objectinspectors;
import static com.amazon.ionhiveserde.objectinspectors.IonUtil.isIonNull;
import com.amazon.ion.IonStruct;
import com.amazon.ion.IonValue;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorUtils;
import org.apache.hadoop.hive.serde2.objectinspector.StructField;
import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
import org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo;
/**
* Adapts an {@link IonStruct} for the struct Hive type.
*/
public class IonStructToStructInspector extends StructObjectInspector {
private final Map fieldsByName;
private final List fields;
/**
* Creates a Ion struct to Hive struct object inspector.
*
* @param structTypeInfo TypeInfo for all fields
* @param fieldObjectInspectors list of field ObjectInspectors in order
*/
public IonStructToStructInspector(final StructTypeInfo structTypeInfo,
final List fieldObjectInspectors) {
fieldsByName = new HashMap<>();
fields = new ArrayList<>();
final ArrayList structFieldNames = structTypeInfo.getAllStructFieldNames();
for (int i = 0; i < structFieldNames.size(); i++) {
final String fieldName = structFieldNames.get(i);
final ObjectInspector objectInspector = fieldObjectInspectors.get(i);
final IonStructField field = new IonStructField(fieldName, objectInspector, i);
fields.add(field);
fieldsByName.put(fieldName, field);
}
}
@Override
public List extends StructField> getAllStructFieldRefs() {
return fields;
}
@Override
public StructField getStructFieldRef(final String fieldName) {
if (fieldName == null) {
throw new IllegalArgumentException("field name cannot be null");
}
return fieldsByName.get(fieldName);
}
@Override
public Object getStructFieldData(final Object data, final StructField fieldRef) {
if (isIonNull((IonValue) data)) {
return null;
}
if (fieldRef == null) {
throw new IllegalArgumentException("fieldRef name cannot be null");
}
final IonStruct struct = (IonStruct) data;
return struct.get(fieldRef.getFieldName());
}
@Override
public List