带字段表达式的apache flink keyby函数

vqlkdk9b  于 2021-06-24  发布在  Flink
关注(0)|答案(1)|浏览(713)

示例消息:

>{"sensor":"temp1", "value":12.0, "timestamp":19200230}
>{"sensor":"temp1", "value":12.0, "timestamp":19200230}
>{"sensor":"temp1", "value":12.0, "timestamp":19200230}
>{"sensor":"temp2", "value":5, "timestamp":19200230}
>{"sensor":"temp2", "value":5, "timestamp":19200230}

我正在尝试使用keyby方法构建流聚合。

DataStream<Message> messageSumStream = messageStream.keyBy("sensor").timeWindowAll(Time.minutes(5)).sum("value");

我预料到了

{"sensor": "temp1", "value": 36.000000, "timestamp":19200230 }
{"sensor": "temp2", "value": 10.000000, "timestamp":19200230 }

但是得到:

{"sensor": "temp1", "value": 46.000000, "timestamp":19200230 }

我错过了什么?

jckbn6z7

jckbn6z71#

您使用的是datastream类中的timewindowall,而不是keyeddatastream中的timewindow,这会导致忽略keyby的代码。
试试这个:

DataStream<Message> messageSumStream = messageStream.keyBy("sensor").timeWindow(Time.minutes(5)).sum("value");

相关问题