Search in sources :

Example 31 with CatalogException

use of org.apache.flink.table.catalog.exceptions.CatalogException in project flink by apache.

the class HiveCatalog method createPartition.

@Override
public void createPartition(ObjectPath tablePath, CatalogPartitionSpec partitionSpec, CatalogPartition partition, boolean ignoreIfExists) throws TableNotExistException, TableNotPartitionedException, PartitionSpecInvalidException, PartitionAlreadyExistsException, CatalogException {
    checkNotNull(tablePath, "Table path cannot be null");
    checkNotNull(partitionSpec, "CatalogPartitionSpec cannot be null");
    checkNotNull(partition, "Partition cannot be null");
    Table hiveTable = getHiveTable(tablePath);
    ensurePartitionedTable(tablePath, hiveTable);
    // partition doesn't have connector property, so check the table
    boolean isHiveTable = isHiveTable(hiveTable.getParameters());
    if (!isHiveTable) {
        throw new CatalogException("Currently only supports partition for hive tables");
    }
    try {
        client.add_partition(instantiateHivePartition(hiveTable, partitionSpec, partition));
    } catch (AlreadyExistsException e) {
        if (!ignoreIfExists) {
            throw new PartitionAlreadyExistsException(getName(), tablePath, partitionSpec);
        }
    } catch (TException e) {
        throw new CatalogException(String.format("Failed to create partition %s of table %s", partitionSpec, tablePath), e);
    }
}
Also used : TException(org.apache.thrift.TException) CatalogTable(org.apache.flink.table.catalog.CatalogTable) SqlCreateHiveTable(org.apache.flink.sql.parser.hive.ddl.SqlCreateHiveTable) Table(org.apache.hadoop.hive.metastore.api.Table) CatalogBaseTable(org.apache.flink.table.catalog.CatalogBaseTable) AlreadyExistsException(org.apache.hadoop.hive.metastore.api.AlreadyExistsException) PartitionAlreadyExistsException(org.apache.flink.table.catalog.exceptions.PartitionAlreadyExistsException) CatalogException(org.apache.flink.table.catalog.exceptions.CatalogException) PartitionAlreadyExistsException(org.apache.flink.table.catalog.exceptions.PartitionAlreadyExistsException)

Example 32 with CatalogException

use of org.apache.flink.table.catalog.exceptions.CatalogException in project flink by apache.

the class HiveCatalog method alterTableStatistics.

// ------ stats ------
@Override
public void alterTableStatistics(ObjectPath tablePath, CatalogTableStatistics tableStatistics, boolean ignoreIfNotExists) throws TableNotExistException, CatalogException {
    try {
        Table hiveTable = getHiveTable(tablePath);
        // versions, so error out
        if (!isTablePartitioned(hiveTable) && hiveVersion.compareTo("1.2.1") < 0) {
            throw new CatalogException("Alter table stats is not supported in Hive version " + hiveVersion);
        }
        // Set table stats
        if (statsChanged(tableStatistics, hiveTable.getParameters())) {
            updateStats(tableStatistics, hiveTable.getParameters());
            client.alter_table(tablePath.getDatabaseName(), tablePath.getObjectName(), hiveTable);
        }
    } catch (TableNotExistException e) {
        if (!ignoreIfNotExists) {
            throw e;
        }
    } catch (TException e) {
        throw new CatalogException(String.format("Failed to alter table stats of table %s", tablePath.getFullName()), e);
    }
}
Also used : TException(org.apache.thrift.TException) CatalogTable(org.apache.flink.table.catalog.CatalogTable) SqlCreateHiveTable(org.apache.flink.sql.parser.hive.ddl.SqlCreateHiveTable) Table(org.apache.hadoop.hive.metastore.api.Table) CatalogBaseTable(org.apache.flink.table.catalog.CatalogBaseTable) TableNotExistException(org.apache.flink.table.catalog.exceptions.TableNotExistException) CatalogException(org.apache.flink.table.catalog.exceptions.CatalogException)

Example 33 with CatalogException

use of org.apache.flink.table.catalog.exceptions.CatalogException in project flink by apache.

the class HiveCatalog method getHiveTable.

