Uncertain result when using group by in stream sql
Posted by liujiangang on Sep 13, 2019; 8:02am
URL: http://apache-flink.370.s1.nabble.com/Uncertain-result-when-using-group-by-in-stream-sql-tp698.html
I use flink stream sql to write a demo about "group by". The records
are [(bj, 1), (bj, 3), (bj, 5)]. I group by the first element and sum the
second element.
Every time I run the program, the result is different. It seems that
the records are out of order. Even sometimes record is lost. I am confused
about that.
The code is as below:
public class Test {
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env =
StreamExecutionEnvironment.getExecutionEnvironment();
StreamTableEnvironment tEnv =
StreamTableEnvironment.getTableEnvironment(env);
DataStream<Tuple2<String, Long>> dataStream = env.fromElements(
Tuple2.of("bj", 1L),
Tuple2.of("bj", 3L),
Tuple2.of("bj", 5L));
tEnv.registerDataStream("person", dataStream);
String sql = "select f0, sum(f1) from person group by f0";
Table table = tEnv.sqlQuery(sql);
tEnv.toRetractStream(table, Row.class).print();
env.execute();
}
}
The results may be as below:
1> (true,bj,1)
1> (false,bj,1)
1> (true,bj,4)
1> (false,bj,4)
1> (true,bj,9)
1> (true,bj,5)
1> (false,bj,5)
1> (true,bj,8)
1> (false,bj,8)
1> (true,bj,9)