Search in sources :

Example 1 with StreamTableEnvironment

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

the class HBaseConnectorITCase method testTableSourceSinkWithDDL.

@Test
public void testTableSourceSinkWithDDL() throws Exception {
    StreamExecutionEnvironment execEnv = StreamExecutionEnvironment.getExecutionEnvironment();
    StreamTableEnvironment tEnv = StreamTableEnvironment.create(execEnv, streamSettings);
    // register HBase table testTable1 which contains test data
    String table1DDL = createHBaseTableDDL(TEST_TABLE_1, true);
    tEnv.executeSql(table1DDL);
    // register HBase table which is empty
    String table3DDL = createHBaseTableDDL(TEST_TABLE_3, true);
    tEnv.executeSql(table3DDL);
    String insertStatement = "INSERT INTO " + TEST_TABLE_3 + " SELECT rowkey," + " family1," + " family2," + " family3," + " family4" + " from " + TEST_TABLE_1;
    TableResult tableResult = tEnv.executeSql(insertStatement);
    // wait to finish
    tableResult.await();
    assertEquals("Expected INSERT rowKind", RowKind.INSERT, tableResult.collect().next().getKind());
    // start a batch scan job to verify contents in HBase table
    TableEnvironment batchEnv = TableEnvironment.create(batchSettings);
    batchEnv.executeSql(table3DDL);
    List<String> expected = new ArrayList<>();
    expected.add("+I[1, 10, Hello-1, 100, 1.01, false, Welt-1, 2019-08-18T19:00, 2019-08-18, 19:00, 12345678.0001]");
    expected.add("+I[2, 20, Hello-2, 200, 2.02, true, Welt-2, 2019-08-18T19:01, 2019-08-18, 19:01, 12345678.0002]");
    expected.add("+I[3, 30, Hello-3, 300, 3.03, false, Welt-3, 2019-08-18T19:02, 2019-08-18, 19:02, 12345678.0003]");
    expected.add("+I[4, 40, null, 400, 4.04, true, Welt-4, 2019-08-18T19:03, 2019-08-18, 19:03, 12345678.0004]");
    expected.add("+I[5, 50, Hello-5, 500, 5.05, false, Welt-5, 2019-08-19T19:10, 2019-08-19, 19:10, 12345678.0005]");
    expected.add("+I[6, 60, Hello-6, 600, 6.06, true, Welt-6, 2019-08-19T19:20, 2019-08-19, 19:20, 12345678.0006]");
    expected.add("+I[7, 70, Hello-7, 700, 7.07, false, Welt-7, 2019-08-19T19:30, 2019-08-19, 19:30, 12345678.0007]");
    expected.add("+I[8, 80, null, 800, 8.08, true, Welt-8, 2019-08-19T19:40, 2019-08-19, 19:40, 12345678.0008]");
    Table countTable = batchEnv.sqlQuery("SELECT COUNT(h.rowkey) FROM " + TEST_TABLE_3 + " AS h");
    assertEquals(new Long(expected.size()), countTable.execute().collect().next().getField(0));
    String query = "SELECT " + "  h.rowkey, " + "  h.family1.col1, " + "  h.family2.col1, " + "  h.family2.col2, " + "  h.family3.col1, " + "  h.family3.col2, " + "  h.family3.col3, " + "  h.family4.col1, " + "  h.family4.col2, " + "  h.family4.col3, " + "  h.family4.col4 " + " FROM " + TEST_TABLE_3 + " AS h";
    TableResult tableResult3 = batchEnv.executeSql(query);
    List<String> result = Lists.newArrayList(tableResult3.collect()).stream().map(Row::toString).sorted().collect(Collectors.toList());
    assertEquals(expected, result);
}
Also used : TableResult(org.apache.flink.table.api.TableResult) Table(org.apache.flink.table.api.Table) ArrayList(java.util.ArrayList) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) StreamTableEnvironment(org.apache.flink.table.api.bridge.java.StreamTableEnvironment) TableEnvironment(org.apache.flink.table.api.TableEnvironment) StreamTableEnvironment(org.apache.flink.table.api.bridge.java.StreamTableEnvironment) Row(org.apache.flink.types.Row) Test(org.junit.Test)

Example 2 with StreamTableEnvironment

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

