Search in sources :

Example 1 with OrcDataSink

use of io.prestosql.orc.OrcDataSink in project hetu-core by openlookeng.

the class OrcFileWriterFactory method createFileWriter.

@Override
public Optional<HiveFileWriter> createFileWriter(Path path, List<String> inputColumnNames, StorageFormat storageFormat, Properties schema, JobConf configuration, ConnectorSession session, Optional<AcidOutputFormat.Options> acidOptions, Optional<HiveACIDWriteType> acidWriteType) {
    if (!OrcOutputFormat.class.getName().equals(storageFormat.getOutputFormat())) {
        return Optional.empty();
    }
    CompressionKind compression = getCompression(schema, configuration);
    // existing tables and partitions may have columns in a different order than the writer is providing, so build
    // an index to rearrange columns in the proper order
    List<String> fileColumnNames = getColumnNames(schema);
    List<Type> fileColumnTypes = getColumnTypes(schema).stream().map(hiveType -> hiveType.getType(typeManager)).collect(toList());
    List<Type> dataFileColumnTypes = fileColumnTypes;
    int[] fileInputColumnIndexes = fileColumnNames.stream().mapToInt(inputColumnNames::indexOf).toArray();
    Optional<HiveFileWriter> deleteDeltaWriter = Optional.empty();
    if (AcidUtils.isTablePropertyTransactional(schema) && !AcidUtils.isInsertOnlyTable(schema)) {
        ImmutableList<String> orcFileColumnNames = ImmutableList.of(OrcPageSourceFactory.ACID_COLUMN_OPERATION, OrcPageSourceFactory.ACID_COLUMN_ORIGINAL_TRANSACTION, OrcPageSourceFactory.ACID_COLUMN_BUCKET, OrcPageSourceFactory.ACID_COLUMN_ROW_ID, OrcPageSourceFactory.ACID_COLUMN_CURRENT_TRANSACTION, OrcPageSourceFactory.ACID_COLUMN_ROW_STRUCT);
        ImmutableList.Builder<RowType.Field> fieldsBuilder = ImmutableList.builder();
        for (int i = 0; i < fileColumnNames.size(); i++) {
            fieldsBuilder.add(new RowType.Field(Optional.of(fileColumnNames.get(i)), fileColumnTypes.get(i)));
        }
        ImmutableList<Type> orcFileColumnTypes = ImmutableList.of(INTEGER, BIGINT, INTEGER, BIGINT, BIGINT, RowType.from(fieldsBuilder.build()));
        fileColumnNames = orcFileColumnNames;
        fileColumnTypes = orcFileColumnTypes;
        if (acidWriteType.isPresent() && acidWriteType.get() == HiveACIDWriteType.UPDATE) {
            AcidOutputFormat.Options deleteOptions = acidOptions.get().clone().writingDeleteDelta(true);
            Path deleteDeltaPath = AcidUtils.createFilename(path.getParent().getParent(), deleteOptions);
            deleteDeltaWriter = createFileWriter(deleteDeltaPath, inputColumnNames, storageFormat, schema, configuration, session, Optional.of(deleteOptions), Optional.of(HiveACIDWriteType.DELETE));
        }
    }
    try {
        FileSystem fileSystem = hdfsEnvironment.getFileSystem(session.getUser(), path, configuration);
        OrcDataSink orcDataSink = createOrcDataSink(session, fileSystem, path);
        Optional<Supplier<OrcDataSource>> validationInputFactory = Optional.empty();
        if (HiveSessionProperties.isOrcOptimizedWriterValidate(session)) {
            validationInputFactory = Optional.of(() -> {
                try {
                    FileStatus fileStatus = fileSystem.getFileStatus(path);
                    return new HdfsOrcDataSource(new OrcDataSourceId(path.toString()), fileStatus.getLen(), HiveSessionProperties.getOrcMaxMergeDistance(session), HiveSessionProperties.getOrcMaxBufferSize(session), HiveSessionProperties.getOrcStreamBufferSize(session), false, fileSystem.open(path), readStats, fileStatus.getModificationTime());
                } catch (IOException e) {
                    throw new PrestoException(HiveErrorCode.HIVE_WRITE_VALIDATION_FAILED, e);
                }
            });
        }
        Callable<Void> rollbackAction = () -> {
            fileSystem.delete(path, false);
            return null;
        };
        return Optional.of(new OrcFileWriter(orcDataSink, rollbackAction, fileColumnNames, fileColumnTypes, dataFileColumnTypes, compression, orcWriterOptions.withStripeMinSize(HiveSessionProperties.getOrcOptimizedWriterMinStripeSize(session)).withStripeMaxSize(HiveSessionProperties.getOrcOptimizedWriterMaxStripeSize(session)).withStripeMaxRowCount(HiveSessionProperties.getOrcOptimizedWriterMaxStripeRows(session)).withDictionaryMaxMemory(HiveSessionProperties.getOrcOptimizedWriterMaxDictionaryMemory(session)).withMaxStringStatisticsLimit(HiveSessionProperties.getOrcStringStatisticsLimit(session)), writeLegacyVersion, fileInputColumnIndexes, ImmutableMap.<String, String>builder().put(HiveMetadata.PRESTO_VERSION_NAME, nodeVersion.toString()).put(HiveMetadata.PRESTO_QUERY_ID_NAME, session.getQueryId()).put("hive.acid.version", String.valueOf(AcidUtils.OrcAcidVersion.ORC_ACID_VERSION)).build(), validationInputFactory, HiveSessionProperties.getOrcOptimizedWriterValidateMode(session), stats, acidOptions, acidWriteType, deleteDeltaWriter, path));
    } catch (IOException e) {
        throw new PrestoException(HiveErrorCode.HIVE_WRITER_OPEN_ERROR, "Error creating ORC file", e);
    }
}
Also used : StorageFormat(io.prestosql.plugin.hive.metastore.StorageFormat) FileSystem(org.apache.hadoop.fs.FileSystem) Flatten(org.weakref.jmx.Flatten) HiveUtil.getColumnTypes(io.prestosql.plugin.hive.HiveUtil.getColumnTypes) Callable(java.util.concurrent.Callable) INTEGER(io.prestosql.spi.type.IntegerType.INTEGER) FileStatus(org.apache.hadoop.fs.FileStatus) Supplier(java.util.function.Supplier) Inject(javax.inject.Inject) ImmutableList(com.google.common.collect.ImmutableList) ConnectorSession(io.prestosql.spi.connector.ConnectorSession) Managed(org.weakref.jmx.Managed) OrcDataSink(io.prestosql.orc.OrcDataSink) OrcConf(org.apache.orc.OrcConf) HiveUtil.getColumnNames(io.prestosql.plugin.hive.HiveUtil.getColumnNames) Objects.requireNonNull(java.util.Objects.requireNonNull) RowType(io.prestosql.spi.type.RowType) OrcPageSourceFactory(io.prestosql.plugin.hive.orc.OrcPageSourceFactory) Path(org.apache.hadoop.fs.Path) Type(io.prestosql.spi.type.Type) BIGINT(io.prestosql.spi.type.BigintType.BIGINT) ENGLISH(java.util.Locale.ENGLISH) PrestoException(io.prestosql.spi.PrestoException) Properties(java.util.Properties) ImmutableMap(com.google.common.collect.ImmutableMap) TypeManager(io.prestosql.spi.type.TypeManager) AcidOutputFormat(org.apache.hadoop.hive.ql.io.AcidOutputFormat) OrcWriterStats(io.prestosql.orc.OrcWriterStats) IOException(java.io.IOException) OrcDataSource(io.prestosql.orc.OrcDataSource) OrcWriterOptions(io.prestosql.orc.OrcWriterOptions) JobConf(org.apache.hadoop.mapred.JobConf) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) OutputStreamOrcDataSink(io.prestosql.orc.OutputStreamOrcDataSink) CompressionKind(io.prestosql.orc.metadata.CompressionKind) HdfsOrcDataSource(io.prestosql.plugin.hive.orc.HdfsOrcDataSource) Optional(java.util.Optional) OrcOutputFormat(org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat) AcidUtils(org.apache.hadoop.hive.ql.io.AcidUtils) OrcDataSourceId(io.prestosql.orc.OrcDataSourceId) FileStatus(org.apache.hadoop.fs.FileStatus) ImmutableList(com.google.common.collect.ImmutableList) OrcDataSink(io.prestosql.orc.OrcDataSink) OutputStreamOrcDataSink(io.prestosql.orc.OutputStreamOrcDataSink) RowType(io.prestosql.spi.type.RowType) HdfsOrcDataSource(io.prestosql.plugin.hive.orc.HdfsOrcDataSource) PrestoException(io.prestosql.spi.PrestoException) AcidOutputFormat(org.apache.hadoop.hive.ql.io.AcidOutputFormat) FileSystem(org.apache.hadoop.fs.FileSystem) Supplier(java.util.function.Supplier) Path(org.apache.hadoop.fs.Path) CompressionKind(io.prestosql.orc.metadata.CompressionKind) OrcDataSourceId(io.prestosql.orc.OrcDataSourceId) IOException(java.io.IOException) RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 OrcDataSink (io.prestosql.orc.OrcDataSink)1 OrcDataSource (io.prestosql.orc.OrcDataSource)1 OrcDataSourceId (io.prestosql.orc.OrcDataSourceId)1 OrcWriterOptions (io.prestosql.orc.OrcWriterOptions)1 OrcWriterStats (io.prestosql.orc.OrcWriterStats)1 OutputStreamOrcDataSink (io.prestosql.orc.OutputStreamOrcDataSink)1 CompressionKind (io.prestosql.orc.metadata.CompressionKind)1 HiveUtil.getColumnNames (io.prestosql.plugin.hive.HiveUtil.getColumnNames)1 HiveUtil.getColumnTypes (io.prestosql.plugin.hive.HiveUtil.getColumnTypes)1 StorageFormat (io.prestosql.plugin.hive.metastore.StorageFormat)1 HdfsOrcDataSource (io.prestosql.plugin.hive.orc.HdfsOrcDataSource)1 OrcPageSourceFactory (io.prestosql.plugin.hive.orc.OrcPageSourceFactory)1 PrestoException (io.prestosql.spi.PrestoException)1 ConnectorSession (io.prestosql.spi.connector.ConnectorSession)1 BIGINT (io.prestosql.spi.type.BigintType.BIGINT)1 INTEGER (io.prestosql.spi.type.IntegerType.INTEGER)1 RowType (io.prestosql.spi.type.RowType)1 Type (io.prestosql.spi.type.Type)1