Converting Tables to DataStreams

Learn how to convert Tables into DataStreams.

A Table, as it is the result of a streaming query, is updated dynamically. The Table changes as new records arrive on the query’s input streams. These Tables can be converted back into DataStreams by capturing the change of the query output.

There are two modes to convert a Table into a DataStream:
  • Append Mode: This mode can only be used if the dynamic Table is only modified by INSERT changes. For example, it is append-only and previously emitted results are never updated.
  • Retract Mode: This mode can always be used. It encodes INSERT and DELETE changes with a boolean flag. True marks inserts, and false marks deletes.
Both toAppendStream and toRetractStream methods take the conversion class or conversion type information as parameters. For the recommended Row conversions, you need to provide the Row.class. For Tuple conversions, you need to provide the Tuple TypeInformation object manually.
Table table = tableEnv.sqlQuery("SELECT name, age FROM People");

DataStream<Row> appendStream = tableEnv.toAppendStream(table, Row.class);
DataStream<Tuple2<Boolean, Row>> retractStream = tableEnv.toRetractStream(table, Row.class);

DataStream<Tuple2<String, Integer>> tupleStream = tableEnv.toAppendStream(
  table, 
  new TypeHint<Tuple2<String, Integer>>() {}.getTypeInfo()
);