开发者社区> 问答> 正文

Apache Flink:keyby和window运算符

"我想知道一些与keyedstream相关的机制。代码如下:

DataStream> counts =

        // split up the lines in pairs (2-tuples) containing: (word,1)
        text.flatMap(new Tokenizer())
        // group by the tuple field ""0"" and sum up tuple field ""1""
                .keyBy(0)
                .window(TumblingProcessingTimeWindows.of(Time.seconds(3)))

如果我想实现窗口wordcount。

Q1:每个窗口中只有一个键还是多个键?

Q2:对于窗口中的函数,我只使用简单的sum ++或者需要通过Apache Storm中的hashmap处理多个键的总和。"

展开
收起
flink小助手 2018-11-28 16:22:04 4746 0
1 条回答
写回答
取消 提交回答
  • flink小助手会定期更新直播回顾等资料和文章干货,还整合了大家在钉群提出的有关flink的问题及回答。

    "即使每个窗口实际上有多个键,每个对process/ reduce/ sum/ aggregate函数的调用都是使用具有相同键的元素进行的。

    在你的示例中,你可以使用sum,Flink将负责所有事情:

    text.flatMap(new Tokenizer())

      .keyBy(0)
      .window(TumblingProcessingTimeWindows.of(Time.seconds(3)))
      .sum(X)

    如果你选择reduce改为......

    text.flatMap(new Tokenizer())

      .keyBy(0)
      .window(TumblingProcessingTimeWindows.of(Time.seconds(3)))
      .reduce(new ReduceFunction<Tuple2<String, Integer>>(){
            @Override
            public Tuple2<String, Integer> reduce(final Tuple2<String, Integer> first, final Tuple2<String, Integer> second) {
                  (... do something with the guarantee that first[0] == second[0](same key) ...)
            }
      });"
    2019-07-17 23:16:51
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Flink CDC Meetup PPT - 龚中强 立即下载
Flink CDC Meetup PPT - 王赫 立即下载
Flink CDC Meetup PPT - 覃立辉 立即下载

相关镜像