Java Code Examples for org.apache.flink.table.api.TableEnvironment#registerCatalog()

The following examples show how to use org.apache.flink.table.api.TableEnvironment#registerCatalog() . 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: HiveTableSourceTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testReadNonPartitionedTable() throws Exception {
	final String catalogName = "hive";
	final String dbName = "source_db";
	final String tblName = "test";
	hiveShell.execute("CREATE TABLE source_db.test ( a INT, b INT, c STRING, d BIGINT, e DOUBLE)");
	hiveShell.insertInto(dbName, tblName)
			.withAllColumns()
			.addRow(1, 1, "a", 1000L, 1.11)
			.addRow(2, 2, "b", 2000L, 2.22)
			.addRow(3, 3, "c", 3000L, 3.33)
			.addRow(4, 4, "d", 4000L, 4.44)
			.commit();

	TableEnvironment tEnv = HiveTestUtils.createTableEnv();
	tEnv.registerCatalog(catalogName, hiveCatalog);
	Table src = tEnv.sqlQuery("select * from hive.source_db.test");
	List<Row> rows = JavaConverters.seqAsJavaListConverter(TableUtil.collect((TableImpl) src)).asJava();

	Assert.assertEquals(4, rows.size());
	Assert.assertEquals("1,1,a,1000,1.11", rows.get(0).toString());
	Assert.assertEquals("2,2,b,2000,2.22", rows.get(1).toString());
	Assert.assertEquals("3,3,c,3000,3.33", rows.get(2).toString());
	Assert.assertEquals("4,4,d,4000,4.44", rows.get(3).toString());
}
 
Example 2
Source File: HiveTableSourceTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testReadComplexDataType() throws Exception {
	final String catalogName = "hive";
	final String dbName = "source_db";
	final String tblName = "complex_test";
	hiveShell.execute("create table source_db.complex_test(" +
					"a array<int>, m map<int,string>, s struct<f1:int,f2:bigint>)");
	Integer[] array = new Integer[]{1, 2, 3};
	Map<Integer, String> map = new LinkedHashMap<>();
	map.put(1, "a");
	map.put(2, "b");
	Object[] struct = new Object[]{3, 3L};
	hiveShell.insertInto(dbName, tblName)
			.withAllColumns()
			.addRow(array, map, struct)
			.commit();
	TableEnvironment tEnv = HiveTestUtils.createTableEnv();
	tEnv.registerCatalog(catalogName, hiveCatalog);
	Table src = tEnv.sqlQuery("select * from hive.source_db.complex_test");
	List<Row> rows = JavaConverters.seqAsJavaListConverter(TableUtil.collect((TableImpl) src)).asJava();
	Assert.assertEquals(1, rows.size());
	assertArrayEquals(array, (Integer[]) rows.get(0).getField(0));
	assertEquals(map, rows.get(0).getField(1));
	assertEquals(Row.of(struct[0], struct[1]), rows.get(0).getField(2));
}
 
Example 3
Source File: HiveTableSourceTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Test to read from partition table.
 * @throws Exception
 */
@Test
public void testReadPartitionTable() throws Exception {
	final String catalogName = "hive";
	final String dbName = "source_db";
	final String tblName = "test_table_pt";
	hiveShell.execute("CREATE TABLE source_db.test_table_pt " +
					"(year STRING, value INT) partitioned by (pt int);");
	hiveShell.insertInto(dbName, tblName)
			.withColumns("year", "value", "pt")
			.addRow("2014", 3, 0)
			.addRow("2014", 4, 0)
			.addRow("2015", 2, 1)
			.addRow("2015", 5, 1)
			.commit();
	TableEnvironment tEnv = HiveTestUtils.createTableEnv();
	tEnv.registerCatalog(catalogName, hiveCatalog);
	Table src = tEnv.sqlQuery("select * from hive.source_db.test_table_pt");
	List<Row> rows = JavaConverters.seqAsJavaListConverter(TableUtil.collect((TableImpl) src)).asJava();

	assertEquals(4, rows.size());
	Object[] rowStrings = rows.stream().map(Row::toString).sorted().toArray();
	assertArrayEquals(new String[]{"2014,3,0", "2014,4,0", "2015,2,1", "2015,5,1"}, rowStrings);
}
 
