Java Code Examples for org.apache.flink.table.catalog.CatalogDatabase#getProperties()

The following examples show how to use org.apache.flink.table.catalog.CatalogDatabase#getProperties() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HiveCatalogHiveMetadataTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
@Test
public void testAlterDb() throws Exception {
	// altering Hive DB merges properties, which is different from generic DB
	CatalogDatabase db = createDb();
	catalog.createDatabase(db1, db, false);

	CatalogDatabase newDb = createAnotherDb();
	newDb.getProperties().put(ALTER_DATABASE_OP, AlterHiveDatabaseOp.CHANGE_PROPS.name());
	catalog.alterDatabase(db1, newDb, false);

	Map<String, String> mergedProps = new HashMap<>(db.getProperties());
	mergedProps.putAll(newDb.getProperties());

	assertTrue(catalog.getDatabase(db1).getProperties().entrySet().containsAll(mergedProps.entrySet()));
}
 
Example 2
Source File: HiveCatalog.java    From flink with Apache License 2.0 5 votes vote down vote up
private static Database instantiateHiveDatabase(String databaseName, CatalogDatabase database) {

		Map<String, String> properties = database.getProperties();

		String dbLocationUri = properties.remove(HiveCatalogConfig.DATABASE_LOCATION_URI);

		return new Database(
			databaseName,
			database.getComment(),
			dbLocationUri,
			properties
		);
	}
 
Example 3
Source File: HiveDatabaseUtil.java    From flink with Apache License 2.0 5 votes vote down vote up
static Database instantiateHiveDatabase(String databaseName, CatalogDatabase database) {

		Map<String, String> properties = database.getProperties();

		boolean isGeneric = isGenericForCreate(properties);

		String dbLocationUri = isGeneric ? null : properties.remove(SqlCreateHiveDatabase.DATABASE_LOCATION_URI);

		return new Database(
				databaseName,
				database.getComment(),
				dbLocationUri,
				properties
		);
	}
 
Example 4
Source File: SqlToOperationConverter.java    From flink with Apache License 2.0 5 votes vote down vote up
/** Convert ALTER DATABASE statement. */
private Operation convertAlterDatabase(SqlAlterDatabase sqlAlterDatabase) {
	String[] fullDatabaseName = sqlAlterDatabase.fullDatabaseName();
	if (fullDatabaseName.length > 2) {
		throw new SqlConversionException("alter database identifier format error");
	}
	String catalogName = (fullDatabaseName.length == 1) ? catalogManager.getCurrentCatalog() : fullDatabaseName[0];
	String databaseName = (fullDatabaseName.length == 1) ? fullDatabaseName[0] : fullDatabaseName[1];
	final Map<String, String> properties;
	CatalogDatabase originCatalogDatabase;
	Optional<Catalog> catalog = catalogManager.getCatalog(catalogName);
	if (catalog.isPresent()) {
		try {
			originCatalogDatabase = catalog.get().getDatabase(databaseName);
			properties = new HashMap<>(originCatalogDatabase.getProperties());
		} catch (DatabaseNotExistException e) {
			throw new SqlConversionException(String.format("Database %s not exists", databaseName), e);
		}
	} else {
		throw new SqlConversionException(String.format("Catalog %s not exists", catalogName));
	}
	// set with properties
	sqlAlterDatabase.getPropertyList().getList().forEach(p ->
		properties.put(((SqlTableOption) p).getKeyString(), ((SqlTableOption) p).getValueString()));
	CatalogDatabase catalogDatabase = new CatalogDatabaseImpl(properties, originCatalogDatabase.getComment());
	return new AlterDatabaseOperation(catalogName, databaseName, catalogDatabase);
}
 
Example 5
Source File: HiveDatabaseUtil.java    From flink with Apache License 2.0 4 votes vote down vote up
static Database alterDatabase(Database hiveDB, CatalogDatabase newDatabase) {
	Map<String, String> params = hiveDB.getParameters();
	boolean isGeneric = isGenericForGet(params);
	if (isGeneric) {
		// altering generic DB doesn't merge properties, see CatalogTest::testAlterDb
		hiveDB.setParameters(newDatabase.getProperties());
	} else {
		String opStr = newDatabase.getProperties().remove(ALTER_DATABASE_OP);
		if (opStr == null) {
			throw new CatalogException(ALTER_DATABASE_OP + " property is missing for alter database statement");
		}
		String newLocation = newDatabase.getProperties().remove(SqlCreateHiveDatabase.DATABASE_LOCATION_URI);
		Map<String, String> newParams = newDatabase.getProperties();
		SqlAlterHiveDatabase.AlterHiveDatabaseOp op = SqlAlterHiveDatabase.AlterHiveDatabaseOp.valueOf(opStr);
		switch (op) {
			case CHANGE_PROPS:
				if (params == null) {
					hiveDB.setParameters(newParams);
				} else {
					params.putAll(newParams);
				}
				break;
			case CHANGE_LOCATION:
				hiveDB.setLocationUri(newLocation);
				break;
			case CHANGE_OWNER:
				String ownerName = newParams.remove(DATABASE_OWNER_NAME);
				String ownerType = newParams.remove(DATABASE_OWNER_TYPE);
				hiveDB.setOwnerName(ownerName);
				switch (ownerType) {
					case SqlAlterHiveDatabaseOwner.ROLE_OWNER:
						hiveDB.setOwnerType(PrincipalType.ROLE);
						break;
					case SqlAlterHiveDatabaseOwner.USER_OWNER:
						hiveDB.setOwnerType(PrincipalType.USER);
						break;
					default:
						throw new CatalogException("Unsupported database owner type: " + ownerType);
				}
				break;
			default:
				throw new CatalogException("Unsupported alter database op:" + opStr);
		}
	}
	return hiveDB;
}