Search in sources :

Example 11 with HdfsContext

use of io.prestosql.plugin.hive.HdfsEnvironment.HdfsContext in project hetu-core by openlookeng.

the class SemiTransactionalHiveMetastore method finishInsertIntoExistingPartition.

public synchronized void finishInsertIntoExistingPartition(ConnectorSession session, String databaseName, String tableName, List<String> partitionValues, Path currentLocation, List<String> fileNames, PartitionStatistics statisticsUpdate, HiveACIDWriteType acidWriteType) {
    setShared();
    isVacuumIncluded |= HiveACIDWriteType.isVacuum(acidWriteType);
    HiveIdentity identity = new HiveIdentity(session);
    SchemaTableName schemaTableName = new SchemaTableName(databaseName, tableName);
    Map<List<String>, Action<PartitionAndMore>> partitionActionsOfTable = partitionActions.computeIfAbsent(schemaTableName, k -> new LinkedHashMap<>());
    Action<PartitionAndMore> oldPartitionAction = partitionActionsOfTable.get(partitionValues);
    if (oldPartitionAction == null) {
        Partition partition = delegate.getPartition(identity, databaseName, tableName, partitionValues).orElseThrow(() -> new PartitionNotFoundException(schemaTableName, partitionValues));
        String partitionName = getPartitionName(identity, databaseName, tableName, partitionValues);
        PartitionStatistics mergedStatistics = statisticsUpdate;
        boolean updateStats = canUpdateStats(session, acidWriteType);
        if (updateStats) {
            PartitionStatistics currentStatistics = closure.getPartitionStatistics(identity, databaseName, tableName, ImmutableSet.of(partitionName)).get(partitionName);
            if (currentStatistics == null) {
                throw new PrestoException(HiveErrorCode.HIVE_METASTORE_ERROR, "currentStatistics is null");
            }
            mergedStatistics = merge(currentStatistics, statisticsUpdate);
        }
        HdfsContext context = new HdfsContext(session, databaseName, tableName);
        partitionActionsOfTable.put(partitionValues, new Action<>(ActionType.INSERT_EXISTING, new PartitionAndMore(identity, partition, currentLocation, Optional.of(fileNames), mergedStatistics, statisticsUpdate, updateStats), context, identity));
        return;
    }
    switch(oldPartitionAction.getType()) {
        case DROP:
            throw new PartitionNotFoundException(schemaTableName, partitionValues);
        case ADD:
        case ALTER:
        case INSERT_EXISTING:
            throw new UnsupportedOperationException("Inserting into a partition that were added, altered, or inserted into in the same transaction is not supported");
        default:
            throw new IllegalStateException("Unknown action type");
    }
}
Also used : PrestoException(io.prestosql.spi.PrestoException) SchemaTableName(io.prestosql.spi.connector.SchemaTableName) HiveIdentity(io.prestosql.plugin.hive.authentication.HiveIdentity) PartitionNotFoundException(io.prestosql.plugin.hive.PartitionNotFoundException) PartitionStatistics(io.prestosql.plugin.hive.PartitionStatistics) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ValidTxnWriteIdList(org.apache.hadoop.hive.common.ValidTxnWriteIdList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) HdfsContext(io.prestosql.plugin.hive.HdfsEnvironment.HdfsContext)

Example 12 with HdfsContext

use of io.prestosql.plugin.hive.HdfsEnvironment.HdfsContext in project hetu-core by openlookeng.

the class SemiTransactionalHiveMetastore method addPartition.

public synchronized void addPartition(ConnectorSession session, String databaseName, String tableName, Partition partition, Path currentLocation, PartitionStatistics statistics, HiveACIDWriteType acidWriteType) {
    setShared();
    checkArgument(getPrestoQueryId(partition).isPresent());
    Map<List<String>, Action<PartitionAndMore>> partitionActionsOfTable = partitionActions.computeIfAbsent(new SchemaTableName(databaseName, tableName), k -> new ConcurrentHashMap<>());
    Action<PartitionAndMore> oldPartitionAction = partitionActionsOfTable.get(partition.getValues());
    HdfsContext hdfsContext = new HdfsContext(session, databaseName, tableName);
    HiveIdentity identity = new HiveIdentity(session);
    boolean canUpdateStats = canUpdateStats(session, acidWriteType);
    this.partitionCommitBatchSize = HiveSessionProperties.getMetastoreWriteBatchSize(session);
    if (oldPartitionAction == null) {
        partitionActionsOfTable.put(partition.getValues(), new Action<>(ActionType.ADD, new PartitionAndMore(identity, partition, currentLocation, Optional.empty(), statistics, statistics, canUpdateStats), hdfsContext, identity));
        return;
    }
    switch(oldPartitionAction.getType()) {
        case DROP:
            {
                if (!oldPartitionAction.getHdfsContext().getIdentity().getUser().equals(session.getUser())) {
                    throw new PrestoException(TRANSACTION_CONFLICT, "Operation on the same partition with different user in the same transaction is not supported");
                }
                partitionActionsOfTable.put(partition.getValues(), new Action<>(ActionType.ALTER, new PartitionAndMore(identity, partition, currentLocation, Optional.empty(), statistics, statistics, canUpdateStats), hdfsContext, identity));
                break;
            }
        case ADD:
        case ALTER:
        case INSERT_EXISTING:
            throw new PrestoException(ALREADY_EXISTS, format("Partition already exists for table '%s.%s': %s", databaseName, tableName, partition.getValues()));
        default:
            throw new IllegalStateException("Unknown action type");
    }
}
Also used : PrestoException(io.prestosql.spi.PrestoException) SchemaTableName(io.prestosql.spi.connector.SchemaTableName) HiveIdentity(io.prestosql.plugin.hive.authentication.HiveIdentity) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ValidTxnWriteIdList(org.apache.hadoop.hive.common.ValidTxnWriteIdList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) HdfsContext(io.prestosql.plugin.hive.HdfsEnvironment.HdfsContext)

Example 13 with HdfsContext

use of io.prestosql.plugin.hive.HdfsEnvironment.HdfsContext in project hetu-core by openlookeng.

the class HiveMetadata method createEmptyFiles.

private void createEmptyFiles(ConnectorSession session, Path path, Table table, Optional<Partition> partition, List<String> fileNames) {
    JobConf conf = ConfigurationUtils.toJobConf(hdfsEnvironment.getConfiguration(new HdfsContext(session, table.getDatabaseName(), table.getTableName()), path));
    Properties schema;
    StorageFormat format;
    if (partition.isPresent()) {
        schema = MetastoreUtil.getHiveSchema(partition.get(), table);
        format = partition.get().getStorage().getStorageFormat();
    } else {
        schema = MetastoreUtil.getHiveSchema(table);
        format = table.getStorage().getStorageFormat();
    }
    hdfsEnvironment.doAs(session.getUser(), () -> {
        for (String fileName : fileNames) {
            writeEmptyFile(session, new Path(path, fileName), conf, schema, format.getSerDe(), format.getOutputFormat());
        }
    });
}
Also used : Path(org.apache.hadoop.fs.Path) HdfsContext(io.prestosql.plugin.hive.HdfsEnvironment.HdfsContext) StorageFormat(io.prestosql.plugin.hive.metastore.StorageFormat) HiveTableProperties.getHiveStorageFormat(io.prestosql.plugin.hive.HiveTableProperties.getHiveStorageFormat) Properties(java.util.Properties) ConnectorTableProperties(io.prestosql.spi.connector.ConnectorTableProperties) JobConf(org.apache.hadoop.mapred.JobConf)

Example 14 with HdfsContext

use of io.prestosql.plugin.hive.HdfsEnvironment.HdfsContext in project hetu-core by openlookeng.

the class HiveMetadata method getTableModificationTime.

@Override
public long getTableModificationTime(ConnectorSession session, ConnectorTableHandle tableHandle) {
    SchemaTableName tableName = ((HiveTableHandle) tableHandle).getSchemaTableName();
    Table table = metastore.getTable(new HiveIdentity(session), tableName.getSchemaName(), tableName.getTableName()).orElseThrow(() -> new TableNotFoundException(tableName));
    String tableLocation = table.getStorage().getLocation();
    Path tablePath = new Path(tableLocation);
    try {
        FileSystem fileSystem = this.hdfsEnvironment.getFileSystem(new HdfsContext(session, tableName.getSchemaName()), tablePath);
        // since HDFS is append-only and any table modification will trigger directory update.
        return fileSystem.getFileStatus(tablePath).getModificationTime();
    }// We want to make sure the query doesn't fail because of star-tree not being able to get last modified time
     catch (Exception e) {
        log.error("Exception thrown while trying to get modified time", e);
        return -1L;
    }
}
Also used : Path(org.apache.hadoop.fs.Path) TableNotFoundException(io.prestosql.spi.connector.TableNotFoundException) SystemTable(io.prestosql.spi.connector.SystemTable) Table(io.prestosql.plugin.hive.metastore.Table) HiveTableProperties.isExternalTable(io.prestosql.plugin.hive.HiveTableProperties.isExternalTable) FileSystem(org.apache.hadoop.fs.FileSystem) HiveWriteUtils.isS3FileSystem(io.prestosql.plugin.hive.HiveWriteUtils.isS3FileSystem) HdfsContext(io.prestosql.plugin.hive.HdfsEnvironment.HdfsContext) SchemaTableName(io.prestosql.spi.connector.SchemaTableName) HiveIdentity(io.prestosql.plugin.hive.authentication.HiveIdentity) TableAlreadyExistsException(io.prestosql.spi.connector.TableAlreadyExistsException) TableNotFoundException(io.prestosql.spi.connector.TableNotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ViewNotFoundException(io.prestosql.spi.connector.ViewNotFoundException) PrestoException(io.prestosql.spi.PrestoException) NoSuchElementException(java.util.NoSuchElementException) VerifyException(com.google.common.base.VerifyException) MalformedURLException(java.net.MalformedURLException)

Example 15 with HdfsContext

use of io.prestosql.plugin.hive.HdfsEnvironment.HdfsContext in project hetu-core by openlookeng.

the class HiveMetadata method computeFileNamesForMissingBuckets.

private List<String> computeFileNamesForMissingBuckets(ConnectorSession session, Table table, HiveStorageFormat storageFormat, Path targetPath, int bucketCount, PartitionUpdate partitionUpdate) {
    if (partitionUpdate.getFileNames().size() == bucketCount) {
        // fast path for common case
        return ImmutableList.of();
    }
    HdfsContext hdfsContext = new HdfsContext(session, table.getDatabaseName(), table.getTableName());
    JobConf conf = ConfigurationUtils.toJobConf(hdfsEnvironment.getConfiguration(hdfsContext, targetPath));
    String fileExtension = HiveWriterFactory.getFileExtension(conf, StorageFormat.fromHiveStorageFormat(storageFormat));
    Set<String> fileNames = ImmutableSet.copyOf(partitionUpdate.getFileNames());
    ImmutableList.Builder<String> missingFileNamesBuilder = ImmutableList.builder();
    for (int i = 0; i < bucketCount; i++) {
        String fileName = HiveWriterFactory.computeBucketedFileName(session.getQueryId(), i) + fileExtension;
        if (!fileNames.contains(fileName)) {
            missingFileNamesBuilder.add(fileName);
        }
    }
    List<String> missingFileNames = missingFileNamesBuilder.build();
    verify(fileNames.size() + missingFileNames.size() == bucketCount);
    return missingFileNames;
}
Also used : ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) HdfsContext(io.prestosql.plugin.hive.HdfsEnvironment.HdfsContext) JobConf(org.apache.hadoop.mapred.JobConf) Constraint(io.prestosql.spi.connector.Constraint)

Aggregations

HdfsContext (io.prestosql.plugin.hive.HdfsEnvironment.HdfsContext)70 Path (org.apache.hadoop.fs.Path)48 HiveIdentity (io.prestosql.plugin.hive.authentication.HiveIdentity)40 ConnectorSession (io.prestosql.spi.connector.ConnectorSession)36 PrestoException (io.prestosql.spi.PrestoException)34 SchemaTableName (io.prestosql.spi.connector.SchemaTableName)28 ConnectorMetadata (io.prestosql.spi.connector.ConnectorMetadata)26 ConnectorTableHandle (io.prestosql.spi.connector.ConnectorTableHandle)26 ImmutableList (com.google.common.collect.ImmutableList)24 TestingConnectorSession (io.prestosql.testing.TestingConnectorSession)24 Slice (io.airlift.slice.Slice)22 Table (io.prestosql.plugin.hive.metastore.Table)22 ColumnHandle (io.prestosql.spi.connector.ColumnHandle)22 List (java.util.List)22 FileSystem (org.apache.hadoop.fs.FileSystem)22 ConnectorInsertTableHandle (io.prestosql.spi.connector.ConnectorInsertTableHandle)20 ConnectorPageSink (io.prestosql.spi.connector.ConnectorPageSink)20 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)18 TableNotFoundException (io.prestosql.spi.connector.TableNotFoundException)18 IOException (java.io.IOException)18