Search in sources :

Example 1 with StructType

use of org.apache.iceberg.types.Types.StructType in project hive by apache.

the class IcebergInternalRecordWrapper method converter.

private static Function<Object, Object> converter(Type type) {
    switch(type.typeId()) {
        case TIMESTAMP:
            return timestamp -> DateTimeUtil.timestamptzFromMicros((Long) timestamp);
        case STRUCT:
            IcebergInternalRecordWrapper wrapper = new IcebergInternalRecordWrapper(type.asStructType(), type.asStructType());
            return struct -> wrapper.wrap((StructLike) struct);
        case LIST:
            if (Type.TypeID.STRUCT.equals(type.asListType().elementType().typeId())) {
                StructType listElementSchema = type.asListType().elementType().asStructType();
                Function<Type, IcebergInternalRecordWrapper> createWrapper = t -> new IcebergInternalRecordWrapper(listElementSchema, listElementSchema);
                return list -> {
                    return ((List<?>) list).stream().map(item -> createWrapper.apply(type).wrap((StructLike) item)).collect(Collectors.toList());
                };
            }
            break;
        default:
    }
    return null;
}
Also used : Arrays(java.util.Arrays) Array(java.lang.reflect.Array) Types(org.apache.iceberg.types.Types) HashMap(java.util.HashMap) StructLike(org.apache.iceberg.StructLike) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Type(org.apache.iceberg.types.Type) DateTimeUtil(org.apache.iceberg.util.DateTimeUtil) List(java.util.List) Record(org.apache.iceberg.data.Record) Map(java.util.Map) StructType(org.apache.iceberg.types.Types.StructType) Type(org.apache.iceberg.types.Type) StructType(org.apache.iceberg.types.Types.StructType) StructType(org.apache.iceberg.types.Types.StructType) StructLike(org.apache.iceberg.StructLike)

Example 2 with StructType

use of org.apache.iceberg.types.Types.StructType in project hive by apache.

the class ClusteredWriter method write.

@Override
public void write(T row, PartitionSpec spec, StructLike partition) {
    if (!spec.equals(currentSpec)) {
        if (currentSpec != null) {
            closeCurrentWriter();
            completedSpecIds.add(currentSpec.specId());
            completedPartitions.clear();
        }
        if (completedSpecIds.contains(spec.specId())) {
            String errorCtx = String.format("spec %s", spec);
            throw new IllegalStateException(NOT_CLUSTERED_ROWS_ERROR_MSG_TEMPLATE + errorCtx);
        }
        StructType partitionType = spec.partitionType();
        this.currentSpec = spec;
        this.partitionComparator = Comparators.forType(partitionType);
        this.completedPartitions = StructLikeSet.create(partitionType);
        // copy the partition key as the key object may be reused
        this.currentPartition = StructCopy.copy(partition);
        this.currentWriter = newWriter(currentSpec, currentPartition);
    } else if (partition != currentPartition && partitionComparator.compare(partition, currentPartition) != 0) {
        closeCurrentWriter();
        completedPartitions.add(currentPartition);
        if (completedPartitions.contains(partition) && !hasBucketTransform(currentSpec)) {
            String errorCtx = String.format("partition '%s' in spec %s", spec.partitionToPath(partition), spec);
            throw new IllegalStateException(NOT_CLUSTERED_ROWS_ERROR_MSG_TEMPLATE + errorCtx);
        }
        // copy the partition key as the key object may be reused
        this.currentPartition = StructCopy.copy(partition);
        this.currentWriter = newWriter(currentSpec, currentPartition);
    }
    currentWriter.write(row);
}
Also used : StructType(org.apache.iceberg.types.Types.StructType)

Aggregations

StructType (org.apache.iceberg.types.Types.StructType)2 Array (java.lang.reflect.Array)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 StructLike (org.apache.iceberg.StructLike)1 Record (org.apache.iceberg.data.Record)1 Type (org.apache.iceberg.types.Type)1 Types (org.apache.iceberg.types.Types)1 DateTimeUtil (org.apache.iceberg.util.DateTimeUtil)1