【问题标题】:How does Round Robin partitioning in Spark work?Spark 中的循环分区如何工作?
【发布时间】:2019-01-10 07:37:01
【问题描述】:

我很难理解 Spark 中的循环分区。考虑以下示例。我将大小为 3 的 Seq 拆分为 3 个分区:

val df = Seq(0,1,2).toDF().repartition(3)

df.explain

== Physical Plan ==
Exchange RoundRobinPartitioning(3)
+- LocalTableScan [value#42]

现在,如果我检查分区,我会得到:

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|
+---------------+-----------------+

如果我对大小为 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|
|              5|                0|
|              6|                4|
|              7|                4|
+---------------+-----------------+

有人可以解释这种行为吗?据我了解循环分区,所有分区都显示为〜相同的大小。

【问题讨论】:

    标签: scala apache-spark partitioning


    【解决方案1】:

    (已检查 Spark 版本 2.1-2.4)

    据我从ShuffleExchangeExec code 看到的,Spark 尝试直接从原始分区(通过mapPartitions)对行进行分区,而不会给驱动程序带来任何东西。

    逻辑是从随机选择的目标分区开始,然后以循环方法将分区分配给行。请注意,每个源分区都会选择“开始”分区,并且可能会发生冲突。

    最终分布取决于许多因素:源/目标分区的数量以及数据框中的行数。

    【讨论】:

      【解决方案2】:

      我无法解释原因,但不知何故它链接到本地​​主机。

      如果你明确设置:

      • --master local => 1 每个分区的行(无并行性)

      • --master "local[2]" => 2 每个分区的行数(4 个分区为空)

      • --master "local[4]" => 4 每个分区的行数(6 个分区为空)

      • --master "local[8]" => 8 每个分区的行数(7 个分区为空)

      【讨论】:

      • 有趣! local[N] 中N 的数量决定了初始分区的数量,例如当N=4,然后(0 to 7).toDF().rdd.getNumPartitions = 4。所以轮询分区使 4 个分区中的 8 个分区,一定有魔法
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-11
      • 1970-01-01
      • 1970-01-01
      • 2019-01-22
      • 1970-01-01
      相关资源
      最近更新 更多