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

The following examples show how to use org.apache.flink.table.client.gateway.Executor#openSession() . 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 6 votes vote down vote up
@Test
public void testListCatalogs() throws Exception {
	final Executor executor = createDefaultExecutor(clusterClient);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	assertEquals("test-session", sessionId);

	final List<String> actualCatalogs = executor.listCatalogs(sessionId);

	final List<String> expectedCatalogs = Arrays.asList(
		"catalog1",
		"default_catalog",
		"simple-catalog");
	assertEquals(expectedCatalogs, actualCatalogs);

	executor.closeSession(sessionId);
}
 
Example 2
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testDropDatabase() throws Exception {
	final Executor executor = createDefaultExecutor(clusterClient);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	assertEquals("test-session", sessionId);

	executor.executeUpdate(sessionId, "create database db1");

	List<String> actualDatabases = executor.listDatabases(sessionId);
	List<String> expectedDatabases = Arrays.asList("default_database", "db1");
	assertEquals(expectedDatabases, actualDatabases);

	executor.executeUpdate(sessionId, "drop database if exists db1");

	actualDatabases = executor.listDatabases(sessionId);
	expectedDatabases = Arrays.asList("default_database");
	assertEquals(expectedDatabases, actualDatabases);

	executor.closeSession(sessionId);
}
 
Example 3
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testAlterTable() throws Exception {
	final Executor executor = createDefaultExecutor(clusterClient);
	final LocalExecutor localExecutor = (LocalExecutor) executor;
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	assertEquals("test-session", sessionId);
	executor.useCatalog(sessionId, "simple-catalog");
	executor.useDatabase(sessionId, "default_database");
	List<String> actualTables = executor.listTables(sessionId);
	List<String> expectedTables = Arrays.asList("test-table");
	assertEquals(expectedTables, actualTables);
	executor.executeUpdate(sessionId, "alter table `test-table` rename to t1");
	actualTables = executor.listTables(sessionId);
	expectedTables = Arrays.asList("t1");
	assertEquals(expectedTables, actualTables);
	//todo: we should add alter table set test when we support create table in executor.
	executor.closeSession(sessionId);
}
 
Example 4
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testTableSchema() throws Exception {
	final Executor executor = createDefaultExecutor(clusterClient);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	assertEquals("test-session", sessionId);

	final TableSchema actualTableSchema = executor.getTableSchema(sessionId, "TableNumber2");

	final TableSchema expectedTableSchema = new TableSchema(
		new String[]{"IntegerField2", "StringField2", "TimestampField2"},
		new TypeInformation[]{Types.INT, Types.STRING, Types.SQL_TIMESTAMP});

	assertEquals(expectedTableSchema, actualTableSchema);
	executor.closeSession(sessionId);
}
 
Example 5
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testCompleteStatement() throws Exception {
	final Executor executor = createDefaultExecutor(clusterClient);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	assertEquals("test-session", sessionId);

	final List<String> expectedTableHints = Arrays.asList(
		"default_catalog.default_database.TableNumber1",
		"default_catalog.default_database.TableNumber2",
		"default_catalog.default_database.TableSourceSink");
	assertEquals(expectedTableHints, executor.completeStatement(sessionId, "SELECT * FROM Ta", 16));

	final List<String> expectedClause = Collections.singletonList("WHERE");
	assertEquals(expectedClause, executor.completeStatement(sessionId, "SELECT * FROM TableNumber2 WH", 29));

	final List<String> expectedField = Arrays.asList("IntegerField1");
	assertEquals(expectedField, executor.completeStatement(sessionId, "SELECT * FROM TableNumber1 WHERE Inte", 37));
	executor.closeSession(sessionId);
}
 
Example 6
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateTable() throws Exception {
	final Executor executor = createDefaultExecutor(clusterClient);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	final String ddlTemplate = "create table %s(\n" +
			"  a int,\n" +
			"  b bigint,\n" +
			"  c varchar\n" +
			") with (\n" +
			"  'connector.type'='filesystem',\n" +
			"  'format.type'='csv',\n" +
			"  'connector.path'='xxx'\n" +
			")\n";
	try {
		// Test create table with simple name.
		executor.useCatalog(sessionId, "catalog1");
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable1"));
		assertEquals(Collections.singletonList("MyTable1"), executor.listTables(sessionId));
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable2"));
		assertEquals(Arrays.asList("MyTable1", "MyTable2"), executor.listTables(sessionId));

		// Test create table with full qualified name.
		executor.useCatalog(sessionId, "catalog1");
		executor.createTable(sessionId, String.format(ddlTemplate, "`simple-catalog`.`default_database`.MyTable3"));
		executor.createTable(sessionId, String.format(ddlTemplate, "`simple-catalog`.`default_database`.MyTable4"));
		assertEquals(Arrays.asList("MyTable1", "MyTable2"), executor.listTables(sessionId));
		executor.useCatalog(sessionId, "simple-catalog");
		assertEquals(Arrays.asList("MyTable3", "MyTable4", "test-table"), executor.listTables(sessionId));

		// Test create table with db and table name.
		executor.useCatalog(sessionId, "catalog1");
		executor.createTable(sessionId, String.format(ddlTemplate, "`default`.MyTable5"));
		executor.createTable(sessionId, String.format(ddlTemplate, "`default`.MyTable6"));
		assertEquals(Arrays.asList("MyTable1", "MyTable2", "MyTable5", "MyTable6"), executor.listTables(sessionId));
	} finally {
		executor.closeSession(sessionId);
	}
}
 
Example 7
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 90_000L)
public void ensureExceptionOnFaultySourceInBatch() throws Exception {
	final String missingFileName = "missing-source";

	final Map<String, String> replaceVars = new HashMap<>();
	replaceVars.put("$VAR_PLANNER", planner);
	replaceVars.put("$VAR_SOURCE_PATH1", missingFileName);
	replaceVars.put("$VAR_EXECUTION_TYPE", "batch");
	replaceVars.put("$VAR_RESULT_MODE", "table");
	replaceVars.put("$VAR_UPDATE_MODE", "");
	replaceVars.put("$VAR_MAX_ROWS", "100");
	replaceVars.put("$VAR_RESTART_STRATEGY_TYPE", "none");

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

	Optional<Throwable> throwableWithMessage = Optional.empty();
	try {
		final ResultDescriptor desc = executor.executeQuery(sessionId, "SELECT * FROM TestView1");
		retrieveTableResult(executor, sessionId, desc.getResultId());
	} catch (SqlExecutionException e) {
		throwableWithMessage = findMissingFileException(e, missingFileName);
	} finally {
		executor.closeSession(sessionId);
	}
	assertTrue(throwableWithMessage.isPresent());
}
 
Example 8
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 90_000L)
public void ensureExceptionOnFaultySourceInStreamingTableMode() throws Exception {
	final String missingFileName = "missing-source";

	final Map<String, String> replaceVars = new HashMap<>();
	replaceVars.put("$VAR_PLANNER", planner);
	replaceVars.put("$VAR_SOURCE_PATH1", missingFileName);
	replaceVars.put("$VAR_EXECUTION_TYPE", "streaming");
	replaceVars.put("$VAR_RESULT_MODE", "table");
	replaceVars.put("$VAR_UPDATE_MODE", "update-mode: append");
	replaceVars.put("$VAR_MAX_ROWS", "1");
	replaceVars.put("$VAR_RESTART_STRATEGY_TYPE", "none");

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

	Optional<Throwable> throwableWithMessage = Optional.empty();
	try {
		final ResultDescriptor desc = executor.executeQuery(sessionId, "SELECT * FROM TestView1");
		retrieveTableResult(executor, sessionId, desc.getResultId());
	} catch (SqlExecutionException e) {
		throwableWithMessage = findMissingFileException(e, missingFileName);
	} finally {
		executor.closeSession(sessionId);
	}
	assertTrue(throwableWithMessage.isPresent());
}
 
