简体   繁体   English

如何通过键合并两个PCollection KV <>?

[英]How to merge two PCollection KV<> by key?

Im trying to ouput a SUM and a COUNT for the same key. 我正在尝试为同一密钥输出SUM和COUNT。 Eg. 例如。 Given a .csv with millions of events of plane delays. 给定具有数百万个飞机延误事件的.csv。 Using Apache Beam (Java) I want to SUM the durations of the delays for each plane, and COUNT how many delays each plane had. 我想使用Apache Beam(Java)求和每个平面的延迟时间,并计算每个平面有多少延迟。

each row has plane_id, delay_duration, date , etc. 每行都有plane_id, delay_duration, date等。

Im trying to create two PCollections and want to kind of merge them before output. 我正在尝试创建两个PCollections,并希望在输出之前进行合并。

PCollection<KV<String, Integer>> sum =  eventInfo.apply(MapElements.into(TypeDescriptors.kvs(TypeDescriptors.strings(),TypeDescriptors.integers())).via((Event.EventInfo gInfo) -> KV.of(gInfo.getKey('plane_id'), gInfo.getDuration()))).apply(Sum.integersPerKey());

PCollection<KV<String, Long>> count =  eventInfo.apply(MapElements.into(TypeDescriptors.kvs(TypeDescriptors.strings(), TypeDescriptors.integers())).via((Event.EventInfo gInfo) -> KV.of(gInfo.getKey('plane_id'), gInfo.getDuration()))).apply(Count.perKey());

This two PCollections work as expected, but I can't figure it out how to output it (merge it?) in 3 columns key | 这两个PCollections可以按预期工作,但我无法弄清楚如何在3列中输出(合并?)它。 sum | 总和 count. 计数。

您将需要CoGBK ,这将帮助您共同定位总和并为同一密钥计数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM