Java Code Examples for org.apache.flink.table.api.java.BatchTableEnvironment#registerTable()

The following examples show how to use org.apache.flink.table.api.java.BatchTableEnvironment#registerTable() . 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: JavaSqlITCase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testSelectFromTable() throws Exception {
	ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	BatchTableEnvironment tableEnv = BatchTableEnvironment.create(env, config());

	DataSet<Tuple3<Integer, Long, String>> ds = CollectionDataSets.get3TupleDataSet(env);
	Table in = tableEnv.fromDataSet(ds, "a,b,c");
	tableEnv.registerTable("T", in);

	String sqlQuery = "SELECT a, c FROM T";
	Table result = tableEnv.sqlQuery(sqlQuery);

	DataSet<Row> resultSet = tableEnv.toDataSet(result, Row.class);
	List<Row> results = resultSet.collect();
	String expected = "1,Hi\n" + "2,Hello\n" + "3,Hello world\n" +
		"4,Hello world, how are you?\n" + "5,I am fine.\n" + "6,Luke Skywalker\n" +
		"7,Comment#1\n" + "8,Comment#2\n" + "9,Comment#3\n" + "10,Comment#4\n" +
		"11,Comment#5\n" + "12,Comment#6\n" + "13,Comment#7\n" +
		"14,Comment#8\n" + "15,Comment#9\n" + "16,Comment#10\n" +
		"17,Comment#11\n" + "18,Comment#12\n" + "19,Comment#13\n" +
		"20,Comment#14\n" + "21,Comment#15\n";
	compareResultAsText(results, expected);
}
 
Example 2
Source File: JavaTableEnvironmentITCase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testTableRegister() throws Exception {
	final String tableName = "MyTable";
	ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	BatchTableEnvironment tableEnv = BatchTableEnvironment.create(env, config());

	DataSet<Tuple3<Integer, Long, String>> ds = CollectionDataSets.get3TupleDataSet(env);
	Table t = tableEnv.fromDataSet(ds);
	tableEnv.registerTable(tableName, t);
	Table result = tableEnv.scan(tableName).select("f0, f1").filter("f0 > 7");

	DataSet<Row> resultSet = tableEnv.toDataSet(result, Row.class);
	List<Row> results = resultSet.collect();
	String expected = "8,4\n" + "9,4\n" + "10,4\n" + "11,5\n" + "12,5\n" +
			"13,5\n" + "14,5\n" + "15,5\n" +
			"16,6\n" + "17,6\n" + "18,6\n" + "19,6\n" + "20,6\n" + "21,6\n";
	compareResultAsText(results, expected);
}
 
Example 3
Source File: JavaSqlITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testSelectFromTable() throws Exception {
	ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	BatchTableEnvironment tableEnv = BatchTableEnvironment.create(env, config());

	DataSet<Tuple3<Integer, Long, String>> ds = CollectionDataSets.get3TupleDataSet(env);
	Table in = tableEnv.fromDataSet(ds, "a,b,c");
	tableEnv.registerTable("T", in);

	String sqlQuery = "SELECT a, c FROM T";
	Table result = tableEnv.sqlQuery(sqlQuery);

	DataSet<Row> resultSet = tableEnv.toDataSet(result, Row.class);
	List<Row> results = resultSet.collect();
	String expected = "1,Hi\n" + "2,Hello\n" + "3,Hello world\n" +
		"4,Hello world, how are you?\n" + "5,I am fine.\n" + "6,Luke Skywalker\n" +
		"7,Comment#1\n" + "8,Comment#2\n" + "9,Comment#3\n" + "10,Comment#4\n" +
		"11,Comment#5\n" + "12,Comment#6\n" + "13,Comment#7\n" +
		"14,Comment#8\n" + "15,Comment#9\n" + "16,Comment#10\n" +
		"17,Comment#11\n" + "18,Comment#12\n" + "19,Comment#13\n" +
		"20,Comment#14\n" + "21,Comment#15\n";
	compareResultAsText(results, expected);
}
 
