Spark RDD读取数据的奇数分区

The odd number of partitions for reading data in Spark RDD

我正在处理 3GB 数据并且我已经使用 Spark RDD 读取数据:

rdd = sc.textFile("data.json")

我用rdd.getNumPartitions()的时候分区数是99!这真的很奇怪。 如果我什至使用sc.textFile("data.json", 20),又是99份!此外,我无法通过 rdd.repartition()rdd.coalesce() 更改分区数。分区数还是99。

我真的很困惑,我不知道为什么我的数据无缘无故分成99个分区!请指教

您可以阅读 textFile() 的工作原理 here。您感兴趣的部分是:

The textFile method also takes an optional second argument for controlling the number of partitions of the file. By default, Spark creates one partition for each block of the file (blocks being 128MB by default in HDFS), but you can also ask for a higher number of partitions by passing a larger value. Note that you cannot have fewer partitions than blocks.

您可以尝试设置分区数量,但该数量必须大于底层 HDFS 中为该文件存储的块数量。在你的情况下,仍然有一些东西没有加起来,因为 3*1024 MB 应该被分成 24 个块 -> 24 个分区而不是 getNumPartitions() 指示的 99。


只是为了给你一个完整的画面,HDFS 被设计(因此优化)来存储非常大的文件,这些文件被分成 64/128MB 的块。这些块根据复制策略分布在不同的Datanode中,以提高读取性能并确保高可用性。