the class HBaseConnectorITCase method testHBaseLookupTableSource.

@Test
public void testHBaseLookupTableSource() {
    StreamExecutionEnvironment execEnv = StreamExecutionEnvironment.getExecutionEnvironment();
    StreamTableEnvironment tEnv = StreamTableEnvironment.create(execEnv, streamSettings);
    tEnv.executeSql("CREATE TABLE " + TEST_TABLE_1 + " (" + " family1 ROW<col1 INT>," + " family2 ROW<col1 STRING, col2 BIGINT>," + " family3 ROW<col1 DOUBLE, col2 BOOLEAN, col3 STRING>," + " rowkey INT," + " family4 ROW<col1 TIMESTAMP(3), col2 DATE, col3 TIME(3), col4 DECIMAL(12, 4)>," + " PRIMARY KEY (rowkey) NOT ENFORCED" + ") WITH (" + " 'connector' = 'hbase-1.4'," + " 'table-name' = '" + TEST_TABLE_1 + "'," + " 'zookeeper.quorum' = '" + getZookeeperQuorum() + "'" + ")");
    // prepare a source table
    String srcTableName = "src";
    DataStream<Row> srcDs = execEnv.fromCollection(testData).returns(testTypeInfo);
    Table in = tEnv.fromDataStream(srcDs, $("a"), $("b"), $("c"), $("proc").proctime());
    tEnv.registerTable(srcTableName, in);
    // perform a temporal table join query
    String dimJoinQuery = "SELECT" + " a," + " b," + " h.family1.col1," + " h.family2.col1," + " h.family2.col2," + " h.family3.col1," + " h.family3.col2," + " h.family3.col3," + " h.family4.col1," + " h.family4.col2," + " h.family4.col3," + " h.family4.col4 " + " FROM src JOIN " + TEST_TABLE_1 + " FOR SYSTEM_TIME AS OF src.proc as h ON src.a = h.rowkey";
    Iterator<Row> collected = tEnv.executeSql(dimJoinQuery).collect();
    List<String> result = CollectionUtil.iteratorToList(collected).stream().map(Row::toString).sorted().collect(Collectors.toList());
    List<String> expected = new ArrayList<>();
    expected.add("+I[1, 1, 10, Hello-1, 100, 1.01, false, Welt-1, 2019-08-18T19:00, 2019-08-18, 19:00, 12345678.0001]");
    expected.add("+I[2, 2, 20, Hello-2, 200, 2.02, true, Welt-2, 2019-08-18T19:01, 2019-08-18, 19:01, 12345678.0002]");
    expected.add("+I[3, 2, 30, Hello-3, 300, 3.03, false, Welt-3, 2019-08-18T19:02, 2019-08-18, 19:02, 12345678.0003]");
    expected.add("+I[3, 3, 30, Hello-3, 300, 3.03, false, Welt-3, 2019-08-18T19:02, 2019-08-18, 19:02, 12345678.0003]");
    assertEquals(expected, result);
}
Also used : Table(org.apache.flink.table.api.Table) ArrayList(java.util.ArrayList) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) StreamTableEnvironment(org.apache.flink.table.api.bridge.java.StreamTableEnvironment) Row(org.apache.flink.types.Row) Test(org.junit.Test)

Example 3 with StreamTableEnvironment

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

the class AvroTypesITCase method testAvroToAvro.

@Test
public void testAvroToAvro() {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
    DataStream<User> ds = testData(env);
    Table t = tEnv.fromDataStream(ds, selectFields(ds));
    Table result = t.select($("*"));
    List<User> results = CollectionUtil.iteratorToList(DataStreamUtils.collect(tEnv.toAppendStream(result, User.class)));
    List<User> expected = Arrays.asList(USER_1, USER_2, USER_3);
    assertEquals(expected, results);
}
Also used : User(org.apache.flink.formats.avro.generated.User) Table(org.apache.flink.table.api.Table) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) StreamTableEnvironment(org.apache.flink.table.api.bridge.java.StreamTableEnvironment) Test(org.junit.Test)

Example 4 with StreamTableEnvironment

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

the class AvroTypesITCase method testAvroStringAccess.

