Search in sources :

Example 16 with Table

use of com.facebook.presto.hive.metastore.Table in project presto by prestodb.

the class FileHiveMetastore method dropPartition.

@Override
public synchronized void dropPartition(String databaseName, String tableName, List<String> partitionValues, boolean deleteData) {
    requireNonNull(databaseName, "databaseName is null");
    requireNonNull(tableName, "tableName is null");
    requireNonNull(partitionValues, "partitionValues is null");
    Optional<Table> tableReference = getTable(databaseName, tableName);
    if (!tableReference.isPresent()) {
        return;
    }
    Table table = tableReference.get();
    Path partitionMetadataDirectory = getPartitionMetadataDirectory(table, partitionValues);
    if (deleteData) {
        deleteMetadataDirectory(partitionMetadataDirectory);
    } else {
        deleteSchemaFile("partition", partitionMetadataDirectory);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) Table(com.facebook.presto.hive.metastore.Table)

Example 17 with Table

use of com.facebook.presto.hive.metastore.Table in project presto by prestodb.

the class FileHiveMetastore method getPartitionNames.

@Override
public synchronized Optional<List<String>> getPartitionNames(String databaseName, String tableName) {
    requireNonNull(databaseName, "databaseName is null");
    requireNonNull(tableName, "tableName is null");
    Optional<Table> tableReference = getTable(databaseName, tableName);
    if (!tableReference.isPresent()) {
        return Optional.empty();
    }
    Table table = tableReference.get();
    Path tableMetadataDirectory = getTableMetadataDirectory(table);
    List<ArrayDeque<String>> partitions = listPartitions(tableMetadataDirectory, table.getPartitionColumns());
    List<String> partitionNames = partitions.stream().map(partitionValues -> makePartName(table.getPartitionColumns(), ImmutableList.copyOf(partitionValues))).collect(toList());
    return Optional.of(ImmutableList.copyOf(partitionNames));
}
Also used : Path(org.apache.hadoop.fs.Path) HdfsEnvironment(com.facebook.presto.hive.HdfsEnvironment) SchemaAlreadyExistsException(com.facebook.presto.hive.SchemaAlreadyExistsException) MetastoreUtil.makePartName(com.facebook.presto.hive.metastore.MetastoreUtil.makePartName) FileSystem(org.apache.hadoop.fs.FileSystem) EXTERNAL_TABLE(org.apache.hadoop.hive.metastore.TableType.EXTERNAL_TABLE) PrincipalType(com.facebook.presto.hive.metastore.PrincipalType) FileStatus(org.apache.hadoop.fs.FileStatus) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) SchemaTableName(com.facebook.presto.spi.SchemaTableName) HIVE_METASTORE_ERROR(com.facebook.presto.hive.HiveErrorCode.HIVE_METASTORE_ERROR) ColumnNotFoundException(com.facebook.presto.spi.ColumnNotFoundException) ExtendedHiveMetastore(com.facebook.presto.hive.metastore.ExtendedHiveMetastore) SchemaNotFoundException(com.facebook.presto.spi.SchemaNotFoundException) Locale(java.util.Locale) Map(java.util.Map) Path(org.apache.hadoop.fs.Path) TableAlreadyExistsException(com.facebook.presto.hive.TableAlreadyExistsException) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) EnumSet(java.util.EnumSet) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) PrincipalPrivileges(com.facebook.presto.hive.metastore.PrincipalPrivileges) Collection(java.util.Collection) Set(java.util.Set) ThreadSafe(javax.annotation.concurrent.ThreadSafe) DEFAULT_DATABASE_NAME(com.facebook.presto.hive.metastore.Database.DEFAULT_DATABASE_NAME) MANAGED_TABLE(org.apache.hadoop.hive.metastore.TableType.MANAGED_TABLE) List(java.util.List) NOT_SUPPORTED(com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED) ByteStreams(com.google.common.io.ByteStreams) Entry(java.util.Map.Entry) USER(com.facebook.presto.hive.metastore.PrincipalType.USER) Optional(java.util.Optional) HivePrivilegeInfo(com.facebook.presto.hive.metastore.HivePrivilegeInfo) JsonCodec(io.airlift.json.JsonCodec) Table(com.facebook.presto.hive.metastore.Table) Column(com.facebook.presto.hive.metastore.Column) Database(com.facebook.presto.hive.metastore.Database) HiveType(com.facebook.presto.hive.HiveType) PrestoException(com.facebook.presto.spi.PrestoException) Function(java.util.function.Function) Partition(com.facebook.presto.hive.metastore.Partition) ArrayList(java.util.ArrayList) OWNERSHIP(com.facebook.presto.hive.metastore.HivePrivilegeInfo.HivePrivilege.OWNERSHIP) Inject(javax.inject.Inject) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) ImmutableList(com.google.common.collect.ImmutableList) ALREADY_EXISTS(com.facebook.presto.spi.StandardErrorCode.ALREADY_EXISTS) Objects.requireNonNull(java.util.Objects.requireNonNull) HiveUtil.toPartitionValues(com.facebook.presto.hive.HiveUtil.toPartitionValues) VIRTUAL_VIEW(org.apache.hadoop.hive.metastore.TableType.VIRTUAL_VIEW) LinkedHashSet(java.util.LinkedHashSet) OutputStream(java.io.OutputStream) ROLE(com.facebook.presto.hive.metastore.PrincipalType.ROLE) IOException(java.io.IOException) Collectors.toList(java.util.stream.Collectors.toList) TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) TableType(org.apache.hadoop.hive.metastore.TableType) ArrayDeque(java.util.ArrayDeque) Table(com.facebook.presto.hive.metastore.Table) ArrayDeque(java.util.ArrayDeque)