Example 4
Source File: HiveTableSinkITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testInsertIntoNonPartitionTable() throws Exception {
	String dbName = "default";
	String tblName = "dest";
	RowTypeInfo rowTypeInfo = createHiveDestTable(dbName, tblName, 0);
	ObjectPath tablePath = new ObjectPath(dbName, tblName);

	TableEnvironment tableEnv = HiveTestUtils.createTableEnvWithBlinkPlannerBatchMode();
	List<Row> toWrite = generateRecords(5);
	Table src = tableEnv.fromTableSource(new CollectionTableSource(toWrite, rowTypeInfo));
	tableEnv.registerTable("src", src);

	tableEnv.registerCatalog("hive", hiveCatalog);
	TableEnvUtil.execInsertTableAndWaitResult(tableEnv.sqlQuery("select * from src"), "hive.`default`.dest");

	verifyWrittenData(toWrite, hiveShell.executeQuery("select * from " + tblName));

	hiveCatalog.dropTable(tablePath, false);
}
 
Example 5
Source File: HiveTableSinkTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testInsertIntoNonPartitionTable() throws Exception {
	String dbName = "default";
	String tblName = "dest";
	RowTypeInfo rowTypeInfo = createDestTable(dbName, tblName, 0);
	ObjectPath tablePath = new ObjectPath(dbName, tblName);

	TableEnvironment tableEnv = HiveTestUtils.createTableEnv();
	List<Row> toWrite = generateRecords(5);
	Table src = tableEnv.fromTableSource(new CollectionTableSource(toWrite, rowTypeInfo));
	tableEnv.registerTable("src", src);

	tableEnv.registerCatalog("hive", hiveCatalog);
	tableEnv.sqlQuery("select * from src").insertInto("hive", "default", "dest");
	tableEnv.execute("mytest");

	verifyWrittenData(toWrite, hiveShell.executeQuery("select * from " + tblName));

	hiveCatalog.dropTable(tablePath, false);
}
 
Example 6
Source File: JavaCatalogTableTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testResolvingSchemaOfCustomCatalogTableTableApi() throws Exception {
	TableTestUtil testUtil = getTestUtil();
	TableEnvironment tableEnvironment = testUtil.getTableEnv();
	GenericInMemoryCatalog genericInMemoryCatalog = new GenericInMemoryCatalog("in-memory");
	genericInMemoryCatalog.createTable(
		new ObjectPath("default", "testTable"),
		new CustomCatalogTable(isStreamingMode),
		false);
	tableEnvironment.registerCatalog("testCatalog", genericInMemoryCatalog);

	Table table = tableEnvironment.from("testCatalog.`default`.testTable")
		.window(Tumble.over(lit(10).minute()).on($("rowtime")).as("w"))
		.groupBy($("w"))
		.select(lit(1).count());
	testUtil.verifyPlan(table);
}
 
Example 7
Source File: HiveCatalogUseBlinkITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testDateUDF() throws Exception {

	TableEnvironment tableEnv = HiveTestUtils.createTableEnvWithBlinkPlannerBatchMode(SqlDialect.HIVE);
	tableEnv.registerCatalog(hiveCatalog.getName(), hiveCatalog);
	tableEnv.useCatalog(hiveCatalog.getName());
	tableEnv.executeSql(String.format("create function mymonth as '%s'", UDFMonth.class.getName()));
	tableEnv.executeSql("create table src(dt date)");
	try {
		HiveTestUtils.createTextTableInserter(hiveShell, "default", "src")
				.addRow(new Object[]{Date.valueOf("2019-01-19")})
				.addRow(new Object[]{Date.valueOf("2019-03-02")})
				.commit();

		List<Row> results = Lists.newArrayList(
				tableEnv.sqlQuery("select mymonth(dt) as m from src order by m").execute().collect());
		Assert.assertEquals(2, results.size());
		Assert.assertEquals("[1, 3]", results.toString());
	} finally {
		tableEnv.executeSql("drop table src");
	}
}
 
