org.apache.paimon.hive.HiveDeserializer Maven / Gradle / Ivy
/*
* 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.paimon.hive;
import org.apache.paimon.data.GenericRow;
import org.apache.paimon.data.InternalArray;
import org.apache.paimon.data.InternalMap;
import org.apache.paimon.data.InternalRow;
import org.apache.paimon.hive.objectinspector.HivePaimonArray;
import org.apache.paimon.hive.objectinspector.WriteableObjectInspector;
import org.apache.paimon.types.ArrayType;
import org.apache.paimon.types.DataType;
import org.apache.paimon.types.MapType;
import org.apache.paimon.types.RowType;
import org.apache.paimon.utils.Preconditions;
import org.apache.paimon.shade.guava30.com.google.common.collect.Lists;
import org.apache.paimon.shade.guava30.com.google.common.collect.Maps;
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 java.util.ArrayList;
import java.util.List;
import java.util.Map;
/** A deserializer to deserialize hive objects to {@link InternalRow}. */
public class HiveDeserializer {
private final FieldDeserializer fieldDeserializer;
/**
* Builder to create a HiveDeserializer instance. Requires a Paimon HiveSchema and the Hive
* ObjectInspector for converting the data.
*/
static class Builder {
private HiveSchema schema;
private StructObjectInspector writerInspector;
private StructObjectInspector sourceInspector;
Builder schema(HiveSchema mainSchema) {
this.schema = mainSchema;
return this;
}
Builder writerInspector(StructObjectInspector inspector) {
this.writerInspector = inspector;
return this;
}
Builder sourceInspector(StructObjectInspector inspector) {
this.sourceInspector = inspector;
return this;
}
HiveDeserializer build() {
return new HiveDeserializer(
schema, new ObjectInspectorPair(writerInspector, sourceInspector));
}
}
/**
* Deserializes the Hive result object to a Paimon record using the provided ObjectInspectors.
*
* @param data The Hive data to deserialize
* @return The resulting Paimon Record
*/
InternalRow deserialize(Object data) {
return (InternalRow) fieldDeserializer.value(data);
}
private HiveDeserializer(HiveSchema schema, ObjectInspectorPair pair) {
this.fieldDeserializer = DeserializerVisitor.visit(schema, pair);
}
private static class DeserializerVisitor
extends SchemaVisitor {
public static FieldDeserializer visit(HiveSchema schema, ObjectInspectorPair pair) {
return visit(
schema,
new SchemaNameMappingObjectInspectorPair(schema, pair),
new DeserializerVisitor(),
new PartnerObjectInspectorByNameAccessors());
}
@Override
public FieldDeserializer primitive(DataType 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 WriteableObjectInspector) {
result = ((WriteableObjectInspector) writerFieldInspector).convert(result);
}
return result;
};
}
@Override
public FieldDeserializer rowType(
RowType type, ObjectInspectorPair pair, List deserializers) {
Preconditions.checkNotNull(type, "Can not create deserializer for null type");
return o -> {
if (o == null) {
return null;
}
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy