How to sort an RDD in Scala Spark?

前端 未结 3 1299
太阳男子
太阳男子 2020-12-01 12:14

Reading Spark method sortByKey :

sortByKey([ascending], [numTasks])   When called on a dataset of (K, V) pairs where K implements Ordered, returns a dataset         


        
3条回答
  •  生来不讨喜
    2020-12-01 12:21

    Most likely you have already perused the source code:

      class OrderedRDDFunctions {
       // 
      def sortByKey(ascending: Boolean = true, numPartitions: Int = self.partitions.size): RDD[P] = {
        val part = new RangePartitioner(numPartitions, self, ascending)
        val shuffled = new ShuffledRDD[K, V, P](self, part)
        shuffled.mapPartitions(iter => {
          val buf = iter.toArray
          if (ascending) {
            buf.sortWith((x, y) => x._1 < y._1).iterator
          } else {
            buf.sortWith((x, y) => x._1 > y._1).iterator
          }
        }, preservesPartitioning = true)
      }
    

    And, as you say, the entire data must go through the shuffle stage - as seen in the snippet.

    However, your concern about subsequently invoking take(K) may not be so accurate. This operation does NOT cycle through all N items:

      /**
       * Take the first num elements of the RDD. It works by first scanning one partition, and use the
       * results from that partition to estimate the number of additional partitions needed to satisfy
       * the limit.
       */
      def take(num: Int): Array[T] = {
    

    So then, it would seem:

    O(myRdd.take(K)) << O(myRdd.sortByKey()) ~= O(myRdd.sortByKey.take(k)) (at least for small K) << O(myRdd.sortByKey().collect()

提交回复
热议问题