Example 18 with Table

use of com.facebook.presto.hive.metastore.Table in project presto by prestodb.

the class HiveMetadata method createTable.

@Override
public void createTable(ConnectorSession session, ConnectorTableMetadata tableMetadata) {
    SchemaTableName schemaTableName = tableMetadata.getTable();
    String schemaName = schemaTableName.getSchemaName();
    String tableName = schemaTableName.getTableName();
    List<String> partitionedBy = getPartitionedBy(tableMetadata.getProperties());
    Optional<HiveBucketProperty> bucketProperty = getBucketProperty(tableMetadata.getProperties());
    if (bucketProperty.isPresent() && !bucketWritingEnabled) {
        throw new PrestoException(NOT_SUPPORTED, "Writing to bucketed Hive table has been temporarily disabled");
    }
    List<HiveColumnHandle> columnHandles = getColumnHandles(connectorId, tableMetadata, ImmutableSet.copyOf(partitionedBy), typeTranslator);
    HiveStorageFormat hiveStorageFormat = getHiveStorageFormat(tableMetadata.getProperties());
    Map<String, String> additionalTableParameters = tableParameterCodec.encode(tableMetadata.getProperties());
    hiveStorageFormat.validateColumns(columnHandles);
    Path targetPath;
    boolean external;
    String externalLocation = getExternalLocation(tableMetadata.getProperties());
    if (externalLocation != null) {
        external = true;
        targetPath = getExternalPath(session.getUser(), externalLocation);
    } else {
        external = false;
        LocationHandle locationHandle = locationService.forNewTable(metastore, session.getUser(), session.getQueryId(), schemaName, tableName);
        targetPath = locationService.targetPathRoot(locationHandle);
    }
    Table table = buildTableObject(session.getQueryId(), schemaName, tableName, session.getUser(), columnHandles, hiveStorageFormat, partitionedBy, bucketProperty, additionalTableParameters, targetPath, external, prestoVersion);
    PrincipalPrivileges principalPrivileges = buildInitialPrivilegeSet(table.getOwner());
    metastore.createTable(session, table, principalPrivileges, Optional.empty());
}
Also used : Path(org.apache.hadoop.fs.Path) Table(com.facebook.presto.hive.metastore.Table) PrincipalPrivileges(com.facebook.presto.hive.metastore.PrincipalPrivileges) PrestoException(com.facebook.presto.spi.PrestoException) SchemaTableName(com.facebook.presto.spi.SchemaTableName) HiveTableProperties.getHiveStorageFormat(com.facebook.presto.hive.HiveTableProperties.getHiveStorageFormat) StorageFormat.fromHiveStorageFormat(com.facebook.presto.hive.metastore.StorageFormat.fromHiveStorageFormat)

Example 19 with Table

use of com.facebook.presto.hive.metastore.Table in project presto by prestodb.

the class HiveMetadata method dropTable.

@Override
public void dropTable(ConnectorSession session, ConnectorTableHandle tableHandle) {
    HiveTableHandle handle = (HiveTableHandle) tableHandle;
    SchemaTableName tableName = schemaTableName(tableHandle);
    Optional<Table> target = metastore.getTable(handle.getSchemaName(), handle.getTableName());
    if (!target.isPresent()) {
        throw new TableNotFoundException(tableName);
    }
    metastore.dropTable(session, handle.getSchemaName(), handle.getTableName());
}
Also used : TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) Table(com.facebook.presto.hive.metastore.Table) SchemaTableName(com.facebook.presto.spi.SchemaTableName)

Example 20 with Table

use of com.facebook.presto.hive.metastore.Table in project presto by prestodb.

the class HiveMetadata method finishInsert.