@Test
public void testAvroStringAccess() {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
    DataStream<User> ds = testData(env);
    Table t = tEnv.fromDataStream(ds, selectFields(ds));
    Table result = t.select($("name"));
    List<Utf8> results = CollectionUtil.iteratorToList(result.execute().collect()).stream().map(row -> (Utf8) row.getField(0)).collect(Collectors.toList());
    String expected = "Charlie\n" + "Terminator\n" + "Whatever";
    TestBaseUtils.compareResultAsText(results, expected);
}
Also used : PojoTypeInfo(org.apache.flink.api.java.typeutils.PojoTypeInfo) StreamTableEnvironment(org.apache.flink.table.api.bridge.java.StreamTableEnvironment) Arrays(java.util.Arrays) HashMap(java.util.HashMap) Expression(org.apache.flink.table.expressions.Expression) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) Expressions(org.apache.flink.table.api.Expressions) BigDecimal(java.math.BigDecimal) Colors(org.apache.flink.formats.avro.generated.Colors) LocalTime(java.time.LocalTime) AbstractTestBase(org.apache.flink.test.util.AbstractTestBase) Utf8(org.apache.avro.util.Utf8) Fixed16(org.apache.flink.formats.avro.generated.Fixed16) Expressions.$(org.apache.flink.table.api.Expressions.$) DataStreamUtils(org.apache.flink.streaming.api.datastream.DataStreamUtils) Test(org.junit.Test) Table(org.apache.flink.table.api.Table) CollectionUtil(org.apache.flink.util.CollectionUtil) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) DataStream(org.apache.flink.streaming.api.datastream.DataStream) List(java.util.List) Fixed2(org.apache.flink.formats.avro.generated.Fixed2) ChronoUnit(java.time.temporal.ChronoUnit) Address(org.apache.flink.formats.avro.generated.Address) LocalDate(java.time.LocalDate) User(org.apache.flink.formats.avro.generated.User) Row(org.apache.flink.types.Row) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) TestBaseUtils(org.apache.flink.test.util.TestBaseUtils) User(org.apache.flink.formats.avro.generated.User) Table(org.apache.flink.table.api.Table) Utf8(org.apache.avro.util.Utf8) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) StreamTableEnvironment(org.apache.flink.table.api.bridge.java.StreamTableEnvironment) Test(org.junit.Test)

Example 5 with StreamTableEnvironment

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

the class AvroTypesITCase method testAvroObjectAccess.

@Test
public void testAvroObjectAccess() {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
    DataStream<User> ds = testData(env);
    Table t = tEnv.fromDataStream(ds, selectFields(ds));
    Table result = t.filter($("type_nested").isNotNull()).select($("type_nested").flatten()).as("city", "num", "state", "street", "zip");
    List<Address> results = CollectionUtil.iteratorToList(DataStreamUtils.collect(tEnv.toAppendStream(result, Address.class)));
    String expected = USER_1.getTypeNested().toString();
    TestBaseUtils.compareResultAsText(results, expected);
}
Also used : User(org.apache.flink.formats.avro.generated.User) Table(org.apache.flink.table.api.Table) Address(org.apache.flink.formats.avro.generated.Address) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) StreamTableEnvironment(org.apache.flink.table.api.bridge.java.StreamTableEnvironment) Test(org.junit.Test)

Aggregations

StreamTableEnvironment (org.apache.flink.table.api.bridge.java.StreamTableEnvironment)64 Test (org.junit.Test)53 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)41 Row (org.apache.flink.types.Row)38 Table (org.apache.flink.table.api.Table)36 ArrayList (java.util.ArrayList)19 TableResult (org.apache.flink.table.api.TableResult)18 List (java.util.List)10 TableDescriptor (org.apache.flink.table.api.TableDescriptor)10 Arrays (java.util.Arrays)6 Collections (java.util.Collections)6 AbstractTestBase (org.apache.flink.test.util.AbstractTestBase)6 IOException (java.io.IOException)5 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)5 ResolvedSchema (org.apache.flink.table.catalog.ResolvedSchema)5 Either (org.apache.flink.types.Either)5 LocalDateTime (java.time.LocalDateTime)4 ZoneId (java.time.ZoneId)4 TypeHint (org.apache.flink.api.common.typeinfo.TypeHint)4 TypeInformation (org.apache.flink.api.common.typeinfo.TypeInformation)4