@VisibleForTesting
public Table getHiveTable(ObjectPath tablePath) throws TableNotExistException {
    try {
        Table table = client.getTable(tablePath.getDatabaseName(), tablePath.getObjectName());
        boolean isHiveTable;
        if (table.getParameters().containsKey(CatalogPropertiesUtil.IS_GENERIC)) {
            isHiveTable = !Boolean.parseBoolean(table.getParameters().remove(CatalogPropertiesUtil.IS_GENERIC));
        } else {
            isHiveTable = !table.getParameters().containsKey(FLINK_PROPERTY_PREFIX + CONNECTOR.key()) && !table.getParameters().containsKey(FLINK_PROPERTY_PREFIX + CONNECTOR_TYPE);
        }
        // for hive table, we add the connector property
        if (isHiveTable) {
            table.getParameters().put(CONNECTOR.key(), IDENTIFIER);
        }
        return table;
    } catch (NoSuchObjectException e) {
        throw new TableNotExistException(getName(), tablePath);
    } catch (TException e) {
        throw new CatalogException(String.format("Failed to get table %s from Hive metastore", tablePath.getFullName()), e);
    }
}
Also used : TException(org.apache.thrift.TException) CatalogTable(org.apache.flink.table.catalog.CatalogTable) SqlCreateHiveTable(org.apache.flink.sql.parser.hive.ddl.SqlCreateHiveTable) Table(org.apache.hadoop.hive.metastore.api.Table) CatalogBaseTable(org.apache.flink.table.catalog.CatalogBaseTable) TableNotExistException(org.apache.flink.table.catalog.exceptions.TableNotExistException) CatalogException(org.apache.flink.table.catalog.exceptions.CatalogException) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) VisibleForTesting(org.apache.flink.annotation.VisibleForTesting)

Example 34 with CatalogException

use of org.apache.flink.table.catalog.exceptions.CatalogException in project flink by apache.

the class HiveCatalog method alterDatabase.

@Override
public void alterDatabase(String databaseName, CatalogDatabase newDatabase, boolean ignoreIfNotExists) throws DatabaseNotExistException, CatalogException {
    checkArgument(!isNullOrWhitespaceOnly(databaseName), "databaseName cannot be null or empty");
    checkNotNull(newDatabase, "newDatabase cannot be null");
    // client.alterDatabase doesn't throw any exception if there is no existing database
    Database hiveDB;
    try {
        hiveDB = getHiveDatabase(databaseName);
    } catch (DatabaseNotExistException e) {
        if (!ignoreIfNotExists) {
            throw new DatabaseNotExistException(getName(), databaseName);
        }
        return;
    }
    try {
        client.alterDatabase(databaseName, alterDatabase(hiveDB, newDatabase));
    } catch (TException e) {
        throw new CatalogException(String.format("Failed to alter database %s", databaseName), e);
    }
}
Also used : TException(org.apache.thrift.TException) CatalogDatabase(org.apache.flink.table.catalog.CatalogDatabase) SqlAlterHiveDatabase(org.apache.flink.sql.parser.hive.ddl.SqlAlterHiveDatabase) SqlCreateHiveDatabase(org.apache.flink.sql.parser.hive.ddl.SqlCreateHiveDatabase) Database(org.apache.hadoop.hive.metastore.api.Database) CatalogException(org.apache.flink.table.catalog.exceptions.CatalogException) DatabaseNotExistException(org.apache.flink.table.catalog.exceptions.DatabaseNotExistException)

Example 35 with CatalogException

use of org.apache.flink.table.catalog.exceptions.CatalogException in project flink by apache.

the class HiveCatalog method createTable.

