如何在 Flink 的 keyBy() 之后获取 DataStream key Java API

How to get DataStream key after keyBy() in Flink Java API

我正在从 Flink 流应用程序中的 Kafka 集群读取数据。获得源流后,我想通过复合键和 timeEvent 翻滚 window 聚合事件,然后将结果写入 table。 问题是,在应用我的 aggregateFunction 后,它只计算 clientId 的点击次数,我找不到获取每个输出记录的键的方法,因为 api returns 一个累积结果的实例但不是相应的键。

    DataStream<Event> stream = environment.addSource(mySource)

    stream.keyBy(new KeySelector<Event,Integer>() {
    public Integer getKey(Event event) { return event.getClientId(); })
.window(TumblingEventTimeWindows.of(Time.minutes(1))).aggregate(new MyAggregateFunction)

如何获取我之前指定的密钥?我没有在累加器中注入输入事件的键,因为我觉得我不会很好。

而不是

.aggregate(new MyAggregateFunction)

你可以使用

.aggregate(new MyAggregateFunction, new MyProcessWindowFunction)

在这种情况下,您的 ProcessWindowFunction 的处理方法将传递密钥,以及您的 AggregateFunction 的 pre-aggregated 结果和具有其他可能相关信息的 Context 对象。有关更多详细信息,请参阅 ProcessWindowFunction with Incremental Aggregation 文档中的部分。