Search in sources :

Example 11 with BatchTableEnvironment

use of org.apache.flink.table.api.java.BatchTableEnvironment in project flink by apache.

the class SqlITCase method testSelectFromTable.

@Test
public void testSelectFromTable() throws Exception {
    ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    BatchTableEnvironment tableEnv = TableEnvironment.getTableEnvironment(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.sql(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);
}
Also used : ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) Table(org.apache.flink.table.api.Table) Tuple3(org.apache.flink.api.java.tuple.Tuple3) Row(org.apache.flink.types.Row) BatchTableEnvironment(org.apache.flink.table.api.java.BatchTableEnvironment) Test(org.junit.Test)

Example 12 with BatchTableEnvironment

use of org.apache.flink.table.api.java.BatchTableEnvironment in project flink by apache.

the class SqlITCase method testAggregation.

@Test
public void testAggregation() throws Exception {
    ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    BatchTableEnvironment tableEnv = TableEnvironment.getTableEnvironment(env, config());
    DataSet<Tuple3<Integer, Long, String>> ds = CollectionDataSets.get3TupleDataSet(env);
    tableEnv.registerDataSet("AggTable", ds, "x, y, z");
    String sqlQuery = "SELECT sum(x), min(x), max(x), count(y), avg(x) FROM AggTable";
    Table result = tableEnv.sql(sqlQuery);
    DataSet<Row> resultSet = tableEnv.toDataSet(result, Row.class);
    List<Row> results = resultSet.collect();
    String expected = "231,1,21,21,11";
    compareResultAsText(results, expected);
}
Also used : ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) Table(org.apache.flink.table.api.Table) Tuple3(org.apache.flink.api.java.tuple.Tuple3) Row(org.apache.flink.types.Row) BatchTableEnvironment(org.apache.flink.table.api.java.BatchTableEnvironment) Test(org.junit.Test)

Example 13 with BatchTableEnvironment

use of org.apache.flink.table.api.java.BatchTableEnvironment in project flink by apache.

the class SqlITCase method testValues.

@Test
public void testValues() throws Exception {
    ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    BatchTableEnvironment tableEnv = TableEnvironment.getTableEnvironment(env, config());
    String sqlQuery = "VALUES (1, 'Test', TRUE, DATE '1944-02-24', 12.4444444444444445)," + "(2, 'Hello', TRUE, DATE '1944-02-24', 12.666666665)," + "(3, 'World', FALSE, DATE '1944-12-24', 12.54444445)";
    Table result = tableEnv.sql(sqlQuery);
    DataSet<Row> resultSet = tableEnv.toDataSet(result, Row.class);
    List<Row> results = resultSet.collect();
    String expected = "3,World,false,1944-12-24,12.5444444500000000\n" + "2,Hello,true,1944-02-24,12.6666666650000000\n" + // Calcite converts to decimals and strings with equal length
    "1,Test ,true,1944-02-24,12.4444444444444445\n";
    compareResultAsText(results, expected);
}
Also used : ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) Table(org.apache.flink.table.api.Table) Row(org.apache.flink.types.Row) BatchTableEnvironment(org.apache.flink.table.api.java.BatchTableEnvironment) Test(org.junit.Test)

Example 14 with BatchTableEnvironment

use of org.apache.flink.table.api.java.BatchTableEnvironment in project flink by apache.

the class TableEnvironmentITCase method testNonStaticClassOutput.

@Test(expected = TableException.class)
public void testNonStaticClassOutput() throws Exception {
    ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    BatchTableEnvironment tableEnv = TableEnvironment.getTableEnvironment(env, config());
    // Must fail since class is not static
    Table t = tableEnv.fromDataSet(env.fromElements(1, 2, 3), "number");
    tableEnv.toDataSet(t, MyNonStatic.class);
}
Also used : ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) Table(org.apache.flink.table.api.Table) BatchTableEnvironment(org.apache.flink.table.api.java.BatchTableEnvironment) Test(org.junit.Test)

Example 15 with BatchTableEnvironment

use of org.apache.flink.table.api.java.BatchTableEnvironment in project flink by apache.

the class TableEnvironmentITCase method testRegisterTableFromOtherEnv.

@Test(expected = TableException.class)
public void testRegisterTableFromOtherEnv() throws Exception {
    ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    BatchTableEnvironment tableEnv1 = TableEnvironment.getTableEnvironment(env, config());
    BatchTableEnvironment tableEnv2 = TableEnvironment.getTableEnvironment(env, config());
    Table t = tableEnv1.fromDataSet(CollectionDataSets.get3TupleDataSet(env));
    // Must fail. Table is bound to different TableEnvironment.
    tableEnv2.registerTable("MyTable", t);
}
Also used : ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) Table(org.apache.flink.table.api.Table) BatchTableEnvironment(org.apache.flink.table.api.java.BatchTableEnvironment) Test(org.junit.Test)

Aggregations

ExecutionEnvironment (org.apache.flink.api.java.ExecutionEnvironment)38 BatchTableEnvironment (org.apache.flink.table.api.java.BatchTableEnvironment)38 Test (org.junit.Test)36 Table (org.apache.flink.table.api.Table)30 Row (org.apache.flink.types.Row)19 Tuple3 (org.apache.flink.api.java.tuple.Tuple3)12 ArrayList (java.util.ArrayList)6 TableConfig (org.apache.flink.table.api.TableConfig)4 HTable (org.apache.hadoop.hbase.client.HTable)4 Tuple5 (org.apache.flink.api.java.tuple.Tuple5)2 BatchTableSource (org.apache.flink.table.sources.BatchTableSource)2 HashMap (java.util.HashMap)1 Tuple4 (org.apache.flink.api.java.tuple.Tuple4)1 TupleTypeInfo (org.apache.flink.api.java.typeutils.TupleTypeInfo)1 CalciteConfig (org.apache.flink.table.calcite.CalciteConfig)1 CalciteConfigBuilder (org.apache.flink.table.calcite.CalciteConfigBuilder)1