【问题标题】:Flink: how to understand how many instances of parallel operators exist?Flink:如何理解并行算子的实例有多少?
【发布时间】:2019-07-08 10:19:38
【问题描述】:

我有一个 KeyedStream 可以按键对事件流进行分片。每个键控流将发出事件,然后需要将这些事件与来自其他键控运算符的所有其他事件重新组合,以形成将存在于 Flink 状态的单个图。

然后需要处理/搜索图表,并可能在下游发出事件。我希望图形运算符能够水平扩展,即每个并行运算符处理图形的一个子集(但这将要求每个运算符都可以访问整个图形)。我对如何将负载分散到所有并行运算符感兴趣。

// key input events for processing by key
KeyedStream<MyEvent> keyedStream = myInputStream.keyBy(...);
// process each keyed input stream and produce output events that need to be combined into a graph
SingleOutputStreamOperator<MyGraphEvent> graphStream = keyedStream.process(...));
// recombine into a single graph operator via broadcast(), then process
DataStream<MyOutputEvent> output = graphStream.broadcast().flatMap(new MyGraphFlatmapFunction());

我想我可以使用broadcast() 来确保每个键控运算符的所有输出都发送到每个下游运算符。

MyGraphFlatmapFunction 获取MyGraphEvent 对象流,在内部状态中创建图形,并可选择生成MyOutputEvent 对象流。我希望每个并行运算符处理图形的一个子集。无论存在多少个运算符的并行实例,我都希望处理所有图形(这意味着我不希望每个运算符只处理图形的某个随机子集)并且我不想让并行运算符处理图形的相同部分(无重复处理)。

我希望能够在MyGraphFlatmapFunction 内做一些事情,例如:

int index;
// I want to get the operator instance number & the number of parallel operators in the stream topology
int operatorIndex = getOperatorIndex();
int operatorCount = getTotalNumberOfParallelOperators();
// process every nth object
for (index = 0; index < someCollection.size(); index++) {
    if (index % operatorCount == operatorIndex) {
        // do some processing
    } else {
        continue;
    }
}

有没有办法知道存在多少并行运算符实例以及这是哪个运算符?还有其他方法可以实现我的目标吗?

【问题讨论】:

    标签: java apache-flink flink-streaming


    【解决方案1】:

    如果您使用RichFlatMapFunction,您可以通过getRuntimeContext() 访问RuntimeContextRuntimeContext 有你需要的两种方法:

    • getNumberOfParallelSubtasks()
    • getIndexOfThisSubtask()

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2020-10-21
      • 1970-01-01
      • 2018-08-25
      • 1970-01-01
      • 2017-03-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-16
      • 1970-01-01
      相关资源
      最近更新 更多