Search in sources :

Example 16 with TableNotExistException

use of org.apache.flink.table.catalog.exceptions.TableNotExistException 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 17 with TableNotExistException

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

the class AbstractJdbcCatalog method getTable.

// ------ tables and views ------
@Override
public CatalogBaseTable getTable(ObjectPath tablePath) throws TableNotExistException, CatalogException {
    if (!tableExists(tablePath)) {
        throw new TableNotExistException(getName(), tablePath);
    }
    String dbUrl = baseUrl + tablePath.getDatabaseName();
    try (Connection conn = DriverManager.getConnection(dbUrl, username, pwd)) {
        DatabaseMetaData metaData = conn.getMetaData();
        Optional<UniqueConstraint> primaryKey = getPrimaryKey(metaData, getSchemaName(tablePath), getTableName(tablePath));
        PreparedStatement ps = conn.prepareStatement(String.format("SELECT * FROM %s;", getSchemaTableName(tablePath)));
        ResultSetMetaData resultSetMetaData = ps.getMetaData();
        String[] columnNames = new String[resultSetMetaData.getColumnCount()];
        DataType[] types = new DataType[resultSetMetaData.getColumnCount()];
        for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) {
            columnNames[i - 1] = resultSetMetaData.getColumnName(i);
            types[i - 1] = fromJDBCType(tablePath, resultSetMetaData, i);
            if (resultSetMetaData.isNullable(i) == ResultSetMetaData.columnNoNulls) {
                types[i - 1] = types[i - 1].notNull();
            }
        }
        Schema.Builder schemaBuilder = Schema.newBuilder().fromFields(columnNames, types);
        primaryKey.ifPresent(pk -> schemaBuilder.primaryKeyNamed(pk.getName(), pk.getColumns()));
        Schema tableSchema = schemaBuilder.build();
        Map<String, String> props = new HashMap<>();
        props.put(CONNECTOR.key(), IDENTIFIER);
        props.put(URL.key(), dbUrl);
        props.put(USERNAME.key(), username);
        props.put(PASSWORD.key(), pwd);
        props.put(TABLE_NAME.key(), getSchemaTableName(tablePath));
        return CatalogTable.of(tableSchema, null, Lists.newArrayList(), props);
    } catch (Exception e) {
        throw new CatalogException(String.format("Failed getting table %s", tablePath.getFullName()), e);
    }
}
Also used : HashMap(java.util.HashMap) TableNotExistException(org.apache.flink.table.catalog.exceptions.TableNotExistException) Schema(org.apache.flink.table.api.Schema) Connection(java.sql.Connection) CatalogException(org.apache.flink.table.catalog.exceptions.CatalogException) UniqueConstraint(org.apache.flink.table.catalog.UniqueConstraint) PreparedStatement(java.sql.PreparedStatement) DatabaseMetaData(java.sql.DatabaseMetaData) UniqueConstraint(org.apache.flink.table.catalog.UniqueConstraint) FunctionAlreadyExistException(org.apache.flink.table.catalog.exceptions.FunctionAlreadyExistException) PartitionNotExistException(org.apache.flink.table.catalog.exceptions.PartitionNotExistException) PartitionSpecInvalidException(org.apache.flink.table.catalog.exceptions.PartitionSpecInvalidException) TablePartitionedException(org.apache.flink.table.catalog.exceptions.TablePartitionedException) FunctionNotExistException(org.apache.flink.table.catalog.exceptions.FunctionNotExistException) DatabaseNotEmptyException(org.apache.flink.table.catalog.exceptions.DatabaseNotEmptyException) DatabaseAlreadyExistException(org.apache.flink.table.catalog.exceptions.DatabaseAlreadyExistException) TableNotPartitionedException(org.apache.flink.table.catalog.exceptions.TableNotPartitionedException) ValidationException(org.apache.flink.table.api.ValidationException) DatabaseNotExistException(org.apache.flink.table.catalog.exceptions.DatabaseNotExistException) PartitionAlreadyExistsException(org.apache.flink.table.catalog.exceptions.PartitionAlreadyExistsException) SQLException(java.sql.SQLException) TableAlreadyExistException(org.apache.flink.table.catalog.exceptions.TableAlreadyExistException) TableNotExistException(org.apache.flink.table.catalog.exceptions.TableNotExistException) CatalogException(org.apache.flink.table.catalog.exceptions.CatalogException) ResultSetMetaData(java.sql.ResultSetMetaData) DataType(org.apache.flink.table.types.DataType)

Example 18 with TableNotExistException

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

the class HiveCatalog method alterTable.

