Spark Dataframe 的分区数 [英] Number of Partitions of Spark Dataframe
问题描述
谁能解释一下将为 Spark Dataframe 创建的分区数量.
Can anyone explain about the number of partitions that will be created for a Spark Dataframe.
我知道对于 RDD,在创建它时,我们可以提及分区的数量,如下所示.
I know that for a RDD, while creating it we can mention the number of partitions like below.
val RDD1 = sc.textFile("path" , 6)
但是对于创建时的 Spark 数据帧,我们似乎无法像 RDD 那样指定分区数.
But for Spark dataframe while creating looks like we do not have option to specify number of partitions like for RDD.
我认为唯一的可能性是,在创建数据帧后,我们可以使用重新分区 API.
Only possibility i think is, after creating dataframe we can use repartition API.
df.repartition(4)
那么任何人都可以告诉我是否可以在创建数据帧时指定分区数.
So can anyone please let me know if we can specify the number of partitions while creating a dataframe.
推荐答案
你不能,或者至少在一般情况下不能,但与 RDD 相比并没有什么不同.例如,您提供的 textFile
示例代码仅设置了最小分区数的限制.
You cannot, or at least not in a general case but it is not that different compared to RDD. For example textFile
example code you've provides sets only a limit on the minimum number of partitions.
一般来说:
- 在本地集合上使用
Datasets
将使用spark.default.parallelism
. 从 Datasets
从其父级继承分区数.Datsets
使用数据源 API 创建:
range
或 toDF
等方法在本地生成的 RDD
创建的 Datasets
generated locally using methods likerange
ortoDF
on local collection will usespark.default.parallelism
.Datasets
created fromRDD
inherit number of partitions from its parent.Datsets
created using data source API:
- 在 Spark 1.x 中,通常取决于 Hadoop 配置(最小/最大拆分大小).
- 在 Spark 2.x 使用 Spark SQL 特定配置.
这篇关于Spark Dataframe 的分区数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!