Example 4
Source File: JavaTableEnvironmentITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testTableRegister() throws Exception {
	final String tableName = "MyTable";
	ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	BatchTableEnvironment tableEnv = BatchTableEnvironment.create(env, config());

	DataSet<Tuple3<Integer, Long, String>> ds = CollectionDataSets.get3TupleDataSet(env);
	Table t = tableEnv.fromDataSet(ds);
	tableEnv.registerTable(tableName, t);
	Table result = tableEnv.scan(tableName).select("f0, f1").filter("f0 > 7");

	DataSet<Row> resultSet = tableEnv.toDataSet(result, Row.class);
	List<Row> results = resultSet.collect();
	String expected = "8,4\n" + "9,4\n" + "10,4\n" + "11,5\n" + "12,5\n" +
			"13,5\n" + "14,5\n" + "15,5\n" +
			"16,6\n" + "17,6\n" + "18,6\n" + "19,6\n" + "20,6\n" + "21,6\n";
	compareResultAsText(results, expected);
}
 
Example 5
Source File: JavaTableSQLAPI.java    From 163-bigdate-note with GNU General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    ExecutionEnvironment environment = ExecutionEnvironment.getExecutionEnvironment();
    BatchTableEnvironment tableEnvironment = BatchTableEnvironment.getTableEnvironment(environment);
    String filepath = "file:\\D:\\imooc\\新一代大数据计算引擎 Flink从入门到实战-v\\input\\sales.csv";
    //csv => DataSet
    DataSource<Sales> csv = environment.readCsvFile(filepath)
            .ignoreFirstLine()
            .pojoType(Sales.class, "transactionId", "customerId", "itemId", "amountPaid");
    //csv.print();

    Table sales = tableEnvironment.fromDataSet(csv);
    tableEnvironment.registerTable("sales", sales);
    Table resultTable = tableEnvironment.sqlQuery("select customerId, sum(amountPaid) money from sales group by customerId");

    DataSet<Row> result = tableEnvironment.toDataSet(resultTable, Row.class);
    result.print();
}
 
Example 6
Source File: JavaTableEnvironmentITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test(expected = TableException.class)
public void testIllegalName() throws Exception {
	ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	BatchTableEnvironment tableEnv = BatchTableEnvironment.create(env, config());

	DataSet<Tuple3<Integer, Long, String>> ds = CollectionDataSets.get3TupleDataSet(env);
	Table t = tableEnv.fromDataSet(ds);
	// Must fail. Table name matches internal name pattern.
	tableEnv.registerTable("_DataSetTable_42", t);
}
 
Example 7
Source File: JavaTableEnvironmentITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test(expected = TableException.class)
public void testRegisterTableFromOtherEnv() throws Exception {
	ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	BatchTableEnvironment tableEnv1 = BatchTableEnvironment.create(env, config());
	BatchTableEnvironment tableEnv2 = BatchTableEnvironment.create(env, config());

	Table t = tableEnv1.fromDataSet(CollectionDataSets.get3TupleDataSet(env));
	// Must fail. Table is bound to different TableEnvironment.
	tableEnv2.registerTable("MyTable", t);
}
 
Example 8
Source File: JavaTableEnvironmentITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test(expected = ValidationException.class)
public void testIllegalEmptyName() throws Exception {
	ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	BatchTableEnvironment tableEnv = BatchTableEnvironment.create(env, config());

	DataSet<Tuple3<Integer, Long, String>> ds = CollectionDataSets.get3TupleDataSet(env);
	Table t = tableEnv.fromDataSet(ds);
	// Must fail. Table is empty
	tableEnv.registerTable("", t);
}
 
Example 9
Source File: JavaTableEnvironmentITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test(expected = ValidationException.class)
public void testIllegalWhitespaceOnlyName() throws Exception {
	ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	BatchTableEnvironment tableEnv = BatchTableEnvironment.create(env, config());

	DataSet<Tuple3<Integer, Long, String>> ds = CollectionDataSets.get3TupleDataSet(env);
	Table t = tableEnv.fromDataSet(ds);
	// Must fail. Table is empty
	tableEnv.registerTable("     ", t);
}
 
