Apache Beam - 如何按键对所有窗口的PCollection<KV<String, Int>>求和
创始人
2024-11-10 00:00:34
0

以下是使用Apache Beam按键对所有窗口的PCollection >进行求和的示例代码:

import org.apache.beam.sdk.Pipeline;
import org.apache.beam.sdk.transforms.*;
import org.apache.beam.sdk.values.KV;
import org.apache.beam.sdk.values.PCollection;
import org.apache.beam.sdk.transforms.windowing.*;
import org.apache.beam.sdk.options.PipelineOptionsFactory;
import org.joda.time.Duration;

public class SumByKeyInAllWindows {

  public static void main(String[] args) {
    // 创建Pipeline
    Pipeline pipeline = Pipeline.create(PipelineOptionsFactory.fromArgs(args).create());

    // 创建输入数据集
    PCollection> input = pipeline
        .apply("Generate input", Create.of(
            KV.of("key1", 1),
            KV.of("key2", 2),
            KV.of("key1", 3),
            KV.of("key2", 4),
            KV.of("key2", 5)
        ));

    // 应用窗口
    PCollection> sumPerKey = input
        .apply("Apply fixed windows", Window.into(FixedWindows.of(Duration.standardMinutes(1))))
        .apply("Sum per key", Sum.perKey());

    // 输出结果
    sumPerKey.apply("Print results", ParDo.of(new DoFn, Void>() {
      @ProcessElement
      public void processElement(ProcessContext c) {
        System.out.println(c.element());
      }
    }));

    // 运行Pipeline
    pipeline.run().waitUntilFinish();
  }
}

在此示例中,我们首先创建一个输入PCollection >,其中键是String类型,值是整数类型。

然后,我们应用了一个Fixed Windows,将输入数据按1分钟的时间窗口进行分组。

接下来,我们使用Sum.perKey()转换对每个键的值进行求和。

最后,我们将结果打印出来,通过ParDo转换应用一个DoFn来处理每个元素。

最后,我们运行Pipeline并等待其完成。

相关内容

热门资讯

外挂经验!哈糖大菠萝破解器,w... 外挂经验!哈糖大菠萝破解器,werplan免费挂下载-确实真的是有辅助神器(哔哩哔哩)进入游戏-大厅...
外挂演示!werplan辅助软... 外挂演示!werplan辅助软件,pokemomo辅助工具-果然一直都是有辅助工具(哔哩哔哩)1、外...
外挂项目!hardrock透视... 外挂项目!hardrock透视工具,agpoker辅助-一直一直都是有辅助app(哔哩哔哩)1、让任...
外挂阶段!fishpoker透... 外挂阶段!fishpoker透视底牌,pokerworld破解版下载-其实有辅助神器(哔哩哔哩)po...
外挂窍门!聚星ai辅助工具激活... 外挂窍门!聚星ai辅助工具激活码,佛手在线大菠萝智能辅助器-好像是真的有辅助插件(哔哩哔哩)1、全新...
外挂演示!德扑圈透视,德普之星... 外挂演示!德扑圈透视,德普之星透视辅助软件激活码-都是真的有辅助神器(哔哩哔哩)该软件可以轻松地帮助...
外挂学习!扑克之星辅助,德州透... 外挂学习!扑克之星辅助,德州透视插件-其实一直都是有辅助软件(哔哩哔哩)1、不需要AI权限,帮助你快...
外挂秘籍!智星菠萝透视,约局吧... 外挂秘籍!智星菠萝透视,约局吧德州真的存在透视吗-都是有辅助方法(哔哩哔哩)1、上手简单,内置详细流...
外挂讲义!uupoker透视,... 外挂讲义!uupoker透视,pokermaster辅助器-确实有辅助脚本(哔哩哔哩)1、玩家可以在...
外挂练习!德普之星私人局辅助器... 外挂练习!德普之星私人局辅助器,sohoo poker辅助-原来是真的有辅助软件(哔哩哔哩)所有人都...