Search in sources :

Example 26 with CatalogException

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

the class HiveCatalog method listPartitions.

@Override
public List<CatalogPartitionSpec> listPartitions(ObjectPath tablePath) throws TableNotExistException, TableNotPartitionedException, CatalogException {
    checkNotNull(tablePath, "Table path cannot be null");
    Table hiveTable = getHiveTable(tablePath);
    ensurePartitionedTable(tablePath, hiveTable);
    try {
        // pass -1 as max_parts to fetch all partitions
        return client.listPartitionNames(tablePath.getDatabaseName(), tablePath.getObjectName(), (short) -1).stream().map(HiveCatalog::createPartitionSpec).collect(Collectors.toList());
    } catch (TException e) {
        throw new CatalogException(String.format("Failed to list partitions of table %s", 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) CatalogException(org.apache.flink.table.catalog.exceptions.CatalogException)

Example 27 with CatalogException

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

the class HiveCatalog method alterTableViaProperties.

private void alterTableViaProperties(AlterTableOp alterOp, Table hiveTable, CatalogTable catalogTable, Map<String, String> oldProps, Map<String, String> newProps, StorageDescriptor sd) {
    switch(alterOp) {
        case CHANGE_TBL_PROPS:
            oldProps.putAll(newProps);
            break;
        case CHANGE_LOCATION:
            HiveTableUtil.extractLocation(sd, newProps);
            break;
        case CHANGE_FILE_FORMAT:
            String newFileFormat = newProps.remove(STORED_AS_FILE_FORMAT);
            HiveTableUtil.setStorageFormat(sd, newFileFormat, hiveConf);
            break;
        case CHANGE_SERDE_PROPS:
            HiveTableUtil.extractRowFormat(sd, newProps);
            break;
        case ALTER_COLUMNS:
            if (hiveTable == null) {
                throw new CatalogException("ALTER COLUMNS cannot be done with ALTER PARTITION");
            }
            HiveTableUtil.alterColumns(hiveTable.getSd(), catalogTable);
            boolean cascade = Boolean.parseBoolean(newProps.remove(ALTER_COL_CASCADE));
            if (cascade) {
                if (!isTablePartitioned(hiveTable)) {
                    throw new CatalogException("ALTER COLUMNS CASCADE for non-partitioned table");
                }
                try {
                    for (CatalogPartitionSpec spec : listPartitions(new ObjectPath(hiveTable.getDbName(), hiveTable.getTableName()))) {
                        Partition partition = getHivePartition(hiveTable, spec);
                        HiveTableUtil.alterColumns(partition.getSd(), catalogTable);
                        client.alter_partition(hiveTable.getDbName(), hiveTable.getTableName(), partition);
                    }
                } catch (Exception e) {
                    throw new CatalogException("Failed to cascade add/replace columns to partitions", e);
                }
            }
            break;
        default:
            throw new CatalogException("Unsupported alter table operation " + alterOp);
    }
}
Also used : Partition(org.apache.hadoop.hive.metastore.api.Partition) CatalogPartition(org.apache.flink.table.catalog.CatalogPartition) ObjectPath(org.apache.flink.table.catalog.ObjectPath) CatalogException(org.apache.flink.table.catalog.exceptions.CatalogException) CatalogPartitionSpec(org.apache.flink.table.catalog.CatalogPartitionSpec) FunctionAlreadyExistException(org.apache.flink.table.catalog.exceptions.FunctionAlreadyExistException) PartitionNotExistException(org.apache.flink.table.catalog.exceptions.PartitionNotExistException) PartitionSpecInvalidException(org.apache.flink.table.catalog.exceptions.PartitionSpecInvalidException) AlreadyExistsException(org.apache.hadoop.hive.metastore.api.AlreadyExistsException) TablePartitionedException(org.apache.flink.table.catalog.exceptions.TablePartitionedException) InvalidOperationException(org.apache.hadoop.hive.metastore.api.InvalidOperationException) DatabaseNotExistException(org.apache.flink.table.catalog.exceptions.DatabaseNotExistException) TableAlreadyExistException(org.apache.flink.table.catalog.exceptions.TableAlreadyExistException) UnknownDBException(org.apache.hadoop.hive.metastore.api.UnknownDBException) TException(org.apache.thrift.TException) IOException(java.io.IOException) CatalogException(org.apache.flink.table.catalog.exceptions.CatalogException) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) MetaException(org.apache.hadoop.hive.metastore.api.MetaException) FunctionNotExistException(org.apache.flink.table.catalog.exceptions.FunctionNotExistException) DatabaseNotEmptyException(org.apache.flink.table.catalog.exceptions.DatabaseNotEmptyException) DatabaseAlreadyExistException(org.apache.flink.table.catalog.exceptions.DatabaseAlreadyExistException) FileNotFoundException(java.io.FileNotFoundException) TableNotPartitionedException(org.apache.flink.table.catalog.exceptions.TableNotPartitionedException) PartitionAlreadyExistsException(org.apache.flink.table.catalog.exceptions.PartitionAlreadyExistsException) TableNotExistException(org.apache.flink.table.catalog.exceptions.TableNotExistException)

Example 28 with CatalogException

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

the class HiveCatalog method alterFunction.

@Override
public void alterFunction(ObjectPath functionPath, CatalogFunction newFunction, boolean ignoreIfNotExists) throws FunctionNotExistException, CatalogException {
    checkNotNull(functionPath, "functionPath cannot be null");
    checkNotNull(newFunction, "newFunction cannot be null");
    try {
        // check if function exists
        getFunction(functionPath);
        Function hiveFunction;
        if (newFunction instanceof CatalogFunctionImpl) {
            hiveFunction = instantiateHiveFunction(functionPath, newFunction);
        } else {
            throw new CatalogException(String.format("Unsupported catalog function type %s", newFunction.getClass().getName()));
        }
        client.alterFunction(functionPath.getDatabaseName(), functionPath.getObjectName(), hiveFunction);
    } catch (FunctionNotExistException e) {
        if (!ignoreIfNotExists) {
            throw e;
        }
    } catch (TException e) {
        throw new CatalogException(String.format("Failed to alter function %s", functionPath.getFullName()), e);
    }
}
Also used : FunctionNotExistException(org.apache.flink.table.catalog.exceptions.FunctionNotExistException) TException(org.apache.thrift.TException) CatalogFunction(org.apache.flink.table.catalog.CatalogFunction) Function(org.apache.hadoop.hive.metastore.api.Function) CatalogException(org.apache.flink.table.catalog.exceptions.CatalogException) CatalogFunctionImpl(org.apache.flink.table.catalog.CatalogFunctionImpl)

Example 29 with CatalogException

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

the class HiveCatalog method alterPartition.

@Override
public void alterPartition(ObjectPath tablePath, CatalogPartitionSpec partitionSpec, CatalogPartition newPartition, boolean ignoreIfNotExists) throws PartitionNotExistException, CatalogException {
    checkNotNull(tablePath, "Table path cannot be null");
    checkNotNull(partitionSpec, "CatalogPartitionSpec cannot be null");
    checkNotNull(newPartition, "New partition cannot be null");
    // the target doesn't exist
    try {
        Table hiveTable = getHiveTable(tablePath);
        boolean isHiveTable = isHiveTable(hiveTable.getParameters());
        if (!isHiveTable) {
            throw new CatalogException("Currently only supports partition for hive tables");
        }
        Partition hivePartition = getHivePartition(hiveTable, partitionSpec);
        if (hivePartition == null) {
            if (ignoreIfNotExists) {
                return;
            }
            throw new PartitionNotExistException(getName(), tablePath, partitionSpec);
        }
        AlterTableOp op = HiveTableUtil.extractAlterTableOp(newPartition.getProperties());
        if (op == null) {
            throw new CatalogException(ALTER_TABLE_OP + " is missing for alter table operation");
        }
        alterTableViaProperties(op, null, null, hivePartition.getParameters(), newPartition.getProperties(), hivePartition.getSd());
        client.alter_partition(tablePath.getDatabaseName(), tablePath.getObjectName(), hivePartition);
    } catch (NoSuchObjectException e) {
        if (!ignoreIfNotExists) {
            throw new PartitionNotExistException(getName(), tablePath, partitionSpec, e);
        }
    } catch (InvalidOperationException | MetaException | TableNotExistException | PartitionSpecInvalidException e) {
        throw new PartitionNotExistException(getName(), tablePath, partitionSpec, e);
    } catch (TException e) {
        throw new CatalogException(String.format("Failed to alter existing partition with new partition %s of table %s", partitionSpec, tablePath), e);
    }
}
Also used : TException(org.apache.thrift.TException) Partition(org.apache.hadoop.hive.metastore.api.Partition) CatalogPartition(org.apache.flink.table.catalog.CatalogPartition) 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) AlterTableOp(org.apache.flink.sql.parser.hive.ddl.SqlAlterHiveTable.AlterTableOp) InvalidOperationException(org.apache.hadoop.hive.metastore.api.InvalidOperationException) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) PartitionNotExistException(org.apache.flink.table.catalog.exceptions.PartitionNotExistException) PartitionSpecInvalidException(org.apache.flink.table.catalog.exceptions.PartitionSpecInvalidException) MetaException(org.apache.hadoop.hive.metastore.api.MetaException)