Example 8
Source File: HiveCatalogUseBlinkITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testTimestampUDF() throws Exception {

	TableEnvironment tableEnv = HiveTestUtils.createTableEnvWithBlinkPlannerBatchMode(SqlDialect.HIVE);
	tableEnv.registerCatalog(hiveCatalog.getName(), hiveCatalog);
	tableEnv.useCatalog(hiveCatalog.getName());
	tableEnv.executeSql(String.format("create function myyear as '%s'", UDFYear.class.getName()));
	tableEnv.executeSql("create table src(ts timestamp)");
	try {
		HiveTestUtils.createTextTableInserter(hiveShell, "default", "src")
				.addRow(new Object[]{Timestamp.valueOf("2013-07-15 10:00:00")})
				.addRow(new Object[]{Timestamp.valueOf("2019-05-23 17:32:55")})
				.commit();

		List<Row> results = Lists.newArrayList(
				tableEnv.sqlQuery("select myyear(ts) as y from src").execute().collect());
		Assert.assertEquals(2, results.size());
		Assert.assertEquals("[2013, 2019]", results.toString());
	} finally {
		tableEnv.executeSql("drop table src");
	}
}
 
Example 9
Source File: TableEnvHiveConnectorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
private TableEnvironment getStreamTableEnvWithHiveCatalog() {
	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
	TableEnvironment tableEnv = HiveTestUtils.createTableEnvWithBlinkPlannerStreamMode(env, SqlDialect.HIVE);
	tableEnv.registerCatalog(hiveCatalog.getName(), hiveCatalog);
	tableEnv.useCatalog(hiveCatalog.getName());
	return tableEnv;
}
 
Example 10
Source File: HiveCatalogITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testNewTableFactory() {
	TableEnvironment tEnv = TableEnvironment.create(
			EnvironmentSettings.newInstance().inBatchMode().build());
	tEnv.registerCatalog("myhive", hiveCatalog);
	tEnv.useCatalog("myhive");
	tEnv.getConfig().getConfiguration().set(TABLE_EXEC_RESOURCE_DEFAULT_PARALLELISM, 1);

	String path = this.getClass().getResource("/csv/test.csv").getPath();

	PrintStream originalSystemOut = System.out;
	try {
		ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
		System.setOut(new PrintStream(arrayOutputStream));

		tEnv.executeSql("create table csv_table (name String, age Int) with (" +
				"'connector.type' = 'filesystem'," +
				"'connector.path' = 'file://" + path + "'," +
				"'format.type' = 'csv')");
		tEnv.executeSql("create table print_table (name String, age Int) with ('connector' = 'print')");

		TableEnvUtil.execInsertSqlAndWaitResult(tEnv, "insert into print_table select * from csv_table");

		// assert query result
		assertEquals("+I(1,1)\n+I(2,2)\n+I(3,3)\n", arrayOutputStream.toString());
	} finally {
		if (System.out != originalSystemOut) {
			System.out.close();
		}
		System.setOut(originalSystemOut);
		tEnv.executeSql("DROP TABLE csv_table");
		tEnv.executeSql("DROP TABLE print_table");
	}
}
 
Example 11
Source File: HiveCatalogITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testTableWithPrimaryKey() {
	EnvironmentSettings.Builder builder = EnvironmentSettings.newInstance().useBlinkPlanner();
	EnvironmentSettings settings = builder.build();
	TableEnvironment tableEnv = TableEnvironment.create(settings);
	tableEnv.getConfig().getConfiguration().setInteger(TABLE_EXEC_RESOURCE_DEFAULT_PARALLELISM, 1);

	tableEnv.registerCatalog("catalog1", hiveCatalog);
	tableEnv.useCatalog("catalog1");

	final String createTable = "CREATE TABLE pk_src (\n" +
			"  uuid varchar(40) not null,\n" +
			"  price DECIMAL(10, 2),\n" +
			"  currency STRING,\n" +
			"  ts6 TIMESTAMP(6),\n" +
			"  ts AS CAST(ts6 AS TIMESTAMP(3)),\n" +
			"  WATERMARK FOR ts AS ts,\n" +
			"  constraint ct1 PRIMARY KEY(uuid) NOT ENFORCED)\n" +
			"  WITH (\n" +
			"    'connector.type' = 'filesystem'," +
			"    'connector.path' = 'file://fakePath'," +
			"    'format.type' = 'csv')";

	tableEnv.executeSql(createTable);

	TableSchema tableSchema = tableEnv.getCatalog(tableEnv.getCurrentCatalog())
			.map(catalog -> {
				try {
					final ObjectPath tablePath = ObjectPath.fromString(catalog.getDefaultDatabase() + '.' + "pk_src");
					return catalog.getTable(tablePath).getSchema();
				} catch (TableNotExistException e) {
					return null;
				}
			}).orElse(null);
	assertNotNull(tableSchema);
	assertEquals(
			tableSchema.getPrimaryKey(),
			Optional.of(UniqueConstraint.primaryKey("ct1", Collections.singletonList("uuid"))));
	tableEnv.executeSql("DROP TABLE pk_src");
}
 