@Override
public void createTable(ObjectPath tablePath, CatalogBaseTable table, boolean ignoreIfExists) throws TableAlreadyExistException, DatabaseNotExistException, CatalogException {
    checkNotNull(tablePath, "tablePath cannot be null");
    checkNotNull(table, "table cannot be null");
    if (!databaseExists(tablePath.getDatabaseName())) {
        throw new DatabaseNotExistException(getName(), tablePath.getDatabaseName());
    }
    boolean managedTable = ManagedTableListener.isManagedTable(this, table);
    Table hiveTable = HiveTableUtil.instantiateHiveTable(tablePath, table, hiveConf, managedTable);
    UniqueConstraint pkConstraint = null;
    List<String> notNullCols = new ArrayList<>();
    boolean isHiveTable = isHiveTable(table.getOptions());
    if (isHiveTable) {
        pkConstraint = table.getSchema().getPrimaryKey().orElse(null);
        String nnColStr = hiveTable.getParameters().remove(NOT_NULL_COLS);
        if (nnColStr != null) {
            notNullCols.addAll(Arrays.asList(nnColStr.split(HiveDDLUtils.COL_DELIMITER)));
        } else {
            for (int i = 0; i < table.getSchema().getFieldDataTypes().length; i++) {
                if (!table.getSchema().getFieldDataTypes()[i].getLogicalType().isNullable()) {
                    notNullCols.add(table.getSchema().getFieldNames()[i]);
                }
            }
        }
        // remove the 'connector' option for hive table
        hiveTable.getParameters().remove(CONNECTOR.key());
    }
    try {
        if (pkConstraint != null || !notNullCols.isEmpty()) {
            // extract constraint traits from table properties
            String pkTraitStr = hiveTable.getParameters().remove(PK_CONSTRAINT_TRAIT);
            byte pkTrait = pkTraitStr == null ? HiveDDLUtils.defaultTrait() : Byte.parseByte(pkTraitStr);
            List<Byte> pkTraits = Collections.nCopies(pkConstraint == null ? 0 : pkConstraint.getColumns().size(), pkTrait);
            List<Byte> nnTraits;
            String nnTraitsStr = hiveTable.getParameters().remove(NOT_NULL_CONSTRAINT_TRAITS);
            if (nnTraitsStr != null) {
                String[] traits = nnTraitsStr.split(HiveDDLUtils.COL_DELIMITER);
                Preconditions.checkArgument(traits.length == notNullCols.size(), "Number of NOT NULL columns and constraint traits mismatch");
                nnTraits = Arrays.stream(traits).map(Byte::new).collect(Collectors.toList());
            } else {
                nnTraits = Collections.nCopies(notNullCols.size(), HiveDDLUtils.defaultTrait());
            }
            client.createTableWithConstraints(hiveTable, hiveConf, pkConstraint, pkTraits, notNullCols, nnTraits);
        } else {
            client.createTable(hiveTable);
        }
    } catch (AlreadyExistsException e) {
        if (!ignoreIfExists) {
            throw new TableAlreadyExistException(getName(), tablePath, e);
        }
    } catch (TException e) {
        throw new CatalogException(String.format("Failed to create table %s", tablePath.getFullName()), e);
    }
}
Also used : TException(org.apache.thrift.TException) CatalogTable(org.apache.flink.table.catalog.CatalogTable) SqlCreateHiveTable(org.apache.flink.sql.parser.hive.ddl.SqlCreateHiveTable) Table(org.apache.hadoop.hive.metastore.api.Table) CatalogBaseTable(org.apache.flink.table.catalog.CatalogBaseTable) AlreadyExistsException(org.apache.hadoop.hive.metastore.api.AlreadyExistsException) PartitionAlreadyExistsException(org.apache.flink.table.catalog.exceptions.PartitionAlreadyExistsException) ArrayList(java.util.ArrayList) CatalogException(org.apache.flink.table.catalog.exceptions.CatalogException) UniqueConstraint(org.apache.flink.table.api.constraints.UniqueConstraint) UniqueConstraint(org.apache.flink.table.api.constraints.UniqueConstraint) TableAlreadyExistException(org.apache.flink.table.catalog.exceptions.TableAlreadyExistException) DatabaseNotExistException(org.apache.flink.table.catalog.exceptions.DatabaseNotExistException)

Aggregations

CatalogException (org.apache.flink.table.catalog.exceptions.CatalogException)53 TException (org.apache.thrift.TException)28 TableNotExistException (org.apache.flink.table.catalog.exceptions.TableNotExistException)16 Table (org.apache.hadoop.hive.metastore.api.Table)15 InvocationTargetException (java.lang.reflect.InvocationTargetException)14 CatalogTable (org.apache.flink.table.catalog.CatalogTable)14 Method (java.lang.reflect.Method)13 CatalogBaseTable (org.apache.flink.table.catalog.CatalogBaseTable)13 SqlCreateHiveTable (org.apache.flink.sql.parser.hive.ddl.SqlCreateHiveTable)12 PartitionNotExistException (org.apache.flink.table.catalog.exceptions.PartitionNotExistException)9 PartitionSpecInvalidException (org.apache.flink.table.catalog.exceptions.PartitionSpecInvalidException)9 MetaException (org.apache.hadoop.hive.metastore.api.MetaException)9 ArrayList (java.util.ArrayList)8 List (java.util.List)8 FlinkHiveException (org.apache.flink.connectors.hive.FlinkHiveException)8 DatabaseNotExistException (org.apache.flink.table.catalog.exceptions.DatabaseNotExistException)8 CatalogPartition (org.apache.flink.table.catalog.CatalogPartition)7 NoSuchObjectException (org.apache.hadoop.hive.metastore.api.NoSuchObjectException)7 PartitionAlreadyExistsException (org.apache.flink.table.catalog.exceptions.PartitionAlreadyExistsException)6 InvalidOperationException (org.apache.hadoop.hive.metastore.api.InvalidOperationException)6