Example 30 with CatalogException

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

the class HiveCatalog method renameTable.

@Override
public void renameTable(ObjectPath tablePath, String newTableName, boolean ignoreIfNotExists) throws TableNotExistException, TableAlreadyExistException, CatalogException {
    checkNotNull(tablePath, "tablePath cannot be null");
    checkArgument(!isNullOrWhitespaceOnly(newTableName), "newTableName cannot be null or empty");
    try {
        // Thus, check the table existence explicitly
        if (tableExists(tablePath)) {
            ObjectPath newPath = new ObjectPath(tablePath.getDatabaseName(), newTableName);
            // Thus, check the table existence explicitly
            if (tableExists(newPath)) {
                throw new TableAlreadyExistException(getName(), newPath);
            } else {
                Table table = getHiveTable(tablePath);
                table.setTableName(newTableName);
                client.alter_table(tablePath.getDatabaseName(), tablePath.getObjectName(), table);
            }
        } else if (!ignoreIfNotExists) {
            throw new TableNotExistException(getName(), tablePath);
        }
    } catch (TException e) {
        throw new CatalogException(String.format("Failed to rename table %s", tablePath.getFullName()), e);
    }
}
Also used : TException(org.apache.thrift.TException) ObjectPath(org.apache.flink.table.catalog.ObjectPath) TableAlreadyExistException(org.apache.flink.table.catalog.exceptions.TableAlreadyExistException) 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)

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