org.apache.flink.table.catalog.CatalogDatabase Java Examples

The following examples show how to use org.apache.flink.table.catalog.CatalogDatabase. 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: SqlToOperationConverter.java    From flink with Apache License 2.0 6 votes vote down vote up
/** Convert CREATE DATABASE statement. */
private Operation convertCreateDatabase(SqlCreateDatabase sqlCreateDatabase) {
	String[] fullDatabaseName = sqlCreateDatabase.fullDatabaseName();
	if (fullDatabaseName.length > 2) {
		throw new SqlConversionException("create database identifier format error");
	}
	String catalogName = (fullDatabaseName.length == 1) ? catalogManager.getCurrentCatalog() : fullDatabaseName[0];
	String databaseName = (fullDatabaseName.length == 1) ? fullDatabaseName[0] : fullDatabaseName[1];
	boolean ignoreIfExists = sqlCreateDatabase.isIfNotExists();
	String databaseComment = sqlCreateDatabase.getComment()
		.map(comment -> comment.getNlsString().getValue()).orElse(null);
	// set with properties
	Map<String, String> properties = new HashMap<>();
	sqlCreateDatabase.getPropertyList().getList().forEach(p ->
		properties.put(((SqlTableOption) p).getKeyString(), ((SqlTableOption) p).getValueString()));
	CatalogDatabase catalogDatabase = new CatalogDatabaseImpl(properties, databaseComment);
	return new CreateDatabaseOperation(catalogName, databaseName, catalogDatabase, ignoreIfExists);
}
 
Example #2
Source File: HiveCatalog.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void alterDatabase(String databaseName, CatalogDatabase newDatabase, boolean ignoreIfNotExists)
		throws DatabaseNotExistException, CatalogException {
	checkArgument(!StringUtils.isNullOrWhitespaceOnly(databaseName), "databaseName cannot be null or empty");
	checkNotNull(newDatabase, "newDatabase cannot be null");

	// client.alterDatabase doesn't throw any exception if there is no existing database
	if (!databaseExists(databaseName)) {
		if (!ignoreIfNotExists) {
			throw new DatabaseNotExistException(getName(), databaseName);
		}

		return;
	}

	Database newHiveDatabase = instantiateHiveDatabase(databaseName, newDatabase);

	try {
		client.alterDatabase(databaseName, newHiveDatabase);
	} catch (TException e) {
		throw new CatalogException(String.format("Failed to alter database %s", databaseName), e);
	}
}
 
Example #3
Source File: SqlToOperationConverter.java    From flink with Apache License 2.0 6 votes vote down vote up
/** Convert CREATE DATABASE statement. */
private Operation convertCreateDatabase(SqlCreateDatabase sqlCreateDatabase) {
	String[] fullDatabaseName = sqlCreateDatabase.fullDatabaseName();
	if (fullDatabaseName.length > 2) {
		throw new SqlConversionException("create database identifier format error");
	}
	String catalogName = (fullDatabaseName.length == 1) ? catalogManager.getCurrentCatalog() : fullDatabaseName[0];
	String databaseName = (fullDatabaseName.length == 1) ? fullDatabaseName[0] : fullDatabaseName[1];
	boolean ignoreIfExists = sqlCreateDatabase.isIfNotExists();
	String databaseComment = sqlCreateDatabase.getComment()
		.map(comment -> comment.getNlsString().getValue()).orElse(null);
	// set with properties
	Map<String, String> properties = new HashMap<>();
	sqlCreateDatabase.getPropertyList().getList().forEach(p ->
		properties.put(((SqlTableOption) p).getKeyString(), ((SqlTableOption) p).getValueString()));
	CatalogDatabase catalogDatabase = new CatalogDatabaseImpl(properties, databaseComment);
	return new CreateDatabaseOperation(catalogName, databaseName, catalogDatabase, ignoreIfExists);
}
 
Example #4
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 #5
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 #6
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 #7
Source File: AlterDatabaseOperation.java    From flink with Apache License 2.0 5 votes vote down vote up
public AlterDatabaseOperation(
		String catalogName,
		String databaseName,
		CatalogDatabase catalogDatabase) {
	this.catalogName = catalogName;
	this.databaseName = databaseName;
	this.catalogDatabase = catalogDatabase;
}
 
Example #8
Source File: CreateDatabaseOperation.java    From flink with Apache License 2.0 5 votes vote down vote up
public CreateDatabaseOperation(
		String catalogName,
		String databaseName,
		CatalogDatabase catalogDatabase, boolean ignoreIfExists) {
	this.catalogName = catalogName;
	this.databaseName = databaseName;
	this.catalogDatabase = catalogDatabase;
	this.ignoreIfExists = ignoreIfExists;
}
 
