gpt4 book ai didi

apache-spark - 是否可以合并 Spark 分区 "evenly"?

转载 作者:行者123 更新时间:2023-12-05 05:16:07 24 4
gpt4 key购买 nike

假设我们有一个 PySpark 数据帧,数据均匀分布在 2048 个分区中,我们想要合并到 32 个分区以将数据写回 HDFS。使用 coalesce 对此非常有用,因为它不需要昂贵的洗牌。

但是 coalesce 的缺点之一是它通常会导致新分区中的数据分布不均匀。我假设这是因为原始分区 ID 被散列到新的分区 ID 空间,并且碰撞次数是随机的。

但是,原则上应该可以均匀合并,以便将原始数据帧的前 64 个分区发送到新数据帧的第一个分区,接下来的 64 个分区发送到第二个分区,如此结束,导致分区均匀分布。生成的数据框通常更适合进一步计算。

这是否可能,同时防止洗牌?

我可以使用类似 this question 中的技巧在初始分区和最终分区之间强制建立我想要的关系,但 Spark 不知道每个原始分区中的所有内容都将转到特定的新分区。因此它无法优化洗牌,并且它的运行速度比 coalesce 慢得多。

最佳答案

在您的情况下,您可以安全地将 2048 个分区合并为 32 个,并假设 Spark 会将上游分区平均分配给合并的分区(在您的情况下每个分区 64 个)。

这里是 an extract from the Scaladoc of RDD#coalesce :

This results in a narrow dependency, e.g. if you go from 1000 partitions to 100 partitions, there will not be a shuffle, instead each of the 100 new partitions will claim 10 of the current partitions.

请考虑您的分区在集群中的物理分布方式也会影响合并发生的方式。以下摘自 CoalescedRDD's ScalaDoc :

If there is no locality information (no preferredLocations) in the parent, then the coalescing is very simple: chunk parents that are close in the Array in chunks. If there is locality information, it proceeds to pack them with the following four goals:

(1) Balance the groups so they roughly have the same number of parent partitions

(2) Achieve locality per partition, i.e. find one machine which most parent partitions prefer

(3) Be efficient, i.e. O(n) algorithm for n parent partitions (problem is likely NP-hard)

(4) Balance preferred machines, i.e. avoid as much as possible picking the same preferred machine

关于apache-spark - 是否可以合并 Spark 分区 "evenly"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50853831/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com