Search in sources :

Example 26 with Table

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

the class FileHiveMetastore method getTablePrivileges.

@Override
public synchronized Set<HivePrivilegeInfo> getTablePrivileges(String user, String databaseName, String tableName) {
    Table table = getRequiredTable(databaseName, tableName);
    Set<HivePrivilegeInfo> privileges = new HashSet<>();
    if (user.equals(table.getOwner())) {
        privileges.add(new HivePrivilegeInfo(OWNERSHIP, true));
    }
    Path permissionsDirectory = getPermissionsDirectory(table);
    privileges.addAll(getTablePrivileges(permissionsDirectory, user, USER));
    for (String role : getRoles(user)) {
        privileges.addAll(getTablePrivileges(permissionsDirectory, role, ROLE));
    }
    return privileges;
}
Also used : Path(org.apache.hadoop.fs.Path) HivePrivilegeInfo(com.facebook.presto.hive.metastore.HivePrivilegeInfo) Table(com.facebook.presto.hive.metastore.Table) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 27 with Table

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

the class FileHiveMetastore method replaceTable.

@Override
public synchronized void replaceTable(String databaseName, String tableName, Table newTable, PrincipalPrivileges principalPrivileges) {
    Table table = getRequiredTable(databaseName, tableName);
    if (!table.getTableType().equals(VIRTUAL_VIEW.name()) || !newTable.getTableType().equals(VIRTUAL_VIEW.name())) {
        throw new PrestoException(HIVE_METASTORE_ERROR, "Only views can be updated with replaceTable");
    }
    if (!table.getDatabaseName().equals(databaseName) || !table.getTableName().equals(tableName)) {
        throw new PrestoException(HIVE_METASTORE_ERROR, "Replacement table must have same name");
    }
    Path tableMetadataDirectory = getTableMetadataDirectory(table);
    writeSchemaFile("table", tableMetadataDirectory, tableCodec, new TableMetadata(newTable), true);
    // replace existing permissions
    deleteTablePrivileges(table);
    for (Entry<String, Collection<HivePrivilegeInfo>> entry : principalPrivileges.getUserPrivileges().asMap().entrySet()) {
        setTablePrivileges(entry.getKey(), USER, table.getDatabaseName(), table.getTableName(), entry.getValue());
    }
    for (Entry<String, Collection<HivePrivilegeInfo>> entry : principalPrivileges.getRolePrivileges().asMap().entrySet()) {
        setTablePrivileges(entry.getKey(), ROLE, table.getDatabaseName(), table.getTableName(), entry.getValue());
    }
}
Also used : Path(org.apache.hadoop.fs.Path) Table(com.facebook.presto.hive.metastore.Table) Collection(java.util.Collection) PrestoException(com.facebook.presto.spi.PrestoException)

Example 28 with Table

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

the class FileHiveMetastore method alterPartition.

@Override
public synchronized void alterPartition(String databaseName, String tableName, Partition partition) {
    Table table = getRequiredTable(databaseName, tableName);
    verifiedPartition(table, partition);
    Path partitionMetadataDirectory = getPartitionMetadataDirectory(table, partition.getValues());
    writeSchemaFile("partition", partitionMetadataDirectory, partitionCodec, new PartitionMetadata(table, partition), true);
}
Also used : Path(org.apache.hadoop.fs.Path) Table(com.facebook.presto.hive.metastore.Table)

Example 29 with Table

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

the class FileHiveMetastore method dropTable.

@Override
public synchronized void dropTable(String databaseName, String tableName, boolean deleteData) {
    requireNonNull(databaseName, "databaseName is null");
    requireNonNull(tableName, "tableName is null");
    Table table = getRequiredTable(databaseName, tableName);
    Path tableMetadataDirectory = getTableMetadataDirectory(databaseName, tableName);
    // It is safe to delete the whole meta directory for external tables and views
    if (!table.getTableType().equals(MANAGED_TABLE.name()) || deleteData) {
        deleteMetadataDirectory(tableMetadataDirectory);
    } else {
        // in this case we only wan to delete the metadata of a managed table
        deleteSchemaFile("table", tableMetadataDirectory);
        deleteTablePrivileges(table);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) Table(com.facebook.presto.hive.metastore.Table)

Example 30 with Table

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

the class FileHiveMetastore method addPartitions.

@Override
public synchronized void addPartitions(String databaseName, String tableName, List<Partition> partitions) {
    requireNonNull(databaseName, "databaseName is null");
    requireNonNull(tableName, "tableName is null");
    requireNonNull(partitions, "partitions is null");
    Table table = getRequiredTable(databaseName, tableName);
    TableType tableType = TableType.valueOf(table.getTableType());
    checkArgument(EnumSet.of(MANAGED_TABLE, EXTERNAL_TABLE).contains(tableType), "Invalid table type: %s", tableType);
    try {
        Map<Path, byte[]> schemaFiles = new LinkedHashMap<>();
        for (Partition partition : partitions) {
            verifiedPartition(table, partition);
            Path partitionMetadataDirectory = getPartitionMetadataDirectory(table, partition.getValues());
            Path schemaPath = new Path(partitionMetadataDirectory, PRESTO_SCHEMA_FILE_NAME);
            if (metadataFileSystem.exists(schemaPath)) {
                throw new PrestoException(HIVE_METASTORE_ERROR, "Partition already exists");
            }
            byte[] schemaJson = partitionCodec.toJsonBytes(new PartitionMetadata(table, partition));
            schemaFiles.put(schemaPath, schemaJson);
        }
        Set<Path> createdFiles = new LinkedHashSet<>();
        try {
            for (Entry<Path, byte[]> entry : schemaFiles.entrySet()) {
                try (OutputStream outputStream = metadataFileSystem.create(entry.getKey())) {
                    createdFiles.add(entry.getKey());
                    outputStream.write(entry.getValue());
                } catch (IOException e) {
                    throw new PrestoException(HIVE_METASTORE_ERROR, "Could not write partition schema", e);
                }
            }
        } catch (Throwable e) {
            for (Path createdFile : createdFiles) {
                try {
                    metadataFileSystem.delete(createdFile, false);
                } catch (IOException ignored) {
                }
            }
            throw e;
        }
    } catch (IOException e) {
        throw new PrestoException(HIVE_METASTORE_ERROR, e);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) LinkedHashSet(java.util.LinkedHashSet) Partition(com.facebook.presto.hive.metastore.Partition) Table(com.facebook.presto.hive.metastore.Table) TableType(org.apache.hadoop.hive.metastore.TableType) OutputStream(java.io.OutputStream) PrestoException(com.facebook.presto.spi.PrestoException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap)

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