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);
}
}
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;
}
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));
}
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);
}
}
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);
}
}
Aggregations