【问题标题】:Data distribution while repartitioning RDD in Spark在 Spark 中重新分区 RDD 时的数据分布
【发布时间】:2017-04-21 10:19:32
【问题描述】:

考虑以下 sn-p(在 Python 2.7 上运行 Spark 2.1):

nums = range(0, 10)

with SparkContext("local[2]") as sc:
    rdd = sc.parallelize(nums)
    print("Number of partitions: {}".format(rdd.getNumPartitions()))
    print("Partitions structure: {}".format(rdd.glom().collect()))

    rdd2 = rdd.repartition(5)
    print("Number of partitions: {}".format(rdd2.getNumPartitions()))
    print("Partitions structure: {}".format(rdd2.glom().collect()))

输出是:

Number of partitions: 2
Partitions structure: [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]

Number of partitions: 5
Partitions structure: [[], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [], [], []]

为什么重新分区后数据没有分布在所有分区上?

【问题讨论】:

    标签: python apache-spark pyspark


    【解决方案1】:

    repartition 在 pyspark 中是 coalesce(numPartitions, shuffle=True) (see core code here)。即数据在整个网络中被打乱,分区以循环方式完成,第一条记录进入第一个处理节点,第二条记录进入第二个处理节点,但在你的情况下,因为你只分配了local[2],即两个(假设的)节点,但我的猜测是spark只能从你的本地机器获得一个核心,所以它将所有值放在那个特定的节点中任务运行的地方。

    【讨论】:

    • 感谢您的评论。我不认为会是这样。此方法在使用 DataFrames 时有效(请参阅hackernoon.com/…)但在纯 RDD 上失败
    猜你喜欢
    • 2020-09-18
    • 2021-01-03
    • 1970-01-01
    • 2016-01-04
    • 2015-09-02
    • 1970-01-01
    • 2016-08-01
    • 2015-06-18
    • 2017-02-17
    相关资源
    最近更新 更多