我无法理解如何在并行运算符实例之间分配运算符状态以获得所需的结果 .
这是一个简单的例子:
class MultiplyNumber implements FlatMapFunction<Integer, Integer> {
// This is the operator state (this is statically defined for simplicity here in this example, but assume this can dynamic based on control stream)
List<Integer> multipliers = Arrays.asList(2,3,4,5);
void flatMap(Integer value, Collector<Integer> out){
for(Integer multiplier: multipliers){
out.collect(mutiplier*value);
}
}
}
假设我们运行parallelism = 2,这意味着我们有2个并行运算符(MultiplyNumber)实例(Operator1和Operator2)
如果运算符状态(即乘数列表)在这些实例之间分配,则说:Operator1有2,3作为乘数,而Operator2有4,5作为乘数 .
现在,假设我们有作为输入的键控整数流 . 所有偶数的关键是“偶数”,所有奇数的关键是“奇数” .
Flink将所有偶数发送给Operator1,所有奇数发送给Operator2(反之亦然) .
这意味着所有偶数应该乘以2和3,所有奇数应该乘以4和5 .
但这不是我期望的结果 . 我希望所有数字乘以2,3,4,5,这是并行度为1时的结果 .
1 回答
首先,你的例子中的乘数不是Flink 's state. It is a local to each operator java object instance. If you want to use Flink' s状态我建议通过文档中的this部分阅读 .
在你的情况下,我认为最有用的状态是flink 1.5中引入的
BroadcastState
(link),它是专门为你描述的用例实现的 .