Example 9
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 10
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 90_000L)
public void testBatchQueryExecutionMultipleTimes() throws Exception {
	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", "batch");
	replaceVars.put("$VAR_RESULT_MODE", "table");
	replaceVars.put("$VAR_UPDATE_MODE", "");
	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);

	final List<String> expectedResults = new ArrayList<>();
	expectedResults.add("47");
	expectedResults.add("27");
	expectedResults.add("37");
	expectedResults.add("37");
	expectedResults.add("47");
	expectedResults.add("57");

	try {
		for (int i = 0; i < 3; i++) {
			final ResultDescriptor desc = executor.executeQuery(sessionId, "SELECT * FROM TestView1");

			assertTrue(desc.isMaterialized());

			final List<String> actualResults = retrieveTableResult(executor, sessionId, desc.getResultId());

			TestBaseUtils.compareResultCollections(expectedResults, actualResults, Comparator.naturalOrder());
		}
	} finally {
		executor.closeSession(sessionId);
	}
}
 
Example 11
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 90_000L)
public void testBatchQueryExecution() throws Exception {
	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", "batch");
	replaceVars.put("$VAR_RESULT_MODE", "table");
	replaceVars.put("$VAR_UPDATE_MODE", "");
	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 {
		final ResultDescriptor desc = executor.executeQuery(sessionId, "SELECT *, 'ABC' FROM TestView1");

		assertTrue(desc.isMaterialized());

		final List<String> actualResults = retrieveTableResult(executor, sessionId, desc.getResultId());

		final List<String> expectedResults = new ArrayList<>();
		expectedResults.add("47,ABC");
		expectedResults.add("27,ABC");
		expectedResults.add("37,ABC");
		expectedResults.add("37,ABC");
		expectedResults.add("47,ABC");
		expectedResults.add("57,ABC");

		TestBaseUtils.compareResultCollections(expectedResults, actualResults, Comparator.naturalOrder());
	} finally {
		executor.closeSession(sessionId);
	}
}
 
Example 12
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 90_000L)
public void testStreamQueryExecutionTableMultipleTimes() throws Exception {
	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_RESULT_MODE", "table");
	replaceVars.put("$VAR_UPDATE_MODE", "update-mode: append");
	replaceVars.put("$VAR_MAX_ROWS", "100");

	final String query = "SELECT scalarUDF(IntegerField1), StringField1 FROM TableNumber1";

	final List<String> expectedResults = new ArrayList<>();
	expectedResults.add("47,Hello World");
	expectedResults.add("27,Hello World");
	expectedResults.add("37,Hello World");
	expectedResults.add("37,Hello World");
	expectedResults.add("47,Hello World");
	expectedResults.add("57,Hello World!!!!");

	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 {
		for (int i = 0; i < 3; i++) {
			executeStreamQueryTable(replaceVars, query, expectedResults);
		}
	} finally {
		executor.closeSession(sessionId);
	}
}
 
Example 13
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 14
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testListUserDefinedFunctions() throws Exception {
	final Executor executor = createDefaultExecutor(clusterClient);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	assertEquals("test-session", sessionId);

	final List<String> actualTables = executor.listUserDefinedFunctions(sessionId);

	final List<String> expectedTables = Arrays.asList("aggregateudf", "tableudf", "scalarudf");
	assertEquals(expectedTables, actualTables);

	executor.closeSession(sessionId);
}
 
