gpt4 book ai didi

scala - Spark saveAsTextFile() 写入多个文件而不是一个

转载 作者:行者123 更新时间:2023-12-04 16:51:42 28 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





how to make saveAsTextFile NOT split output into multiple file?

(9 个回答)


5年前关闭。




目前我正在笔记本电脑上使用 Spark 和 Scala。

当我将 RDD 写入文件时,输出将写入两个文件“part-00000”和“part-00001”。如何强制 Spark/Scala 到 写入一个文件 ?

我的代码目前是:

myRDD.map(x => x._1 + "," + x._2).saveAsTextFile("/path/to/output")

我在哪里 removing the parentheses写出键值对。

最佳答案

“问题”确实是一个特征,它是由你的RDD 产生的。已分区,因此在 n 中分隔零件在哪里n是分区数。要解决此问题,您只需使用 repartition 将分区数更改为 1。在您的 RDD .该文件指出:

repartition(numPartitions)

Return a new RDD that has exactly numPartitions partitions.

Can increase or decrease the level of parallelism in this RDD. Internally, this uses a shuffle to redistribute data. If you are decreasing the number of partitions in this RDD, consider using coalesce, which can avoid performing a shuffle.



例如,此更改应该有效。
myRDD.map(x => x._1 + "," + x._2).repartition(1).saveAsTextFile("/path/to/output")

正如文档所说,您也可以使用 coalesce ,这实际上是减少分区数量时的推荐选项。然而,将分区数量减少到一个被认为是一个坏主意,因为它会导致数据混洗到一个节点并失去并行性。

关于scala - Spark saveAsTextFile() 写入多个文件而不是一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35445486/

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