我想查看如何获取有关每个分区的信息,例如总数.当使用部署模式作为纱线群集提交Spark作业以便在控制台上记录或打印时,驱动程序端的每个分区中的记录数.
我很难理解 Spark 中的循环分区。考虑以下示例。我将大小为 3 的 Seq 拆分为 3 个分区:
val df = Seq(0,1,2).toDF().repartition(3)
df.explain
== Physical Plan ==
Exchange RoundRobinPartitioning(3)
+- LocalTableScan [value#42]
Run Code Online (Sandbox Code Playgroud)
现在,如果我检查分区,我会得到:
df
.rdd
.mapPartitionsWithIndex{case (i,rows) => Iterator((i,rows.size))}
.toDF("partition_index","number_of_records")
.show
+---------------+-----------------+
|partition_index|number_of_records|
+---------------+-----------------+
| 0| 0|
| 1| 2|
| 2| 1|
+---------------+-----------------+
Run Code Online (Sandbox Code Playgroud)
如果我对大小为 8 的 Seq 执行相同操作并将其拆分为 8 个分区,则会出现更严重的偏差:
(0 to 7).toDF().repartition(8)
.rdd
.mapPartitionsWithIndex{case (i,rows) => Iterator((i,rows.size))}
.toDF("partition_index","number_of_records")
.show
+---------------+-----------------+
|partition_index|number_of_records|
+---------------+-----------------+
| 0| 0|
| 1| 0|
| 2| 0|
| 3| 0|
| 4| 0| …Run Code Online (Sandbox Code Playgroud)