Java Code Examples for org.apache.flink.test.util.TestBaseUtils#compareResultCollections()

The following examples show how to use org.apache.flink.test.util.TestBaseUtils#compareResultCollections() . 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
private void executeStreamQueryTable(
		Map<String, String> replaceVars,
		String query,
		List<String> expectedResults) throws Exception {

	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, query);

		assertTrue(desc.isMaterialized());

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

		TestBaseUtils.compareResultCollections(expectedResults, actualResults, Comparator.naturalOrder());
	} finally {
		executor.closeSession(sessionId);
	}
}
 
Example 2
Source File: LocalExecutorITCase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private void executeStreamQueryTable(
		Map<String, String> replaceVars,
		String query,
		List<String> expectedResults) throws Exception {

	final Executor executor = createModifiedExecutor(clusterClient, replaceVars);
	final SessionContext session = new SessionContext("test-session", new Environment());

	try {
		// start job and retrieval
		final ResultDescriptor desc = executor.executeQuery(session, query);

		assertTrue(desc.isMaterialized());

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

		TestBaseUtils.compareResultCollections(expectedResults, actualResults, Comparator.naturalOrder());
	} finally {
		executor.stop(session);
	}
}
 
Example 3
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
private void executeStreamQueryTable(
		Map<String, String> replaceVars,
		String query,
		List<String> expectedResults) throws Exception {

	final Executor executor = createModifiedExecutor(clusterClient, replaceVars);
	final SessionContext session = new SessionContext("test-session", new Environment());

	try {
		// start job and retrieval
		final ResultDescriptor desc = executor.executeQuery(session, query);

		assertTrue(desc.isMaterialized());

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

		TestBaseUtils.compareResultCollections(expectedResults, actualResults, Comparator.naturalOrder());
	} finally {
		executor.stop(session);
	}
}
 
Example 4
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
private void verifySinkResult(String path) throws IOException {
	final List<String> actualResults = new ArrayList<>();
	TestBaseUtils.readAllResultLines(actualResults, path);
	final List<String> expectedResults = new ArrayList<>();
	expectedResults.add("true,hello world,2020-01-01 00:00:01.0");
	expectedResults.add("false,hello world,2020-01-01 00:00:02.0");
	expectedResults.add("false,hello world,2020-01-01 00:00:03.0");
	expectedResults.add("false,hello world,2020-01-01 00:00:04.0");
	expectedResults.add("true,hello world,2020-01-01 00:00:05.0");
	expectedResults.add("false,hello world!!!!,2020-01-01 00:00:06.0");
	TestBaseUtils.compareResultCollections(expectedResults, actualResults, Comparator.naturalOrder());
}
 
Example 5
Source File: GroupingSetsITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void compareSql(String query1, String query2) throws Exception {

		// Function to map row to string
		MapFunction<Row, String> mapFunction = new MapFunction<Row, String>() {

			@Override
			public String map(Row value) throws Exception {
				return value == null ? "null" : value.toString();
			}
		};

		// Execute first query and store results
		Table resultTable1 = tableEnv.sqlQuery(query1);
		DataSet<Row> resultDataSet1 = tableEnv.toDataSet(resultTable1, Row.class);
		List<String> results1 = resultDataSet1.map(mapFunction).collect();

		// Execute second query and store results
		Table resultTable2 = tableEnv.sqlQuery(query2);
		DataSet<Row> resultDataSet2 = tableEnv.toDataSet(resultTable2, Row.class);
		List<String> results2 = resultDataSet2.map(mapFunction).collect();

		// Compare results
		TestBaseUtils.compareResultCollections(results1, results2, new Comparator<String>() {

			@Override
			public int compare(String o1, String o2) {
				return o2 == null ? o1 == null ? 0 : 1 : o1.compareTo(o2);
			}
		});
	}
 
Example 6
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 7
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 8
Source File: GroupingSetsITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
private void compareSql(String query1, String query2) throws Exception {

		// Function to map row to string
		MapFunction<Row, String> mapFunction = new MapFunction<Row, String>() {

			@Override
			public String map(Row value) throws Exception {
				return value == null ? "null" : value.toString();
			}
		};

		// Execute first query and store results
		Table resultTable1 = tableEnv.sqlQuery(query1);
		DataSet<Row> resultDataSet1 = tableEnv.toDataSet(resultTable1, Row.class);
		List<String> results1 = resultDataSet1.map(mapFunction).collect();

		// Execute second query and store results
		Table resultTable2 = tableEnv.sqlQuery(query2);
		DataSet<Row> resultDataSet2 = tableEnv.toDataSet(resultTable2, Row.class);
		List<String> results2 = resultDataSet2.map(mapFunction).collect();

		// Compare results
		TestBaseUtils.compareResultCollections(results1, results2, new Comparator<String>() {

			@Override
			public int compare(String o1, String o2) {
				return o2 == null ? o1 == null ? 0 : 1 : o1.compareTo(o2);
			}
		});
	}
 