@Override
public void alterTable(ObjectPath tablePath, CatalogBaseTable newCatalogTable, boolean ignoreIfNotExists) throws TableNotExistException, CatalogException {
    checkNotNull(tablePath, "tablePath cannot be null");
    checkNotNull(newCatalogTable, "newCatalogTable cannot be null");
    Table hiveTable;
    try {
        hiveTable = getHiveTable(tablePath);
    } catch (TableNotExistException e) {
        if (!ignoreIfNotExists) {
            throw e;
        }
        return;
    }
    CatalogBaseTable existingTable = instantiateCatalogTable(hiveTable);
    if (existingTable.getTableKind() != newCatalogTable.getTableKind()) {
        throw new CatalogException(String.format("Table types don't match. Existing table is '%s' and new table is '%s'.", existingTable.getTableKind(), newCatalogTable.getTableKind()));
    }
    disallowChangeCatalogTableType(existingTable.getOptions(), newCatalogTable.getOptions());
    boolean isHiveTable = isHiveTable(hiveTable.getParameters());
    if (isHiveTable) {
        AlterTableOp op = HiveTableUtil.extractAlterTableOp(newCatalogTable.getOptions());
        if (op == null) {
            // the alter operation isn't encoded as properties
            hiveTable = HiveTableUtil.alterTableViaCatalogBaseTable(tablePath, newCatalogTable, hiveTable, hiveConf, false);
        } else {
            alterTableViaProperties(op, hiveTable, (CatalogTable) newCatalogTable, hiveTable.getParameters(), newCatalogTable.getOptions(), hiveTable.getSd());
        }
    } else {
        hiveTable = HiveTableUtil.alterTableViaCatalogBaseTable(tablePath, newCatalogTable, hiveTable, hiveConf, ManagedTableListener.isManagedTable(this, newCatalogTable));
    }
    if (isHiveTable) {
        hiveTable.getParameters().remove(CONNECTOR.key());
    }
    try {
        client.alter_table(tablePath.getDatabaseName(), tablePath.getObjectName(), hiveTable);
    } catch (TException e) {
        throw new CatalogException(String.format("Failed to alter table %s", tablePath.getFullName()), e);
    }
}
Also used : TException(org.apache.thrift.TException) CatalogBaseTable(org.apache.flink.table.catalog.CatalogBaseTable) 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) AlterTableOp(org.apache.flink.sql.parser.hive.ddl.SqlAlterHiveTable.AlterTableOp) TableNotExistException(org.apache.flink.table.catalog.exceptions.TableNotExistException) CatalogException(org.apache.flink.table.catalog.exceptions.CatalogException)

Example 19 with TableNotExistException

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

the class MySqlCatalogITCase method testGetTables_TableNotExistException.

@Test
public void testGetTables_TableNotExistException() throws TableNotExistException {
    String anyTableNotExist = "anyTable";
    assertThatThrownBy(() -> catalog.getTable(new ObjectPath(TEST_DB, anyTableNotExist))).satisfies(anyCauseMatches(TableNotExistException.class, String.format("Table (or view) %s.%s does not exist in Catalog", TEST_DB, anyTableNotExist)));
}
Also used : ObjectPath(org.apache.flink.table.catalog.ObjectPath) TableNotExistException(org.apache.flink.table.catalog.exceptions.TableNotExistException) Test(org.junit.Test)

Example 20 with TableNotExistException

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

the class GenericInMemoryCatalog method renameTable.

@Override
public void renameTable(ObjectPath tablePath, String newTableName, boolean ignoreIfNotExists) throws TableNotExistException, TableAlreadyExistException {
    checkNotNull(tablePath);
    checkArgument(!StringUtils.isNullOrWhitespaceOnly(newTableName));
    if (tableExists(tablePath)) {
        ObjectPath newPath = new ObjectPath(tablePath.getDatabaseName(), newTableName);
        if (tableExists(newPath)) {
            throw new TableAlreadyExistException(getName(), newPath);
        } else {
            tables.put(newPath, tables.remove(tablePath));
            // table statistics
            if (tableStats.containsKey(tablePath)) {
                tableStats.put(newPath, tableStats.remove(tablePath));
            }
            // table column statistics
            if (tableColumnStats.containsKey(tablePath)) {
                tableColumnStats.put(newPath, tableColumnStats.remove(tablePath));
            }
            // partitions
            if (partitions.containsKey(tablePath)) {
                partitions.put(newPath, partitions.remove(tablePath));
            }
            // partition statistics
            if (partitionStats.containsKey(tablePath)) {
                partitionStats.put(newPath, partitionStats.remove(tablePath));
            }
            // partition column statistics
            if (partitionColumnStats.containsKey(tablePath)) {
                partitionColumnStats.put(newPath, partitionColumnStats.remove(tablePath));
            }
        }
    } else if (!ignoreIfNotExists) {
        throw new TableNotExistException(getName(), tablePath);
    }
}
Also used : TableAlreadyExistException(org.apache.flink.table.catalog.exceptions.TableAlreadyExistException) TableNotExistException(org.apache.flink.table.catalog.exceptions.TableNotExistException)

Aggregations

TableNotExistException (org.apache.flink.table.catalog.exceptions.TableNotExistException)25 CatalogException (org.apache.flink.table.catalog.exceptions.CatalogException)15 TException (org.apache.thrift.TException)11 CatalogBaseTable (org.apache.flink.table.catalog.CatalogBaseTable)10 CatalogTable (org.apache.flink.table.catalog.CatalogTable)10 SqlCreateHiveTable (org.apache.flink.sql.parser.hive.ddl.SqlCreateHiveTable)8 PartitionSpecInvalidException (org.apache.flink.table.catalog.exceptions.PartitionSpecInvalidException)8 Table (org.apache.hadoop.hive.metastore.api.Table)8 ObjectPath (org.apache.flink.table.catalog.ObjectPath)7 PartitionNotExistException (org.apache.flink.table.catalog.exceptions.PartitionNotExistException)7 CatalogPartition (org.apache.flink.table.catalog.CatalogPartition)6 List (java.util.List)5 NoSuchObjectException (org.apache.hadoop.hive.metastore.api.NoSuchObjectException)5 ArrayList (java.util.ArrayList)4 Catalog (org.apache.flink.table.catalog.Catalog)4 Partition (org.apache.hadoop.hive.metastore.api.Partition)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 TableException (org.apache.flink.table.api.TableException)3 TableSchema (org.apache.flink.table.api.TableSchema)3