Example 12
Source File: HiveTableSourceITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
private void testSourceConfig(boolean fallbackMR, boolean inferParallelism) throws Exception {
	HiveTableFactory tableFactorySpy = spy((HiveTableFactory) hiveCatalog.getTableFactory().get());

	doAnswer(invocation -> {
		TableSourceFactory.Context context = invocation.getArgument(0);
		return new TestConfigSource(
				new JobConf(hiveCatalog.getHiveConf()),
				context.getConfiguration(),
				context.getObjectIdentifier().toObjectPath(),
				context.getTable(),
				fallbackMR,
				inferParallelism);
	}).when(tableFactorySpy).createTableSource(any(TableSourceFactory.Context.class));

	HiveCatalog catalogSpy = spy(hiveCatalog);
	doReturn(Optional.of(tableFactorySpy)).when(catalogSpy).getTableFactory();

	TableEnvironment tableEnv = HiveTestUtils.createTableEnvWithBlinkPlannerBatchMode();
	tableEnv.getConfig().getConfiguration().setBoolean(
			HiveOptions.TABLE_EXEC_HIVE_FALLBACK_MAPRED_READER, fallbackMR);
	tableEnv.getConfig().getConfiguration().setBoolean(
			HiveOptions.TABLE_EXEC_HIVE_INFER_SOURCE_PARALLELISM, inferParallelism);
	tableEnv.getConfig().getConfiguration().setInteger(
			ExecutionConfigOptions.TABLE_EXEC_RESOURCE_DEFAULT_PARALLELISM, 2);
	tableEnv.registerCatalog(catalogSpy.getName(), catalogSpy);
	tableEnv.useCatalog(catalogSpy.getName());

	List<Row> results = Lists.newArrayList(
			tableEnv.sqlQuery("select * from db1.src order by x").execute().collect());
	assertEquals("[1,a, 2,b]", results.toString());
}
 
Example 13
Source File: JavaCatalogTableTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testResolvingSchemaOfCustomCatalogTableSql() throws Exception {
	TableTestUtil testUtil = getTestUtil();
	TableEnvironment tableEnvironment = testUtil.getTableEnv();
	GenericInMemoryCatalog genericInMemoryCatalog = new GenericInMemoryCatalog("in-memory");
	genericInMemoryCatalog.createTable(
		new ObjectPath("default", "testTable"),
		new CustomCatalogTable(isStreamingMode),
		false);
	tableEnvironment.registerCatalog("testCatalog", genericInMemoryCatalog);
	tableEnvironment.executeSql("CREATE VIEW testTable2 AS SELECT * FROM testCatalog.`default`.testTable");

	testUtil.verifyPlan(
		"SELECT COUNT(*) FROM testTable2 GROUP BY TUMBLE(rowtime, INTERVAL '10' MINUTE)");
}
 