Example 15
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test @Ignore // TODO: reopen when FLINK-15075 was fixed.
public void testCreateTableWithWatermark() throws Exception {
	final Map<String, String> replaceVars = new HashMap<>();
	replaceVars.put("$VAR_PLANNER", planner);
	replaceVars.put("$VAR_SOURCE_PATH1", "file:///fakePath1");
	replaceVars.put("$VAR_SOURCE_PATH2", "file:///fakePath2");
	replaceVars.put("$VAR_EXECUTION_TYPE", "batch");
	replaceVars.put("$VAR_UPDATE_MODE", "update-mode: append");
	replaceVars.put("$VAR_MAX_ROWS", "100");
	replaceVars.put("$VAR_RESULT_MODE", "table");
	final Executor executor = createModifiedExecutor(clusterClient, replaceVars);
	final String ddlTemplate = "create table %s(\n" +
			"  a int,\n" +
			"  b timestamp(3),\n" +
			"  watermark for b as b - INTERVAL '5' second\n" +
			") with (\n" +
			"  'connector.type'='filesystem',\n" +
			"  'format.type'='csv',\n" +
			"  'connector.path'='xxx'\n" +
			")\n";
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	try {
		executor.useCatalog(sessionId, "catalog1");
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable1"));
		assertEquals(Collections.singletonList("MyTable1"), executor.listTables(sessionId));
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable2"));
		assertEquals(Arrays.asList("MyTable1", "MyTable2"), executor.listTables(sessionId));
	} finally {
		executor.closeSession(sessionId);
	}
}
 
Example 16
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testUseNonExistingDatabase() throws Exception {
	final Executor executor = createDefaultExecutor(clusterClient);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	assertEquals("test-session", sessionId);

	exception.expect(SqlExecutionException.class);
	executor.useDatabase(sessionId, "nonexistingdb");
}
 
Example 17
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateDatabase() throws Exception {
	final Executor executor = createDefaultExecutor(clusterClient);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	assertEquals("test-session", sessionId);

	executor.executeUpdate(sessionId, "create database db1");

	final List<String> actualDatabases = executor.listDatabases(sessionId);
	final List<String> expectedDatabases = Arrays.asList("default_database", "db1");
	assertEquals(expectedDatabases, actualDatabases);

	executor.closeSession(sessionId);
}
 
Example 18
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 90_000L)
public void testStreamQueryExecutionChangelogMultipleTimes() throws Exception {
	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_RESULT_MODE", "changelog");
	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);

	final List<String> expectedResults = new ArrayList<>();
	expectedResults.add("(true,47,Hello World)");
	expectedResults.add("(true,27,Hello World)");
	expectedResults.add("(true,37,Hello World)");
	expectedResults.add("(true,37,Hello World)");
	expectedResults.add("(true,47,Hello World)");
	expectedResults.add("(true,57,Hello World!!!!)");

	try {
		for (int i = 0; i < 3; i++) {
			// start job and retrieval
			final ResultDescriptor desc = executor.executeQuery(
					sessionId,
					"SELECT scalarUDF(IntegerField1), StringField1 FROM TableNumber1");

			assertFalse(desc.isMaterialized());

			final List<String> actualResults =
					retrieveChangelogResult(executor, sessionId, desc.getResultId());

			TestBaseUtils.compareResultCollections(expectedResults, actualResults, Comparator.naturalOrder());
		}
	} finally {
		executor.closeSession(sessionId);
	}
}
 
Example 19
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 90_000L)
public void testStreamQueryExecutionChangelog() throws Exception {
	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_RESULT_MODE", "changelog");
	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 {
		// start job and retrieval
		final ResultDescriptor desc = executor.executeQuery(
			sessionId,
			"SELECT scalarUDF(IntegerField1), StringField1, 'ABC' FROM TableNumber1");

		assertFalse(desc.isMaterialized());

		final List<String> actualResults =
			retrieveChangelogResult(executor, sessionId, desc.getResultId());

		final List<String> expectedResults = new ArrayList<>();
		expectedResults.add("(true,47,Hello World,ABC)");
		expectedResults.add("(true,27,Hello World,ABC)");
		expectedResults.add("(true,37,Hello World,ABC)");
		expectedResults.add("(true,37,Hello World,ABC)");
		expectedResults.add("(true,47,Hello World,ABC)");
		expectedResults.add("(true,57,Hello World!!!!,ABC)");

		TestBaseUtils.compareResultCollections(expectedResults, actualResults, Comparator.naturalOrder());
	} finally {
		executor.closeSession(sessionId);
	}
}
 
Example 20
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);
	}
}