Example 10
Source File: JavaTableEnvironmentITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test(expected = TableException.class)
public void testRegisterTableFromOtherEnv() throws Exception {
	ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	BatchTableEnvironment tableEnv1 = BatchTableEnvironment.create(env, config());
	BatchTableEnvironment tableEnv2 = BatchTableEnvironment.create(env, config());

	Table t = tableEnv1.fromDataSet(CollectionDataSets.get3TupleDataSet(env));
	// Must fail. Table is bound to different TableEnvironment.
	tableEnv2.registerTable("MyTable", t);
}
 
Example 11
Source File: DocWordSplitCountTest.java    From Alink with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws Exception {
    BatchTableEnvironment environment = MLEnvironmentFactory.getDefault().getBatchTableEnvironment();
    DataSet<Row> input = MLEnvironmentFactory.getDefault().getExecutionEnvironment().fromElements(Row.of("a b c d a b c"));
    Table table = environment.fromDataSet(input);
    environment.registerFunction("DocWordSplitCount", new DocWordSplitCount(" "));
    environment.registerTable("myTable", table);
    List<Row> list = environment.toDataSet(
        environment.sqlQuery("SELECT w, cnt FROM myTable, LATERAL TABLE(DocWordSplitCount(f0)) as T(w, cnt)"),
        Row.class).collect();
    Assert.assertArrayEquals(list.toArray(),
        new Row[] {Row.of("a", 2L), Row.of("b", 2L), Row.of("c", 2L), Row.of("d", 1L)});
}
 
Example 12
Source File: UdfDemo.java    From flink-simple-tutorial with Apache License 2.0 3 votes vote down vote up
public static void main(String[] args) throws Exception {


        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
        BatchTableEnvironment tEnv = BatchTableEnvironment.create(env);

        DataSource<String> ds = env.fromElements("Hello#aaaaa","bbbb#ccc");
        DataSource<Tuple2<String,Integer>> meanDataset
                = env.fromElements(new Tuple2<>("a",2),new Tuple2<>("b",3),new Tuple2<>("a",8));

        Table table = tEnv.fromDataSet(ds,"a");
        tEnv.registerTable("t",table);

        Table t3 = tEnv.fromDataSet(meanDataset,"name,v");
        tEnv.registerTable("t3",t3);

        tEnv.registerFunction("stringLength",new StringLength());
        tEnv.registerFunction("split", new StringSplit());
        tEnv.registerFunction("get_mean", new Mean());

        //tEnv.registerFunction("top2", new Top2());


        //Table result = tEnv.sqlQuery("select word,stringLength(word) from " + table);
        //Table result = tEnv.sqlQuery("SELECT a,word, length FROM t, LATERAL TABLE(split(a)) as T(word, length)");
        Table result = tEnv.sqlQuery("SELECT name,get_mean(v) as mean_value FROM t3 GROUP BY name");


        //TypeInformation<Tuple3<String,String,Integer>> tpinf = new TypeHint<Tuple3<String,String,Integer>>(){}.getTypeInfo();
        //TypeInformation<String> tpinf = new TypeHint<String>(){}.getTypeInfo();
        TypeInformation<Tuple2<String,Integer>> tpinf = new TypeHint<Tuple2<String,Integer>>(){}.getTypeInfo();
        tEnv.toDataSet(result,tpinf).print();



    }
 
Example 13
Source File: BatchSqlOperators.java    From Alink with Apache License 2.0 3 votes vote down vote up
/**
 * Register the output table of a BatchOperator to the {@link BatchTableEnvironment}
 * with a temporary table name.
 *
 * @param batchOp The BatchOperator who's output table is being registered.
 * @return The temporary table name.
 */
private static String registerTempTable(BatchOperator batchOp) {
    BatchTableEnvironment tEnv = getMLEnv(batchOp).getBatchTableEnvironment();
    String tmpTableName = TableUtil.getTempTableName();
    tEnv.registerTable(tmpTableName, batchOp.getOutputTable());
    return tmpTableName;
}