Search in sources :

Example 11 with TableMetadata

use of org.apache.iceberg.TableMetadata in project presto by prestodb.

the class IcebergHiveMetadata method beginCreateTable.

@Override
public ConnectorOutputTableHandle beginCreateTable(ConnectorSession session, ConnectorTableMetadata tableMetadata, Optional<ConnectorNewTableLayout> layout) {
    SchemaTableName schemaTableName = tableMetadata.getTable();
    String schemaName = schemaTableName.getSchemaName();
    String tableName = schemaTableName.getTableName();
    Schema schema = toIcebergSchema(tableMetadata.getColumns());
    PartitionSpec partitionSpec = parsePartitionFields(schema, getPartitioning(tableMetadata.getProperties()));
    MetastoreContext metastoreContext = new MetastoreContext(session.getIdentity(), session.getQueryId(), session.getClientInfo(), session.getSource(), Optional.empty(), false, HiveColumnConverterProvider.DEFAULT_COLUMN_CONVERTER_PROVIDER);
    Database database = metastore.getDatabase(metastoreContext, schemaName).orElseThrow(() -> new SchemaNotFoundException(schemaName));
    HdfsContext hdfsContext = new HdfsContext(session, schemaName, tableName);
    String targetPath = getTableLocation(tableMetadata.getProperties());
    if (targetPath == null) {
        Optional<String> location = database.getLocation();
        if (!location.isPresent() || location.get().isEmpty()) {
            throw new PrestoException(NOT_SUPPORTED, "Database " + schemaName + " location is not set");
        }
        Path databasePath = new Path(location.get());
        Path resultPath = new Path(databasePath, tableName);
        targetPath = resultPath.toString();
    }
    TableOperations operations = new HiveTableOperations(metastore, new MetastoreContext(session.getIdentity(), session.getQueryId(), session.getClientInfo(), session.getSource(), Optional.empty(), false, HiveColumnConverterProvider.DEFAULT_COLUMN_CONVERTER_PROVIDER), hdfsEnvironment, hdfsContext, schemaName, tableName, session.getUser(), targetPath);
    if (operations.current() != null) {
        throw new TableAlreadyExistsException(schemaTableName);
    }
    ImmutableMap.Builder<String, String> propertiesBuilder = ImmutableMap.builderWithExpectedSize(2);
    FileFormat fileFormat = getFileFormat(tableMetadata.getProperties());
    propertiesBuilder.put(DEFAULT_FILE_FORMAT, fileFormat.toString());
    if (tableMetadata.getComment().isPresent()) {
        propertiesBuilder.put(TABLE_COMMENT, tableMetadata.getComment().get());
    }
    TableMetadata metadata = newTableMetadata(schema, partitionSpec, targetPath, propertiesBuilder.build());
    transaction = createTableTransaction(tableName, operations, metadata);
    return new IcebergWritableTableHandle(schemaName, tableName, SchemaParser.toJson(metadata.schema()), PartitionSpecParser.toJson(metadata.spec()), getColumns(metadata.schema(), typeManager), targetPath, fileFormat, metadata.properties());
}
Also used : Path(org.apache.hadoop.fs.Path) TableMetadata(org.apache.iceberg.TableMetadata) TableMetadata.newTableMetadata(org.apache.iceberg.TableMetadata.newTableMetadata) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) TableAlreadyExistsException(com.facebook.presto.hive.TableAlreadyExistsException) Schema(org.apache.iceberg.Schema) MetastoreContext(com.facebook.presto.hive.metastore.MetastoreContext) PrestoException(com.facebook.presto.spi.PrestoException) FileFormat(org.apache.iceberg.FileFormat) IcebergTableProperties.getFileFormat(com.facebook.presto.iceberg.IcebergTableProperties.getFileFormat) SchemaTableName(com.facebook.presto.spi.SchemaTableName) PartitionSpec(org.apache.iceberg.PartitionSpec) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) TableOperations(org.apache.iceberg.TableOperations) Database(com.facebook.presto.hive.metastore.Database) SchemaNotFoundException(com.facebook.presto.spi.SchemaNotFoundException) HdfsContext(com.facebook.presto.hive.HdfsContext)

Aggregations

TableMetadata (org.apache.iceberg.TableMetadata)11 Table (org.apache.iceberg.Table)7 Test (org.junit.Test)5 TableOperations (org.apache.iceberg.TableOperations)4 HdfsContext (com.facebook.presto.hive.HdfsContext)2 MetastoreContext (com.facebook.presto.hive.metastore.MetastoreContext)2 PrestoException (com.facebook.presto.spi.PrestoException)2 SchemaTableName (com.facebook.presto.spi.SchemaTableName)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 Logger (com.facebook.airlift.log.Logger)1 HdfsEnvironment (com.facebook.presto.hive.HdfsEnvironment)1 TABLE_COMMENT (com.facebook.presto.hive.HiveMetadata.TABLE_COMMENT)1 HiveType (com.facebook.presto.hive.HiveType)1 TableAlreadyExistsException (com.facebook.presto.hive.TableAlreadyExistsException)1 Column (com.facebook.presto.hive.metastore.Column)1 Database (com.facebook.presto.hive.metastore.Database)1 ExtendedHiveMetastore (com.facebook.presto.hive.metastore.ExtendedHiveMetastore)1 HivePrivilegeInfo (com.facebook.presto.hive.metastore.HivePrivilegeInfo)1 DELETE (com.facebook.presto.hive.metastore.HivePrivilegeInfo.HivePrivilege.DELETE)1 INSERT (com.facebook.presto.hive.metastore.HivePrivilegeInfo.HivePrivilege.INSERT)1