Java Code Examples for org.apache.flink.table.client.gateway.Executor#executeSql()

The following examples show how to use org.apache.flink.table.client.gateway.Executor#executeSql() . 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: LocalExecutorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateFunction() throws Exception {
	final Executor executor = createDefaultExecutor(clusterClient);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	// arguments: [TEMPORARY|TEMPORARY SYSTEM], [IF NOT EXISTS], func_name
	final String ddlTemplate = "create %s function %s %s \n"
			+ "as 'org.apache.flink.table.client.gateway.local.LocalExecutorITCase$TestScalaFunction' LANGUAGE JAVA";
	try {
		// Test create table with simple name.
		executor.useCatalog(sessionId, "catalog1");
		executor.executeSql(sessionId, String.format(ddlTemplate, "", "", "func1"));
		assertThat(executor.listFunctions(sessionId), hasItems("func1"));
		executor.executeSql(sessionId, String.format(ddlTemplate, "TEMPORARY", "IF NOT EXISTS", "func2"));
		assertThat(executor.listFunctions(sessionId), hasItems("func1", "func2"));

		// Test create function with full qualified name.
		executor.useCatalog(sessionId, "catalog1");
		executor.createTable(sessionId, String.format(ddlTemplate, "", "", "`simple-catalog`.`default_database`.func3"));
		executor.createTable(sessionId, String.format(ddlTemplate, "TEMPORARY", "IF NOT EXISTS", "`simple-catalog`.`default_database`.func4"));
		assertThat(executor.listFunctions(sessionId), hasItems("func1", "func2"));
		executor.useCatalog(sessionId, "simple-catalog");
		assertThat(executor.listFunctions(sessionId), hasItems("func3", "func4"));

		// Test create function with db and table name.
		executor.useCatalog(sessionId, "catalog1");
		executor.createTable(sessionId, String.format(ddlTemplate, "TEMPORARY", "", "`default`.func5"));
		executor.createTable(sessionId, String.format(ddlTemplate, "TEMPORARY", "", "`default`.func6"));
		assertThat(executor.listFunctions(sessionId), hasItems("func1", "func2", "func5", "func6"));
	} finally {
		executor.closeSession(sessionId);
	}
}
 
Example 2
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testAlterFunction() throws Exception {
	final Executor executor = createDefaultExecutor(clusterClient);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	try {
		executor.useCatalog(sessionId, "catalog1");
		executor.setSessionProperty(sessionId, "execution.type", "batch");
		// arguments: [TEMPORARY|TEMPORARY SYSTEM], [IF NOT EXISTS], func_name
		final String createTemplate = "create %s function %s %s \n"
				+ "as 'org.apache.flink.table.client.gateway.local.LocalExecutorITCase$TestScalaFunction' LANGUAGE JAVA";
		// arguments: [TEMPORARY|TEMPORARY SYSTEM], [IF EXISTS], func_name, func_class
		final String alterTemplate = "alter %s function %s %s AS %s";
		// Test alter function.
		executor.executeSql(sessionId, String.format(createTemplate, "", "", "func1"));
		assertThat(executor.listFunctions(sessionId), hasItems("func1"));
		executor.executeSql(sessionId, String.format(alterTemplate, "", "IF EXISTS", "`default`.func1", "'newClass'"));
		assertThat(executor.listFunctions(sessionId), hasItems("func1"));

		// Test alter non temporary function with TEMPORARY keyword.
		try {
			executor.executeSql(sessionId, String.format(alterTemplate, "TEMPORARY", "IF EXISTS", "`default`.func2", "'func3'"));
			fail("unexpected exception");
		} catch (Exception var1) {
			assertThat(var1.getCause().getMessage(), is("Alter temporary catalog function is not supported"));
		}
	} finally {
		executor.closeSession(sessionId);
	}
}
 
Example 3
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 90_000L)
public void testStreamQueryExecutionSink() throws Exception {
	final String csvOutputPath = new File(tempFolder.newFolder().getAbsolutePath(), "test-out.csv").toURI().toString();
	final URL url = getClass().getClassLoader().getResource("test-data.csv");
	Objects.requireNonNull(url);
	final Map<String, String> replaceVars = new HashMap<>();
	replaceVars.put("$VAR_PLANNER", planner);
	replaceVars.put("$VAR_SOURCE_PATH1", url.getPath());
	replaceVars.put("$VAR_EXECUTION_TYPE", "streaming");
	replaceVars.put("$VAR_SOURCE_SINK_PATH", csvOutputPath);
	replaceVars.put("$VAR_UPDATE_MODE", "update-mode: append");
	replaceVars.put("$VAR_MAX_ROWS", "100");

	final Executor executor = createModifiedExecutor(clusterClient, replaceVars);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	assertEquals("test-session", sessionId);

	try {
		executor.executeSql(sessionId, "CREATE FUNCTION LowerUDF AS 'LowerUDF'");
		// Case 1: Registered sink
		// Case 1.1: Registered sink with uppercase insert into keyword.
		// FLINK-18302: wrong classloader when INSERT INTO with UDF
		final String statement1 = "INSERT INTO TableSourceSink SELECT IntegerField1 = 42," +
				" LowerUDF(StringField1), TimestampField1 FROM TableNumber1";
		executeAndVerifySinkResult(executor, sessionId, statement1, csvOutputPath);
		// Case 1.2: Registered sink with lowercase insert into keyword.
		final String statement2 = "insert Into TableSourceSink \n "
				+ "SELECT IntegerField1 = 42, LowerUDF(StringField1), TimestampField1 "
				+ "FROM TableNumber1";
		executeAndVerifySinkResult(executor, sessionId, statement2, csvOutputPath);
		// Case 1.3: Execute the same statement again, the results should expect to be the same.
		executeAndVerifySinkResult(executor, sessionId, statement2, csvOutputPath);

		// Case 2: Temporary sink
		executor.useCatalog(sessionId, "simple-catalog");
		executor.useDatabase(sessionId, "default_database");
		// all queries are pipelined to an in-memory sink, check it is properly registered
		final ResultDescriptor otherCatalogDesc = executor.executeQuery(sessionId, "SELECT * FROM `test-table`");

		final List<String> otherCatalogResults = retrieveTableResult(
			executor,
			sessionId,
			otherCatalogDesc.getResultId());

		TestBaseUtils.compareResultCollections(
			SimpleCatalogFactory.TABLE_CONTENTS.stream().map(Row::toString).collect(Collectors.toList()),
			otherCatalogResults,
			Comparator.naturalOrder());
	} finally {
		executor.closeSession(sessionId);
	}
}
 
