Java Code Examples for org.apache.flink.table.catalog.ObjectPath#getDatabaseName()

The following examples show how to use org.apache.flink.table.catalog.ObjectPath#getDatabaseName() . 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: HiveCatalog.java    From flink with Apache License 2.0 6 votes vote down vote up
private static Function instantiateHiveFunction(ObjectPath functionPath, CatalogFunction function) {

		boolean isGeneric = Boolean.valueOf(function.getProperties().get(CatalogConfig.IS_GENERIC));

		// Hive Function does not have properties map
		// thus, use a prefix in class name to distinguish Flink and Hive functions
		String functionClassName = isGeneric ?
			FLINK_FUNCTION_PREFIX + function.getClassName() :
			function.getClassName();

		return new Function(
			// due to https://issues.apache.org/jira/browse/HIVE-22053, we have to normalize function name ourselves
			HiveStringUtils.normalizeIdentifier(functionPath.getObjectName()),
			functionPath.getDatabaseName(),
			functionClassName,
			null,			// Owner name
			PrincipalType.GROUP,	// Temporarily set to GROUP type because it's required by Hive. May change later
			(int) (System.currentTimeMillis() / 1000),
			FunctionType.JAVA,		// FunctionType only has JAVA now
			new ArrayList<>()		// Resource URIs
		);
	}
 
Example 2
Source File: HiveCatalog.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void renameTable(ObjectPath tablePath, String newTableName, boolean ignoreIfNotExists)
		throws TableNotExistException, TableAlreadyExistException, CatalogException {
	checkNotNull(tablePath, "tablePath cannot be null");
	checkArgument(!StringUtils.isNullOrWhitespaceOnly(newTableName), "newTableName cannot be null or empty");

	try {
		// alter_table() doesn't throw a clear exception when target table doesn't exist.
		// Thus, check the table existence explicitly
		if (tableExists(tablePath)) {
			ObjectPath newPath = new ObjectPath(tablePath.getDatabaseName(), newTableName);
			// alter_table() doesn't throw a clear exception when new table already exists.
			// Thus, check the table existence explicitly
			if (tableExists(newPath)) {
				throw new TableAlreadyExistException(getName(), newPath);
			} else {
				Table table = getHiveTable(tablePath);
				table.setTableName(newTableName);
				client.alter_table(tablePath.getDatabaseName(), tablePath.getObjectName(), table);
			}
		} else if (!ignoreIfNotExists) {
			throw new TableNotExistException(getName(), tablePath);
		}
	} catch (TException e) {
		throw new CatalogException(
			String.format("Failed to rename table %s", tablePath.getFullName()), e);
	}
}
 
Example 3
Source File: HiveCatalog.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void renameTable(ObjectPath tablePath, String newTableName, boolean ignoreIfNotExists)
		throws TableNotExistException, TableAlreadyExistException, CatalogException {
	checkNotNull(tablePath, "tablePath cannot be null");
	checkArgument(!StringUtils.isNullOrWhitespaceOnly(newTableName), "newTableName cannot be null or empty");

	try {
		// alter_table() doesn't throw a clear exception when target table doesn't exist.
		// Thus, check the table existence explicitly
		if (tableExists(tablePath)) {
			ObjectPath newPath = new ObjectPath(tablePath.getDatabaseName(), newTableName);
			// alter_table() doesn't throw a clear exception when new table already exists.
			// Thus, check the table existence explicitly
			if (tableExists(newPath)) {
				throw new TableAlreadyExistException(getName(), newPath);
			} else {
				Table table = getHiveTable(tablePath);
				table.setTableName(newTableName);
				client.alter_table(tablePath.getDatabaseName(), tablePath.getObjectName(), table);
			}
		} else if (!ignoreIfNotExists) {
			throw new TableNotExistException(getName(), tablePath);
		}
	} catch (TException e) {
		throw new CatalogException(
			String.format("Failed to rename table %s", tablePath.getFullName()), e);
	}
}
 
Example 4
Source File: HiveCatalog.java    From flink with Apache License 2.0 5 votes vote down vote up
private static Function instantiateHiveFunction(ObjectPath functionPath, CatalogFunction function) {

		boolean isGeneric = function.isGeneric();

		// Hive Function does not have properties map
		// thus, use a prefix in class name to distinguish Flink and Hive functions
		String functionClassName;
		if (function.getFunctionLanguage().equals(FunctionLanguage.JAVA)) {
			functionClassName = isGeneric ?
				FLINK_FUNCTION_PREFIX + function.getClassName() :
				function.getClassName();
		} else if (function.getFunctionLanguage().equals(FunctionLanguage.PYTHON)) {
			functionClassName = FLINK_PYTHON_FUNCTION_PREFIX + function.getClassName();
		} else {
			throw new UnsupportedOperationException("HiveCatalog supports only creating" +
				" JAVA or PYTHON based function for now");
		}

		return new Function(
			// due to https://issues.apache.org/jira/browse/HIVE-22053, we have to normalize function name ourselves
			functionPath.getObjectName().trim().toLowerCase(),
			functionPath.getDatabaseName(),
			functionClassName,
			null,			// Owner name
			PrincipalType.GROUP,	// Temporarily set to GROUP type because it's required by Hive. May change later
			(int) (System.currentTimeMillis() / 1000),
			FunctionType.JAVA,		// FunctionType only has JAVA now
			new ArrayList<>()		// Resource URIs
		);
	}
 
Example 5
Source File: PostgresCatalog.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public CatalogBaseTable getTable(ObjectPath tablePath) throws TableNotExistException, CatalogException {
	if (!tableExists(tablePath)) {
		throw new TableNotExistException(getName(), tablePath);
	}

	PostgresTablePath pgPath = PostgresTablePath.fromFlinkTableName(tablePath.getObjectName());

	String dbUrl = baseUrl + tablePath.getDatabaseName();
	try (Connection conn = DriverManager.getConnection(dbUrl, username, pwd)) {
		DatabaseMetaData metaData = conn.getMetaData();
		Optional<UniqueConstraint> primaryKey = getPrimaryKey(
			metaData,
			pgPath.getPgSchemaName(),
			pgPath.getPgTableName());

		PreparedStatement ps = conn.prepareStatement(
			String.format("SELECT * FROM %s;", pgPath.getFullPath()));

		ResultSetMetaData rsmd = ps.getMetaData();

		String[] names = new String[rsmd.getColumnCount()];
		DataType[] types = new DataType[rsmd.getColumnCount()];

		for (int i = 1; i <= rsmd.getColumnCount(); i++) {
			names[i - 1] = rsmd.getColumnName(i);
			types[i - 1] = fromJDBCType(rsmd, i);
			if (rsmd.isNullable(i) == ResultSetMetaData.columnNoNulls) {
				types[i - 1] = types[i - 1].notNull();
			}
		}

		TableSchema.Builder tableBuilder = new TableSchema.Builder()
			.fields(names, types);
		primaryKey.ifPresent(pk ->
			tableBuilder.primaryKey(pk.getName(), pk.getColumns().toArray(new String[0]))
		);
		TableSchema tableSchema = tableBuilder.build();

		Map<String, String> props = new HashMap<>();
		props.put(CONNECTOR.key(), IDENTIFIER);
		props.put(URL.key(), dbUrl);
		props.put(TABLE_NAME.key(), pgPath.getFullPath());
		props.put(USERNAME.key(), username);
		props.put(PASSWORD.key(), pwd);

		return new CatalogTableImpl(
			tableSchema,
			props,
			""
		);
	} catch (Exception e) {
		throw new CatalogException(
			String.format("Failed getting table %s", tablePath.getFullName()), e);
	}
}