Flink:及时合并所有键的结果 windows

Flink: merging results over all keys in time windows

我有一个来自 Kafka 的数据流:

stream
    .keyBy("key")
    .window(<tumbling window of 5 mins>)
    .aggregate(<aggFunc>, <function adding window key and start wd time>)
    ...
    .addSink(sink)

1window中的键数是动态的(window1有3个键,window2可以有4个键)。

Window 1 输出:5 分钟 - 例如 00:00 到 00:05。编号仅供后面参考,每条记录都是一个POJO,这里我只保留重要的字段。

1. (<aggregated val>, key1, <window1-start-time)
2. (<aggregated val>, key2, <window1-start-time)
3. (<aggregated val>, key3, <window1-start-time)

Window 2 输出:00:05 到 00:05-00:10

4. (<aggregated val>, key1, <window1-start-time)
5. (<aggregated val>, key3, <window2-start-time)
6. (<aggregated val>, key4, <window2-start-time)
7. (<aggregated val>, key5, <window2-start-time)

我想要的:字符串(json对象列表)

[json-string(1), json-string(2), json-string(3)] 
-> send to sink after window 1 fires
[json-string(4), json-string(5), json-string(6), json-string(7)] 
-> send to sink after window 2 fires

我的接收器是接受 json 负载的 HTTP 请求的端点。我想将我所有的密钥批量放入 window 中,并为每个 window 发送一次到接收器。 怎么做?

我目前使用 CountWindowAll 但这不是最佳选择,因为 countWindow 可能包含来自 2 windows 的数据。我正在考虑另一个 TublingWindow after apply 但不知道 Flink 如何在 apply.

之后派生时间戳

我正在使用从 Kafka 记录中的字段派生时间戳的 EventTime。在 apply 运算符之后,事件不再有该时间戳记录,而是一个 Long windowStartTime.

您可以在键控 TimeWindow 之后使用非键控 TimeWindowAll,它将第一个 window:

的所有结果汇总在一起
stream
    .keyBy("key")
    .window(<tumbling window of 5 mins>)
    .aggregate(<aggFunc>, <function adding window key and start wd time>)
    .windowAll(<tumbling window of 5 mins>)
    .process(<function iterating over batch of keys for each window>)
    .addSink(sink)

时间window 发出的记录会以允许应用另一层windowing 的方式自动加盖时间戳。这可用于聚合所有键的结果(如此处所示),或在不同时间范围内生成键控结果(例如,将 5 分钟 windows 合并为 60 分钟 windows)。

Flink 训练包含一个 exercise 来说明此模式。