Example 9
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
private void verifySinkResult(String path) throws IOException {
	final List<String> actualResults = new ArrayList<>();
	TestBaseUtils.readAllResultLines(actualResults, path);
	final List<String> expectedResults = new ArrayList<>();
	expectedResults.add("true,Hello World");
	expectedResults.add("false,Hello World");
	expectedResults.add("false,Hello World");
	expectedResults.add("false,Hello World");
	expectedResults.add("true,Hello World");
	expectedResults.add("false,Hello World!!!!");
	TestBaseUtils.compareResultCollections(expectedResults, actualResults, Comparator.naturalOrder());
}
 
Example 10
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 30_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());

	try {
		final ResultDescriptor desc = executor.executeQuery(session, "SELECT * FROM TestView1");

		assertTrue(desc.isMaterialized());

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

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

		TestBaseUtils.compareResultCollections(expectedResults, actualResults, Comparator.naturalOrder());
	} finally {
		executor.stop(session);
	}
}
 
Example 11
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 30_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());

	try {
		// start job and retrieval
		final ResultDescriptor desc = executor.executeQuery(
			session,
			"SELECT scalarUDF(IntegerField1), StringField1 FROM TableNumber1");

		assertFalse(desc.isMaterialized());

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

		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!!!!)");

		TestBaseUtils.compareResultCollections(expectedResults, actualResults, Comparator.naturalOrder());
	} finally {
		executor.stop(session);
	}
}
 
Example 12
Source File: GroupingSetsITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
private void compareSql(String query1, String query2) throws Exception {

		// Function to map row to string
		MapFunction<Row, String> mapFunction = new MapFunction<Row, String>() {

			@Override
			public String map(Row value) throws Exception {
				return value == null ? "null" : value.toString();
			}
		};

		// Execute first query and store results
		Table resultTable1 = tableEnv.sqlQuery(query1);
		DataSet<Row> resultDataSet1 = tableEnv.toDataSet(resultTable1, Row.class);
		List<String> results1 = resultDataSet1.map(mapFunction).collect();

		// Execute second query and store results
		Table resultTable2 = tableEnv.sqlQuery(query2);
		DataSet<Row> resultDataSet2 = tableEnv.toDataSet(resultTable2, Row.class);
		List<String> results2 = resultDataSet2.map(mapFunction).collect();

		// Compare results
		TestBaseUtils.compareResultCollections(results1, results2, new Comparator<String>() {

			@Override
			public int compare(String o1, String o2) {
				return o2 == null ? o1 == null ? 0 : 1 : o1.compareTo(o2);
			}
		});
	}
 
Example 13
Source File: LocalExecutorITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void verifySinkResult(String path) throws IOException {
	final List<String> actualResults = new ArrayList<>();
	TestBaseUtils.readAllResultLines(actualResults, path);
	final List<String> expectedResults = new ArrayList<>();
	expectedResults.add("true,Hello World");
	expectedResults.add("false,Hello World");
	expectedResults.add("false,Hello World");
	expectedResults.add("false,Hello World");
	expectedResults.add("true,Hello World");
	expectedResults.add("false,Hello World!!!!");
	TestBaseUtils.compareResultCollections(expectedResults, actualResults, Comparator.naturalOrder());
}
 
Example 14
Source File: LocalExecutorITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 30_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_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());

	try {
		final ResultDescriptor desc = executor.executeQuery(session, "SELECT * FROM TestView1");

		assertTrue(desc.isMaterialized());

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

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

		TestBaseUtils.compareResultCollections(expectedResults, actualResults, Comparator.naturalOrder());
	} finally {
		executor.stop(session);
	}
}
 
Example 15
Source File: LocalExecutorITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 30_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_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());

	try {
		// start job and retrieval
		final ResultDescriptor desc = executor.executeQuery(
			session,
			"SELECT scalarUDF(IntegerField1), StringField1 FROM TableNumber1");

		assertFalse(desc.isMaterialized());

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

		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!!!!)");

		TestBaseUtils.compareResultCollections(expectedResults, actualResults, Comparator.naturalOrder());
	} finally {
		executor.stop(session);
	}
}
 
Example 16
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 30_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());

	try {
		// Case 1: Registered sink
		final ProgramTargetDescriptor targetDescriptor = executor.executeUpdate(
			session,
			"INSERT INTO TableSourceSink SELECT IntegerField1 = 42, StringField1 FROM TableNumber1");

		// wait for job completion and verify result
		boolean isRunning = true;
		while (isRunning) {
			Thread.sleep(50); // slow the processing down
			final JobStatus jobStatus = clusterClient.getJobStatus(JobID.fromHexString(targetDescriptor.getJobId())).get();
			switch (jobStatus) {
				case CREATED:
				case RUNNING:
					continue;
				case FINISHED:
					isRunning = false;
					verifySinkResult(csvOutputPath);
					break;
				default:
					fail("Unexpected job status.");
			}
		}

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

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

		TestBaseUtils.compareResultCollections(
			SimpleCatalogFactory.TABLE_CONTENTS.stream().map(Row::toString).collect(Collectors.toList()),
			otherCatalogResults,
			Comparator.naturalOrder());
	} finally {
		executor.stop(session);
	}
}
 
Example 17
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 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 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);
	}
}