Example 4
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testDropFunction() throws Exception {
	final Executor executor = createDefaultExecutor(clusterClient);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	try {
		executor.useCatalog(sessionId, "catalog1");
		executor.setSessionProperty(sessionId, "execution.type", "batch");
		// arguments: [TEMPORARY|TEMPORARY SYSTEM], [IF NOT EXISTS], func_name
		final String createTemplate = "create %s function %s %s \n"
				+ "as 'org.apache.flink.table.client.gateway.local.LocalExecutorITCase$TestScalaFunction' LANGUAGE JAVA";
		// arguments: [TEMPORARY|TEMPORARY SYSTEM], [IF EXISTS], func_name
		final String dropTemplate = "drop %s function %s %s";
		// Test drop function.
		executor.executeSql(sessionId, String.format(createTemplate, "", "", "func1"));
		assertThat(executor.listFunctions(sessionId), hasItems("func1"));
		executor.executeSql(sessionId, String.format(dropTemplate, "", "", "func1"));
		assertThat(executor.listFunctions(sessionId), not(hasItems("func1")));

		// Test drop function if exists.
		executor.executeSql(sessionId, String.format(createTemplate, "", "", "func1"));
		assertThat(executor.listFunctions(sessionId), hasItems("func1"));
		executor.executeSql(sessionId, String.format(dropTemplate, "", "IF EXISTS", "func1"));
		assertThat(executor.listFunctions(sessionId), not(hasItems("func1")));

		// Test drop function with full qualified name.
		executor.executeSql(sessionId, String.format(createTemplate, "", "", "func1"));
		assertThat(executor.listFunctions(sessionId), hasItems("func1"));
		executor.executeSql(sessionId, String.format(dropTemplate, "", "IF EXISTS", "catalog1.`default`.func1"));
		assertThat(executor.listFunctions(sessionId), not(hasItems("func1")));

		// Test drop function with db and function name.
		executor.executeSql(sessionId, String.format(createTemplate, "", "", "func1"));
		assertThat(executor.listFunctions(sessionId), hasItems("func1"));
		executor.executeSql(sessionId, String.format(dropTemplate, "", "IF EXISTS", "`default`.func1"));
		assertThat(executor.listFunctions(sessionId), not(hasItems("func1")));

		// Test drop function that does not exist.
		executor.executeSql(sessionId, String.format(createTemplate, "", "", "func1"));
		assertThat(executor.listFunctions(sessionId), hasItems("func1"));
		try {
			executor.executeSql(sessionId, String.format(dropTemplate, "", "IF EXISTS", "catalog2.`default`.func1"));
			fail("unexpected");
		} catch (Exception e) {
			assertThat(e.getCause().getMessage(), is("Catalog catalog2 does not exist"));
		}
		assertThat(executor.listFunctions(sessionId), hasItems("func1"));
		executor.executeSql(sessionId, String.format(dropTemplate, "", "", "`default`.func1"));

		// Test drop function with properties changed.
		executor.executeSql(sessionId, String.format(createTemplate, "", "", "func1"));
		// Change the session property to trigger `new ExecutionContext`.
		executor.setSessionProperty(sessionId, "execution.restart-strategy.failure-rate-interval", "12345");
		executor.executeSql(sessionId, String.format(createTemplate, "", "", "func2"));
		assertThat(executor.listFunctions(sessionId), hasItems("func1", "func2"));
		executor.executeSql(sessionId, String.format(dropTemplate, "", "", "func1"));
		executor.executeSql(sessionId, String.format(dropTemplate, "", "", "func2"));
		assertThat(executor.listFunctions(sessionId), not(hasItems("func1", "func2")));

		// Test drop function with properties reset.
		// Reset the session properties.
		executor.executeSql(sessionId, String.format(createTemplate, "", "", "func1"));
		executor.executeSql(sessionId, String.format(createTemplate, "", "", "func2"));
		executor.resetSessionProperties(sessionId);
		executor.executeSql(sessionId, String.format(createTemplate, "", "", "func3"));
		assertThat(executor.listFunctions(sessionId), hasItems("func1", "func2", "func3"));
		executor.executeSql(sessionId, String.format(dropTemplate, "", "", "func1"));
		executor.executeSql(sessionId, String.format(dropTemplate, "", "", "func2"));
		executor.executeSql(sessionId, String.format(dropTemplate, "", "", "func3"));
		assertThat(executor.listFunctions(sessionId), not(hasItems("func1", "func2", "func3")));
	} finally {
		executor.closeSession(sessionId);
	}
}