@Override
public Optional<ConnectorOutputMetadata> finishInsert(ConnectorSession session, ConnectorInsertTableHandle insertHandle, Collection<Slice> fragments) {
    HiveInsertTableHandle handle = (HiveInsertTableHandle) insertHandle;
    List<PartitionUpdate> partitionUpdates = fragments.stream().map(Slice::getBytes).map(partitionUpdateCodec::fromJson).collect(toList());
    HiveStorageFormat tableStorageFormat = handle.getTableStorageFormat();
    partitionUpdates = PartitionUpdate.mergePartitionUpdates(partitionUpdates);
    Optional<Table> table = metastore.getTable(handle.getSchemaName(), handle.getTableName());
    if (!table.isPresent()) {
        throw new TableNotFoundException(new SchemaTableName(handle.getSchemaName(), handle.getTableName()));
    }
    if (!table.get().getStorage().getStorageFormat().getInputFormat().equals(tableStorageFormat.getInputFormat()) && respectTableFormat) {
        throw new PrestoException(HIVE_CONCURRENT_MODIFICATION_DETECTED, "Table format changed during insert");
    }
    if (handle.getBucketProperty().isPresent()) {
        ImmutableList<PartitionUpdate> partitionUpdatesForMissingBuckets = computePartitionUpdatesForMissingBuckets(handle, table.get(), partitionUpdates);
        // replace partitionUpdates before creating the empty files so that those files will be cleaned up if we end up rollback
        partitionUpdates = PartitionUpdate.mergePartitionUpdates(Iterables.concat(partitionUpdates, partitionUpdatesForMissingBuckets));
        for (PartitionUpdate partitionUpdate : partitionUpdatesForMissingBuckets) {
            Optional<Partition> partition = table.get().getPartitionColumns().isEmpty() ? Optional.empty() : Optional.of(buildPartitionObject(session.getQueryId(), table.get(), partitionUpdate));
            createEmptyFile(partitionUpdate.getWritePath(), table.get(), partition, partitionUpdate.getFileNames());
        }
    }
    for (PartitionUpdate partitionUpdate : partitionUpdates) {
        if (partitionUpdate.getName().isEmpty()) {
            // insert into unpartitioned table
            metastore.finishInsertIntoExistingTable(session, handle.getSchemaName(), handle.getTableName(), partitionUpdate.getWritePath(), partitionUpdate.getFileNames());
        } else if (!partitionUpdate.isNew()) {
            // insert into existing partition
            metastore.finishInsertIntoExistingPartition(session, handle.getSchemaName(), handle.getTableName(), toPartitionValues(partitionUpdate.getName()), partitionUpdate.getWritePath(), partitionUpdate.getFileNames());
        } else {
            // insert into new partition
            Partition partition = buildPartitionObject(session.getQueryId(), table.get(), partitionUpdate);
            if (!partition.getStorage().getStorageFormat().getInputFormat().equals(handle.getPartitionStorageFormat().getInputFormat()) && respectTableFormat) {
                throw new PrestoException(HIVE_CONCURRENT_MODIFICATION_DETECTED, "Partition format changed during insert");
            }
            metastore.addPartition(session, handle.getSchemaName(), handle.getTableName(), partition, partitionUpdate.getWritePath());
        }
    }
    return Optional.of(new HiveWrittenPartitions(partitionUpdates.stream().map(PartitionUpdate::getName).collect(Collectors.toList())));
}
Also used : Partition(com.facebook.presto.hive.metastore.Partition) Table(com.facebook.presto.hive.metastore.Table) PrestoException(com.facebook.presto.spi.PrestoException) SchemaTableName(com.facebook.presto.spi.SchemaTableName) TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) HiveTableProperties.getHiveStorageFormat(com.facebook.presto.hive.HiveTableProperties.getHiveStorageFormat) StorageFormat.fromHiveStorageFormat(com.facebook.presto.hive.metastore.StorageFormat.fromHiveStorageFormat) Slice(io.airlift.slice.Slice)

Aggregations

Table (com.facebook.presto.hive.metastore.Table)33 Path (org.apache.hadoop.fs.Path)24 SchemaTableName (com.facebook.presto.spi.SchemaTableName)20 TableNotFoundException (com.facebook.presto.spi.TableNotFoundException)20 PrestoException (com.facebook.presto.spi.PrestoException)18 ImmutableMap (com.google.common.collect.ImmutableMap)17 PrincipalPrivileges (com.facebook.presto.hive.metastore.PrincipalPrivileges)16 Slice (io.airlift.slice.Slice)16 ColumnHandle (com.facebook.presto.spi.ColumnHandle)15 ImmutableList (com.google.common.collect.ImmutableList)15 Partition (com.facebook.presto.hive.metastore.Partition)14 ConnectorTableHandle (com.facebook.presto.spi.ConnectorTableHandle)14 ConnectorTableMetadata (com.facebook.presto.spi.ConnectorTableMetadata)14 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)14 Collection (java.util.Collection)14 List (java.util.List)14 Map (java.util.Map)14 Optional (java.util.Optional)14 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)13 ConnectorOutputTableHandle (com.facebook.presto.spi.ConnectorOutputTableHandle)13