Example 14
Source File: HiveTableSourceITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testPartitionFilterDateTimestamp() throws Exception {
	TableEnvironment tableEnv = HiveTestUtils.createTableEnvWithBlinkPlannerBatchMode(SqlDialect.HIVE);
	TestPartitionFilterCatalog catalog = new TestPartitionFilterCatalog(
			hiveCatalog.getName(), hiveCatalog.getDefaultDatabase(), hiveCatalog.getHiveConf(), hiveCatalog.getHiveVersion());
	tableEnv.registerCatalog(catalog.getName(), catalog);
	tableEnv.useCatalog(catalog.getName());
	tableEnv.executeSql("create database db1");
	try {
		tableEnv.executeSql("create table db1.part(x int) partitioned by (p1 date,p2 timestamp)");
		HiveTestUtils.createTextTableInserter(hiveShell, "db1", "part")
				.addRow(new Object[]{1}).commit("p1='2018-08-08',p2='2018-08-08 08:08:08'");
		HiveTestUtils.createTextTableInserter(hiveShell, "db1", "part")
				.addRow(new Object[]{2}).commit("p1='2018-08-09',p2='2018-08-08 08:08:09'");
		HiveTestUtils.createTextTableInserter(hiveShell, "db1", "part")
				.addRow(new Object[]{3}).commit("p1='2018-08-10',p2='2018-08-08 08:08:10'");

		Table query = tableEnv.sqlQuery(
				"select x from db1.part where p1>cast('2018-08-09' as date) and p2<>cast('2018-08-08 08:08:09' as timestamp)");
		String[] explain = query.explain().split("==.*==\n");
		assertTrue(catalog.fallback);
		String optimizedPlan = explain[2];
		assertTrue(optimizedPlan, optimizedPlan.contains("PartitionPruned: true, PartitionNums: 1"));
		List<Row> results = Lists.newArrayList(query.execute().collect());
		assertEquals("[3]", results.toString());
		System.out.println(results);
	} finally {
		tableEnv.executeSql("drop database db1 cascade");
	}
}
 
Example 15
Source File: HiveTableSinkITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
private void assertBatch(String table, List<String> expected) {
	// using batch table env to query.
	List<String> results = new ArrayList<>();
	TableEnvironment batchTEnv = HiveTestUtils.createTableEnvWithBlinkPlannerBatchMode();
	batchTEnv.registerCatalog(hiveCatalog.getName(), hiveCatalog);
	batchTEnv.useCatalog(hiveCatalog.getName());
	batchTEnv.executeSql("select * from " + table).collect()
			.forEachRemaining(r -> results.add(r.toString()));
	results.sort(String::compareTo);
	expected.sort(String::compareTo);
	Assert.assertEquals(expected, results);
}
 
Example 16
Source File: HiveCatalogITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testCsvTableViaAPI() throws Exception {
	EnvironmentSettings settings = EnvironmentSettings.newInstance().useBlinkPlanner().inBatchMode().build();
	TableEnvironment tableEnv = TableEnvironment.create(settings);
	tableEnv.getConfig().addConfiguration(new Configuration().set(CoreOptions.DEFAULT_PARALLELISM, 1));

	tableEnv.registerCatalog("myhive", hiveCatalog);
	tableEnv.useCatalog("myhive");

	TableSchema schema = TableSchema.builder()
		.field("name", DataTypes.STRING())
		.field("age", DataTypes.INT())
		.build();

	FormatDescriptor format = new OldCsv()
		.field("name", Types.STRING())
		.field("age", Types.INT());

	CatalogTable source =
		new CatalogTableBuilder(
			new FileSystem().path(this.getClass().getResource("/csv/test.csv").getPath()),
			schema)
		.withFormat(format)
		.inAppendMode()
		.withComment("Comment.")
		.build();

	Path p = Paths.get(tempFolder.newFolder().getAbsolutePath(), "test.csv");

	CatalogTable sink =
		new CatalogTableBuilder(
			new FileSystem().path(p.toAbsolutePath().toString()),
			schema)
			.withFormat(format)
			.inAppendMode()
			.withComment("Comment.")
			.build();

	hiveCatalog.createTable(
		new ObjectPath(HiveCatalog.DEFAULT_DB, sourceTableName),
		source,
		false
	);

	hiveCatalog.createTable(
		new ObjectPath(HiveCatalog.DEFAULT_DB, sinkTableName),
		sink,
		false
	);

	Table t = tableEnv.sqlQuery(
		String.format("select * from myhive.`default`.%s", sourceTableName));

	List<Row> result = Lists.newArrayList(t.execute().collect());
	result.sort(Comparator.comparing(String::valueOf));

	// assert query result
	assertEquals(
		Arrays.asList(
			Row.of("1", 1),
			Row.of("2", 2),
			Row.of("3", 3)),
		result
	);

	TableEnvUtil.execInsertSqlAndWaitResult(tableEnv,
		String.format("insert into myhive.`default`.%s select * from myhive.`default`.%s",
			sinkTableName,
			sourceTableName));

	// assert written result
	File resultFile = new File(p.toAbsolutePath().toString());
	BufferedReader reader = new BufferedReader(new FileReader(resultFile));
	String readLine;
	for (int i = 0; i < 3; i++) {
		readLine = reader.readLine();
		assertEquals(String.format("%d,%d", i + 1, i + 1), readLine);
	}

	// No more line
	assertNull(reader.readLine());

	tableEnv.executeSql(String.format("DROP TABLE %s", sourceTableName));
	tableEnv.executeSql(String.format("DROP TABLE %s", sinkTableName));
}
 
