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

The following examples show how to use org.apache.flink.table.client.gateway.Executor#createTable() . 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 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 2
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 testCreateTableWithComputedColumn() throws Exception {
	Assume.assumeTrue(planner.equals("blink"));
	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 bigint,\n" +
			"  c as a + 1\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 3
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 4
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateTableWithPropertiesChanged() 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");
		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" +
				"  'update-mode'='append'\n" +
				")\n";
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable1"));
		// Change the session property to trigger `new ExecutionContext`.
		executor.setSessionProperty(sessionId, "execution.restart-strategy.failure-rate-interval", "12345");
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable2"));
		assertEquals(Arrays.asList("MyTable1", "MyTable2"), executor.listTables(sessionId));

		// Reset the session properties.
		executor.resetSessionProperties(sessionId);
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable3"));
		assertEquals(Arrays.asList("MyTable1", "MyTable2", "MyTable3"), executor.listTables(sessionId));
	} finally {
		executor.closeSession(sessionId);
	}
}
 
Example 5
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 6
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testDropTable() 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");
		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" +
				"  'update-mode'='append'\n" +
				")\n";
		// Test drop table.
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable1"));
		assertEquals(Collections.singletonList("MyTable1"), executor.listTables(sessionId));
		executor.dropTable(sessionId, "DROP TABLE MyTable1");
		assertEquals(Collections.emptyList(), executor.listTables(sessionId));

		// Test drop table if exists.
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable1"));
		assertEquals(Collections.singletonList("MyTable1"), executor.listTables(sessionId));
		executor.dropTable(sessionId, "DROP TABLE IF EXISTS MyTable1");
		assertEquals(Collections.emptyList(), executor.listTables(sessionId));

		// Test drop table with full qualified name.
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable1"));
		assertEquals(Collections.singletonList("MyTable1"), executor.listTables(sessionId));
		executor.dropTable(sessionId, "DROP TABLE catalog1.`default`.MyTable1");
		assertEquals(Collections.emptyList(), executor.listTables(sessionId));

		// Test drop table with db and table name.
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable1"));
		assertEquals(Collections.singletonList("MyTable1"), executor.listTables(sessionId));
		executor.dropTable(sessionId, "DROP TABLE `default`.MyTable1");
		assertEquals(Collections.emptyList(), executor.listTables(sessionId));

		// Test drop table that does not exist.
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable1"));
		assertEquals(Collections.singletonList("MyTable1"), executor.listTables(sessionId));
		executor.dropTable(sessionId, "DROP TABLE IF EXISTS catalog2.`default`.MyTable1");
		assertEquals(Collections.singletonList("MyTable1"), executor.listTables(sessionId));
		executor.dropTable(sessionId, "DROP TABLE `default`.MyTable1");

		// Test drop table with properties changed.
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable1"));
		// Change the session property to trigger `new ExecutionContext`.
		executor.setSessionProperty(sessionId, "execution.restart-strategy.failure-rate-interval", "12345");
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable2"));
		assertEquals(Arrays.asList("MyTable1", "MyTable2"), executor.listTables(sessionId));
		executor.dropTable(sessionId, "DROP TABLE MyTable1");
		executor.dropTable(sessionId, "DROP TABLE MyTable2");
		assertEquals(Collections.emptyList(), executor.listTables(sessionId));

		// Test drop table with properties reset.
		// Reset the session properties.
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable1"));
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable2"));
		executor.resetSessionProperties(sessionId);
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable3"));
		assertEquals(Arrays.asList("MyTable1", "MyTable2", "MyTable3"), executor.listTables(sessionId));
		executor.dropTable(sessionId, "DROP TABLE MyTable1");
		executor.dropTable(sessionId, "DROP TABLE MyTable2");
		executor.dropTable(sessionId, "DROP TABLE MyTable3");
		assertEquals(Collections.emptyList(), executor.listTables(sessionId));
	} finally {
		executor.closeSession(sessionId);
	}
}