org.apache.iceberg.mr.hive.Deserializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of iceberg-mr Show documentation
Show all versions of iceberg-mr Show documentation
A table format for huge analytic datasets
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License 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 org.apache.iceberg.mr.hive;
import java.util.List;
import java.util.Map;
import org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.StructField;
import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
import org.apache.iceberg.Schema;
import org.apache.iceberg.data.GenericRecord;
import org.apache.iceberg.data.Record;
import org.apache.iceberg.mr.hive.serde.objectinspector.WriteObjectInspector;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
import org.apache.iceberg.schema.SchemaWithPartnerVisitor;
import org.apache.iceberg.types.Type.PrimitiveType;
import org.apache.iceberg.types.Types.ListType;
import org.apache.iceberg.types.Types.MapType;
import org.apache.iceberg.types.Types.NestedField;
import org.apache.iceberg.types.Types.StructType;
class Deserializer {
private final FieldDeserializer fieldDeserializer;
/**
* Builder to create a Deserializer instance. Requires an Iceberg Schema and the Hive
* ObjectInspector for converting the data.
*/
static class Builder {
private Schema schema;
private StructObjectInspector writerInspector;
private StructObjectInspector sourceInspector;
Builder schema(Schema mainSchema) {
this.schema = mainSchema;
return this;
}
Builder writerInspector(StructObjectInspector inspector) {
this.writerInspector = inspector;
return this;
}
Builder sourceInspector(StructObjectInspector inspector) {
this.sourceInspector = inspector;
return this;
}
Deserializer build() {
return new Deserializer(schema, new ObjectInspectorPair(writerInspector, sourceInspector));
}
}
/**
* Deserializes the Hive result object to an Iceberg record using the provided ObjectInspectors.
*
* @param data The Hive data to deserialize
* @return The resulting Iceberg Record
*/
Record deserialize(Object data) {
return (Record) fieldDeserializer.value(data);
}
private Deserializer(Schema schema, ObjectInspectorPair pair) {
this.fieldDeserializer = DeserializerVisitor.visit(schema, pair);
}
private static class DeserializerVisitor
extends SchemaWithPartnerVisitor {
public static FieldDeserializer visit(Schema schema, ObjectInspectorPair pair) {
return visit(
schema,
new FixNameMappingObjectInspectorPair(schema, pair),
new DeserializerVisitor(),
new PartnerObjectInspectorByNameAccessors());
}
@Override
public FieldDeserializer schema(
Schema schema, ObjectInspectorPair pair, FieldDeserializer deserializer) {
return deserializer;
}
@Override
public FieldDeserializer field(
NestedField field, ObjectInspectorPair pair, FieldDeserializer deserializer) {
return deserializer;
}
@Override
public FieldDeserializer primitive(PrimitiveType type, ObjectInspectorPair pair) {
return o -> {
if (o == null) {
return null;
}
ObjectInspector writerFieldInspector = pair.writerInspector();
ObjectInspector sourceFieldInspector = pair.sourceInspector();
Object result = ((PrimitiveObjectInspector) sourceFieldInspector).getPrimitiveJavaObject(o);
if (writerFieldInspector instanceof WriteObjectInspector) {
// If we have a conversion method defined for the ObjectInspector then convert
result = ((WriteObjectInspector) writerFieldInspector).convert(result);
}
return result;
};
}
@Override
public FieldDeserializer struct(
StructType type, ObjectInspectorPair pair, List deserializers) {
Preconditions.checkNotNull(type, "Can not create reader for null type");
GenericRecord template = GenericRecord.create(type);
return o -> {
if (o == null) {
return null;
}
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy