为什么在 Scio 中你更喜欢聚合而不是 groupByKey?

And*_*idy 1 scala dataflow apache-beam spotify-scio

从:

https://github.com/spotify/scio/wiki/Scio-data-guideline

“比 groupByKey 更喜欢组合/聚合/减少转换。请记住,减少操作必须是关联的和可交换的。”

为什么特别喜欢聚合而不是 groupByKey?

And*_*nly 5

组合、聚合和减少转换比 groupByKey 更受欢迎,因为前者在管道执行期间内存效率更高。这是由于在 Apache Beam 中实现了原语GroupByKeyCombine转换。这个问题的答案不一定特定于 Scio。

GroupByKey要求所有键值对都保留在内存中,这可能会导致OutOfMemoryErrors。每个窗口的所有键值对都保留在内存中。groupByKey使用 Beam 的原始GroupByKey变换。

聚合消除了将所有值保存在内存中的需要,因为在执行转换期间值会不断组合/减少。值以不确定的顺序组合/减少,这就是所有组合/减少操作必须关联的原因。Scio 的实现aggregateByKey使用 Beam 的原语Combine变换。

参考资料:
1. Scio groupByKey
2. Scio aggregateByKey
3. Apache Beam GroupByKey
4. Apache Beam Combine
5. Google Cloud Dataflow Combine