gpt4 book ai didi

python - 如何跨分区平衡我的数据?

转载 作者:太空狗 更新时间:2023-10-29 18:08:07 26 4
gpt4 key购买 nike

编辑:答案有帮助,但我在以下位置描述了我的解决方案:memoryOverhead issue in Spark .


我有一个包含 202092 个分区的 RDD,它读取其他人创建的数据集。我可以手动看到分区之间的数据不平衡,例如其中一些有 0 个图像而其他有 4k,而平均值为 432。处理数据时,我收到此错误:

Container killed by YARN for exceeding memory limits. 16.9 GB of 16 GB physical memory used. Consider boosting spark.yarn.executor.memoryOverhead.

虽然 memoryOverhead 已经提升。我觉得一些尖峰正在发生,这让 Yarn 杀死了我的容器,因为那个尖峰溢出了指定的边界。

那么我应该怎么做才能确保我的数据(大致)跨分区平衡?/p>


我的想法是 repartition()会工作,它会调用洗牌:

dataset = dataset.repartition(202092)

但我只是得到了同样的错误,尽管 programming-guide的说明:

repartition(numPartitions)

Reshuffle the data in the RDD randomly to create either more or fewer partitions and balance it across them. This always shuffles all data over the network.


检查我的玩具示例:

data = sc.parallelize([0,1,2], 3).mapPartitions(lambda x: range((x.next() + 1) * 1000))
d = data.glom().collect()
len(d[0]) # 1000
len(d[1]) # 2000
len(d[2]) # 3000
repartitioned_data = data.repartition(3)
re_d = repartitioned_data.glom().collect()
len(re_d[0]) # 1854
len(re_d[1]) # 1754
len(re_d[2]) # 2392
repartitioned_data = data.repartition(6)
re_d = repartitioned_data.glom().collect()
len(re_d[0]) # 422
len(re_d[1]) # 845
len(re_d[2]) # 1643
len(re_d[3]) # 1332
len(re_d[4]) # 1547
len(re_d[5]) # 211
repartitioned_data = data.repartition(12)
re_d = repartitioned_data.glom().collect()
len(re_d[0]) # 132
len(re_d[1]) # 265
len(re_d[2]) # 530
len(re_d[3]) # 1060
len(re_d[4]) # 1025
len(re_d[5]) # 145
len(re_d[6]) # 290
len(re_d[7]) # 580
len(re_d[8]) # 1113
len(re_d[9]) # 272
len(re_d[10]) # 522
len(re_d[11]) # 66

最佳答案

我认为内存开销超出限制的问题是由于在获取期间使用了 DirectMemory 缓冲区。我认为它在 2.0.0 中已修复。 (我们遇到了同样的问题,但是当我们发现升级到 2.0.0 可以解决问题时,我们停止了更深入的挖掘。不幸的是,我没有 Spark 问题编号来支持我。)


repartition 后的分区不均匀令人惊讶。对比 https://github.com/apache/spark/blob/v2.0.0/core/src/main/scala/org/apache/spark/rdd/RDD.scala#L443 . Spark 甚至在 repartition 中生成随机键,因此它不会使用可能有偏差的哈希来完成。

我尝试了您的示例并得到了与 Spark 1.6.2 和 Spark 2.0.0 完全相同的结果。但不是来自 Scala spark-shell:

scala> val data = sc.parallelize(1 to 3, 3).mapPartitions { it => (1 to it.next * 1000).iterator }
data: org.apache.spark.rdd.RDD[Int] = MapPartitionsRDD[6] at mapPartitions at <console>:24

scala> data.mapPartitions { it => Iterator(it.toSeq.size) }.collect.toSeq
res1: Seq[Int] = WrappedArray(1000, 2000, 3000)

scala> data.repartition(3).mapPartitions { it => Iterator(it.toSeq.size) }.collect.toSeq
res2: Seq[Int] = WrappedArray(1999, 2001, 2000)

scala> data.repartition(6).mapPartitions { it => Iterator(it.toSeq.size) }.collect.toSeq
res3: Seq[Int] = WrappedArray(999, 1000, 1000, 1000, 1001, 1000)

scala> data.repartition(12).mapPartitions { it => Iterator(it.toSeq.size) }.collect.toSeq
res4: Seq[Int] = WrappedArray(500, 501, 501, 501, 501, 500, 499, 499, 499, 499, 500, 500)

好漂亮的分区!


(抱歉,这不是一个完整的答案。我只是想分享我到目前为止的发现。)

关于python - 如何跨分区平衡我的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38799753/

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