Example 17
Source File: HiveCatalogITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testCsvTableViaSQL() throws Exception {
	EnvironmentSettings settings = EnvironmentSettings.newInstance().useBlinkPlanner().inBatchMode().build();
	TableEnvironment tableEnv = TableEnvironment.create(settings);

	tableEnv.registerCatalog("myhive", hiveCatalog);
	tableEnv.useCatalog("myhive");

	String path = this.getClass().getResource("/csv/test.csv").getPath();

	tableEnv.executeSql("create table test2 (name String, age Int) with (\n" +
		"   'connector.type' = 'filesystem',\n" +
		"   'connector.path' = 'file://" + path + "',\n" +
		"   'format.type' = 'csv'\n" +
		")");

	Table t = tableEnv.sqlQuery("SELECT * FROM myhive.`default`.test2");

	List<Row> result = Lists.newArrayList(t.execute().collect());

	// assert query result
	assertEquals(
		new HashSet<>(Arrays.asList(
			Row.of("1", 1),
			Row.of("2", 2),
			Row.of("3", 3))),
		new HashSet<>(result)
	);

	tableEnv.executeSql("ALTER TABLE test2 RENAME TO newtable");

	t = tableEnv.sqlQuery("SELECT * FROM myhive.`default`.newtable");

	result = Lists.newArrayList(t.execute().collect());

	// assert query result
	assertEquals(
		new HashSet<>(Arrays.asList(
			Row.of("1", 1),
			Row.of("2", 2),
			Row.of("3", 3))),
		new HashSet<>(result)
	);

	tableEnv.executeSql("DROP TABLE newtable");
}
 
Example 18
Source File: HiveTestUtils.java    From flink with Apache License 2.0 4 votes vote down vote up
public static TableEnvironment createTableEnvWithHiveCatalog(HiveCatalog catalog) {
	TableEnvironment tableEnv = HiveTestUtils.createTableEnvWithBlinkPlannerBatchMode();
	tableEnv.registerCatalog(catalog.getName(), catalog);
	tableEnv.useCatalog(catalog.getName());
	return tableEnv;
}
 
