gpt4 book ai didi

apache-flink - 在 Flink 中,在 Keyed Stream 上应用 Process Function 时如何访问 Key?

转载 作者:行者123 更新时间:2023-12-05 05:18:39 24 4
gpt4 key购买 nike

在 Flink 中,我有一个键控流,我正在向其应用处理函数。

myDataStream
.keyBy(new MyKeySelector())
.process(new FooBarProcessFunction())

我的 Key Selector 看起来像这样......

public class MyKeySelector implements KeySelector<FooBar, FooKey>

public FooKey getKey (FooBar value) {
return new FooKey (value);
}

FooBarProcessFunction 看起来像这样......

public class FooBarProcessFunction extends ProcessFunction<FooBar, TransformedFooBar> {

public void processElement(FooBar newFooBar, Context ctx, Collector<FooBar> out) {
//do something with newFooBar
// *****but I also want to know the Key (FooKey) here*****
}
}

在 FooBarProcessFunction 中,我想获取由 MyKeySelector 的 getKey 方法创建的 Key。这可行吗?

目前,我正在使用一种变通方法,我基本上是在 processElement 函数中重新创建 key 。但如果我能避免这样做,那就太理想了。

最佳答案

为了从进程函数访问 key ,您应该使用KeyedProcessFunction

你的例子变成:

public class FooBarProcessFunction extends KeyedProcessFunction<FooKey, FooBar, TransformedFooBar> {

public void processElement(FooBar newFooBar, Context ctx, Collector<FooBar> out) {
//do something with newFooBar
// *****but I also want to know the Key (FooKey) here*****
ctx.getCurrentKey
}
}

关于apache-flink - 在 Flink 中,在 Keyed Stream 上应用 Process Function 时如何访问 Key?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47480586/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com