Search in sources :

Example 46 with CatalogException

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

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

the class HiveCatalog method createHiveConf.

@VisibleForTesting
static HiveConf createHiveConf(@Nullable String hiveConfDir, @Nullable String hadoopConfDir) {
    // create HiveConf from hadoop configuration with hadoop conf directory configured.
    Configuration hadoopConf = null;
    if (isNullOrWhitespaceOnly(hadoopConfDir)) {
        for (String possibleHadoopConfPath : HadoopUtils.possibleHadoopConfPaths(new org.apache.flink.configuration.Configuration())) {
            hadoopConf = getHadoopConfiguration(possibleHadoopConfPath);
            if (hadoopConf != null) {
                break;
            }
        }
    } else {
        hadoopConf = getHadoopConfiguration(hadoopConfDir);
        if (hadoopConf == null) {
            String possiableUsedConfFiles = "core-site.xml | hdfs-site.xml | yarn-site.xml | mapred-site.xml";
            throw new CatalogException("Failed to load the hadoop conf from specified path:" + hadoopConfDir, new FileNotFoundException("Please check the path none of the conf files (" + possiableUsedConfFiles + ") exist in the folder."));
        }
    }
    if (hadoopConf == null) {
        hadoopConf = new Configuration();
    }
    // ignore all the static conf file URLs that HiveConf may have set
    HiveConf.setHiveSiteLocation(null);
    HiveConf.setLoadMetastoreConfig(false);
    HiveConf.setLoadHiveServer2Config(false);
    HiveConf hiveConf = new HiveConf(hadoopConf, HiveConf.class);
    LOG.info("Setting hive conf dir as {}", hiveConfDir);
    if (hiveConfDir != null) {
        Path hiveSite = new Path(hiveConfDir, HIVE_SITE_FILE);
        if (!hiveSite.toUri().isAbsolute()) {
            // treat relative URI as local file to be compatible with previous behavior
            hiveSite = new Path(new File(hiveSite.toString()).toURI());
        }
        try (InputStream inputStream = hiveSite.getFileSystem(hadoopConf).open(hiveSite)) {
            hiveConf.addResource(inputStream, hiveSite.toString());
            // trigger a read from the conf so that the input stream is read
            isEmbeddedMetastore(hiveConf);
        } catch (IOException e) {
            throw new CatalogException("Failed to load hive-site.xml from specified path:" + hiveSite, e);
        }
    } else {
        // user doesn't provide hive conf dir, we try to find it in classpath
        URL hiveSite = Thread.currentThread().getContextClassLoader().getResource(HIVE_SITE_FILE);
        if (hiveSite != null) {
            LOG.info("Found {} in classpath: {}", HIVE_SITE_FILE, hiveSite);
            hiveConf.addResource(hiveSite);
        }
    }
    return hiveConf;
}
Also used : Path(org.apache.hadoop.fs.Path) ObjectPath(org.apache.flink.table.catalog.ObjectPath) Configuration(org.apache.hadoop.conf.Configuration) HiveTableUtil.getHadoopConfiguration(org.apache.flink.table.catalog.hive.util.HiveTableUtil.getHadoopConfiguration) InputStream(java.io.InputStream) CatalogException(org.apache.flink.table.catalog.exceptions.CatalogException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) URL(java.net.URL) HiveConf(org.apache.hadoop.hive.conf.HiveConf) File(java.io.File) VisibleForTesting(org.apache.flink.annotation.VisibleForTesting)

Example 48 with CatalogException

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

the class GenericInMemoryCatalogTest method testRegisterCatalog.

@Test
public void testRegisterCatalog() {
    final TableEnvironmentMock tableEnv = TableEnvironmentMock.getStreamingInstance();
    try {
        tableEnv.registerCatalog(TEST_CATALOG_NAME, new MyCatalog(TEST_CATALOG_NAME));
    } catch (CatalogException e) {
    }
    assertThat(tableEnv.getCatalog(TEST_CATALOG_NAME).isPresent(), equalTo(false));
}
Also used : CatalogException(org.apache.flink.table.catalog.exceptions.CatalogException) TableEnvironmentMock(org.apache.flink.table.utils.TableEnvironmentMock) Test(org.junit.Test)

Example 49 with CatalogException

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

the class CatalogPropertiesUtil method serializeCatalogTable.

/**
 * Serializes the given {@link ResolvedCatalogTable} into a map of string properties.
 */
public static Map<String, String> serializeCatalogTable(ResolvedCatalogTable resolvedTable) {
    try {
        final Map<String, String> properties = new HashMap<>();
        serializeResolvedSchema(properties, resolvedTable.getResolvedSchema());
        final String comment = resolvedTable.getComment();
        if (comment != null && comment.length() > 0) {
            properties.put(COMMENT, comment);
        }
        serializePartitionKeys(properties, resolvedTable.getPartitionKeys());
        properties.putAll(resolvedTable.getOptions());
        // reserved option
        properties.remove(IS_GENERIC);
        return properties;
    } catch (Exception e) {
        throw new CatalogException("Error in serializing catalog table.", e);
    }
}
Also used : HashMap(java.util.HashMap) CatalogException(org.apache.flink.table.catalog.exceptions.CatalogException) TableException(org.apache.flink.table.api.TableException) CatalogException(org.apache.flink.table.catalog.exceptions.CatalogException)

Example 50 with CatalogException

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

the class GenericInMemoryCatalog method alterPartition.

@Override
public void alterPartition(ObjectPath tablePath, CatalogPartitionSpec partitionSpec, CatalogPartition newPartition, boolean ignoreIfNotExists) throws PartitionNotExistException, CatalogException {
    checkNotNull(tablePath);
    checkNotNull(partitionSpec);
    checkNotNull(newPartition);
    if (partitionExists(tablePath, partitionSpec)) {
        CatalogPartition existingPartition = partitions.get(tablePath).get(partitionSpec);
        if (existingPartition.getClass() != newPartition.getClass()) {
            throw new CatalogException(String.format("Partition types don't match. Existing partition is '%s' and new partition is '%s'.", existingPartition.getClass().getName(), newPartition.getClass().getName()));
        }
        partitions.get(tablePath).put(partitionSpec, newPartition.copy());
    } else if (!ignoreIfNotExists) {
        throw new PartitionNotExistException(getName(), tablePath, partitionSpec);
    }
}
Also used : CatalogException(org.apache.flink.table.catalog.exceptions.CatalogException) PartitionNotExistException(org.apache.flink.table.catalog.exceptions.PartitionNotExistException)

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