Example 19
Source File: HiveTableSourceITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testPartitionFilter() throws Exception {
	TableEnvironment tableEnv = HiveTestUtils.createTableEnvWithBlinkPlannerBatchMode(SqlDialect.HIVE);
	TestPartitionFilterCatalog catalog = new TestPartitionFilterCatalog(
			hiveCatalog.getName(), hiveCatalog.getDefaultDatabase(), hiveCatalog.getHiveConf(), hiveCatalog.getHiveVersion());
	tableEnv.registerCatalog(catalog.getName(), catalog);
	tableEnv.useCatalog(catalog.getName());
	tableEnv.executeSql("create database db1");
	try {
		tableEnv.executeSql("create table db1.part(x int) partitioned by (p1 int,p2 string)");
		HiveTestUtils.createTextTableInserter(hiveShell, "db1", "part")
				.addRow(new Object[]{1}).commit("p1=1,p2='a'");
		HiveTestUtils.createTextTableInserter(hiveShell, "db1", "part")
				.addRow(new Object[]{2}).commit("p1=2,p2='b'");
		HiveTestUtils.createTextTableInserter(hiveShell, "db1", "part")
				.addRow(new Object[]{3}).commit("p1=3,p2='c'");
		// test string partition columns with special characters
		HiveTestUtils.createTextTableInserter(hiveShell, "db1", "part")
				.addRow(new Object[]{4}).commit("p1=4,p2='c:2'");
		Table query = tableEnv.sqlQuery("select x from db1.part where p1>1 or p2<>'a' order by x");
		String[] explain = query.explain().split("==.*==\n");
		assertFalse(catalog.fallback);
		String optimizedPlan = explain[2];
		assertTrue(optimizedPlan, optimizedPlan.contains("PartitionPruned: true, PartitionNums: 3"));
		List<Row> results = Lists.newArrayList(query.execute().collect());
		assertEquals("[2, 3, 4]", results.toString());

		query = tableEnv.sqlQuery("select x from db1.part where p1>2 and p2<='a' order by x");
		explain = query.explain().split("==.*==\n");
		assertFalse(catalog.fallback);
		optimizedPlan = explain[2];
		assertTrue(optimizedPlan, optimizedPlan.contains("PartitionPruned: true, PartitionNums: 0"));
		results = Lists.newArrayList(query.execute().collect());
		assertEquals("[]", results.toString());

		query = tableEnv.sqlQuery("select x from db1.part where p1 in (1,3,5) order by x");
		explain = query.explain().split("==.*==\n");
		assertFalse(catalog.fallback);
		optimizedPlan = explain[2];
		assertTrue(optimizedPlan, optimizedPlan.contains("PartitionPruned: true, PartitionNums: 2"));
		results = Lists.newArrayList(query.execute().collect());
		assertEquals("[1, 3]", results.toString());

		query = tableEnv.sqlQuery("select x from db1.part where (p1=1 and p2='a') or ((p1=2 and p2='b') or p2='d') order by x");
		explain = query.explain().split("==.*==\n");
		assertFalse(catalog.fallback);
		optimizedPlan = explain[2];
		assertTrue(optimizedPlan, optimizedPlan.contains("PartitionPruned: true, PartitionNums: 2"));
		results = Lists.newArrayList(query.execute().collect());
		assertEquals("[1, 2]", results.toString());

		query = tableEnv.sqlQuery("select x from db1.part where p2 = 'c:2' order by x");
		explain = query.explain().split("==.*==\n");
		assertFalse(catalog.fallback);
		optimizedPlan = explain[2];
		assertTrue(optimizedPlan, optimizedPlan.contains("PartitionPruned: true, PartitionNums: 1"));
		results = Lists.newArrayList(query.execute().collect());
		assertEquals("[4]", results.toString());
	} finally {
		tableEnv.executeSql("drop database db1 cascade");
	}
}
 
Example 20
Source File: HiveTableSinkTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testWriteComplexType() throws Exception {
	String dbName = "default";
	String tblName = "dest";
	ObjectPath tablePath = new ObjectPath(dbName, tblName);

	TableSchema.Builder builder = new TableSchema.Builder();
	builder.fields(new String[]{"a", "m", "s"}, new DataType[]{
			DataTypes.ARRAY(DataTypes.INT()),
			DataTypes.MAP(DataTypes.INT(), DataTypes.STRING()),
			DataTypes.ROW(DataTypes.FIELD("f1", DataTypes.INT()), DataTypes.FIELD("f2", DataTypes.STRING()))});

	RowTypeInfo rowTypeInfo = createDestTable(dbName, tblName, builder.build(), 0);
	List<Row> toWrite = new ArrayList<>();
	Row row = new Row(rowTypeInfo.getArity());
	Object[] array = new Object[]{1, 2, 3};
	Map<Integer, String> map = new HashMap<Integer, String>() {{
		put(1, "a");
		put(2, "b");
	}};
	Row struct = new Row(2);
	struct.setField(0, 3);
	struct.setField(1, "c");

	row.setField(0, array);
	row.setField(1, map);
	row.setField(2, struct);
	toWrite.add(row);

	TableEnvironment tableEnv = HiveTestUtils.createTableEnv();
	Table src = tableEnv.fromTableSource(new CollectionTableSource(toWrite, rowTypeInfo));
	tableEnv.registerTable("complexSrc", src);

	tableEnv.registerCatalog("hive", hiveCatalog);
	tableEnv.sqlQuery("select * from complexSrc").insertInto("hive", "default", "dest");
	tableEnv.execute("mytest");

	List<String> result = hiveShell.executeQuery("select * from " + tblName);
	assertEquals(1, result.size());
	assertEquals("[1,2,3]\t{1:\"a\",2:\"b\"}\t{\"f1\":3,\"f2\":\"c\"}", result.get(0));

	hiveCatalog.dropTable(tablePath, false);
}