【问题标题】:Does repartition(1) maintains order all the time?repartition(1) 是否一直保持秩序?
【发布时间】:2017-10-19 17:41:27
【问题描述】:

我需要压缩两个可能有也可能没有相同分区的rdd,因此寻找重新分区的方法。我需要在压缩时保持顺序,而且我通常知道重新分区洗牌。但是下面的代码显示 repartiton(1) 没有改组 rdd。是只有这一次还是我们每次都能保证?

repartition(1) 是否类似于 .collect 因为它们都将 rdd 带到单个节点??

scala> var k = sc.parallelize((1 to 100),4)
k: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[0] at parallelize at <console>:27

scala> k.repartition(2)
res0: org.apache.spark.rdd.RDD[Int] = MapPartitionsRDD[4] at repartition at <console>:30

scala> res0.collect
res1: Array[Int] = Array(1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99)


scala> var l = sc.parallelize((1 to 100),4)
l: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[11] at parallelize at <console>:27

scala> l.repartition(1)
res5: org.apache.spark.rdd.RDD[Int] = MapPartitionsRDD[15] at repartition at <console>:30

scala> .collect
res6: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100)

【问题讨论】:

    标签: scala apache-spark rdd


    【解决方案1】:

    当您将repartition 设置为较低的值(1 是可能的最低分区数)时,您实际上是在执行coalesce 方法的工作。

    repartition 方法的文档字符串(和实现)将比我能给出的任何回复都清晰:

    /**
     * Return a new RDD that has exactly numPartitions partitions.
     *
     * Can increase or decrease the level of parallelism in this RDD. Internally, this uses
     * a shuffle to redistribute data.
     *
     * If you are decreasing the number of partitions in this RDD, consider using `coalesce`,
     * which can avoid performing a shuffle.
     */
    def repartition(numPartitions: Int)(implicit ord: Ordering[T] = null): RDD[T] = withScope {
      coalesce(numPartitions, shuffle = true)
    }
    

    但是,如果您打算zip,请考虑到压缩无论如何都会随机播放。如果您真的想要控制分区,您可以手动重新分区(也许使用自定义分区器,如果您有PairRDD),然后使用zipPartitions 指定您想要保留分区。

    然而,在大多数情况下,您可能只想坚持使用zip 默认实现,如下所示:

    /**
     * Zips this RDD with another one, returning key-value pairs with the first element in each RDD,
     * second element in each RDD, etc. Assumes that the two RDDs have the *same number of
     * partitions* and the *same number of elements in each partition* (e.g. one was made through
     * a map on the other).
     */
    def zip[U: ClassTag](other: RDD[U]): RDD[(T, U)] = withScope {
      zipPartitions(other, preservesPartitioning = false) { (thisIter, otherIter) =>
        new Iterator[(T, U)] {
          def hasNext: Boolean = (thisIter.hasNext, otherIter.hasNext) match {
            case (true, true) => true
            case (false, false) => false
            case _ => throw new SparkException("Can only zip RDDs with " +
              "same number of elements in each partition")
          }
          def next(): (T, U) = (thisIter.next(), otherIter.next())
        }
      }
    }
    

    如您所见,zip 已经完全符合您的要求。

    【讨论】:

    • 我关心的不仅仅是分区的顺序,还有我的 rdd 本身的顺序。假设我使用 repartition(1)(或 coalesce(1)),元素的顺序会保持不变吗?在我输入问题的代码sn-p中,repartition(1)的顺序是维护的,能保证一直维护吗?
    • 您是否认为 shuffling 我们的意思是 shuffling an array?在 Spark/Hadoop 术语中,shuffle 是指需要数据在网络中移动的计算。例如,通过 key 对项目应用归约将使具有相同 key 的所有项目移动到某个分区,从而 shuffle。在这种情况下,shuffle 不会破坏排序,它是同一个术语,但含义与通常应用于本地集合的术语不同。
    猜你喜欢
    • 2017-12-23
    • 2018-11-08
    • 2014-07-14
    • 1970-01-01
    • 2013-07-22
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 2018-04-17
    相关资源
    最近更新 更多