Example #9
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];
	Map<String, String> properties = new HashMap<>();
	CatalogDatabase originCatalogDatabase;
	Optional<Catalog> catalog = catalogManager.getCatalog(catalogName);
	if (catalog.isPresent()) {
		try {
			originCatalogDatabase = catalog.get().getDatabase(databaseName);
			properties.putAll(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 #10
Source File: HiveCatalogDataTypeTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private static CatalogDatabase createDb() {
	return new CatalogDatabaseImpl(
		new HashMap<String, String>() {{
			put("k1", "v1");
		}},
		""
	);
}
 
Example #11
Source File: HiveCatalog.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public CatalogDatabase getDatabase(String databaseName) throws DatabaseNotExistException, CatalogException {
	Database hiveDatabase = getHiveDatabase(databaseName);

	Map<String, String> properties = hiveDatabase.getParameters();

	boolean isGeneric = isGenericForGet(properties);
	if (!isGeneric) {
		properties.put(SqlCreateHiveDatabase.DATABASE_LOCATION_URI, hiveDatabase.getLocationUri());
	}

	return new CatalogDatabaseImpl(properties, hiveDatabase.getDescription());
}
 
Example #12
Source File: PostgresCatalog.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public CatalogDatabase getDatabase(String databaseName) throws DatabaseNotExistException, CatalogException {
	if (listDatabases().contains(databaseName)) {
		return new CatalogDatabaseImpl(Collections.emptyMap(), null);
	} else {
		throw new DatabaseNotExistException(getName(), databaseName);
	}
}
 
Example #13
Source File: KuduCatalog.java    From bahir-flink with Apache License 2.0 5 votes vote down vote up
@Override
public CatalogDatabase getDatabase(String databaseName) throws DatabaseNotExistException, CatalogException {
    if (databaseName.equals(getDefaultDatabase())) {
        return new CatalogDatabaseImpl(new HashMap<>(), null);
    } else {
        throw new DatabaseNotExistException(getName(), databaseName);
    }
}
 
Example #14
Source File: HiveCatalogDataTypeTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private static CatalogDatabase createDb() {
	return new CatalogDatabaseImpl(
		new HashMap<String, String>() {{
			put("k1", "v1");
		}},
		""
	);
}
 
Example #15
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 #16
Source File: AbstractJdbcCatalog.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void createDatabase(String name, CatalogDatabase database, boolean ignoreIfExists) throws DatabaseAlreadyExistException, CatalogException {
	throw new UnsupportedOperationException();
}
 
Example #17
Source File: AbstractJdbcCatalog.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void alterDatabase(String name, CatalogDatabase newDatabase, boolean ignoreIfNotExists) throws DatabaseNotExistException, CatalogException {
	throw new UnsupportedOperationException();
}
 
Example #18
Source File: PulsarCatalog.java    From pulsar-flink with Apache License 2.0 4 votes vote down vote up
@Override
public CatalogDatabase getDatabase(String databaseName) throws DatabaseNotExistException, CatalogException {
    Map<String, String> properties = new HashMap<>();
    return new CatalogDatabaseImpl(properties, databaseName);
}
 
Example #19
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;
}
 
Example #20
Source File: JdbcCatalog.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public CatalogDatabase getDatabase(String databaseName) throws DatabaseNotExistException, CatalogException {
	return internal.getDatabase(databaseName);
}
 
Example #21
Source File: AbstractReadOnlyCatalog.java    From bahir-flink with Apache License 2.0 4 votes vote down vote up
@Override
public void alterDatabase(String name, CatalogDatabase newDatabase, boolean ignoreIfNotExists) throws DatabaseNotExistException, CatalogException {
    throw UNSUPPORTED_ERR;
}
 
Example #22
Source File: AbstractReadOnlyCatalog.java    From bahir-flink with Apache License 2.0 4 votes vote down vote up
@Override
public void createDatabase(String name, CatalogDatabase database, boolean ignoreIfExists) throws DatabaseAlreadyExistException, CatalogException {
    throw UNSUPPORTED_ERR;
}
 
Example #23
Source File: CreateDatabaseOperation.java    From flink with Apache License 2.0 4 votes vote down vote up
public CatalogDatabase getCatalogDatabase() {
	return catalogDatabase;
}
 
Example #24
Source File: AlterDatabaseOperation.java    From flink with Apache License 2.0 4 votes vote down vote up
public CatalogDatabase getCatalogDatabase() {
	return catalogDatabase;
}
 
Example #25
Source File: PulsarCatalog.java    From pulsar-flink with Apache License 2.0 4 votes vote down vote up
@Override
public void alterDatabase(String name, CatalogDatabase newDatabase, boolean ignoreIfNotExists) throws DatabaseNotExistException, CatalogException {
    throw new UnsupportedOperationException();
}
 
Example #26
Source File: HiveCatalog.java    From flink with Apache License 2.0 3 votes vote down vote up
@Override
public CatalogDatabase getDatabase(String databaseName) throws DatabaseNotExistException, CatalogException {
	Database hiveDatabase = getHiveDatabase(databaseName);

	Map<String, String> properties = hiveDatabase.getParameters();

	properties.put(HiveCatalogConfig.DATABASE_LOCATION_URI, hiveDatabase.getLocationUri());

	return new CatalogDatabaseImpl(properties, hiveDatabase.getDescription());
}