sortByKey()

sortByKey一定会有shuffer
作用:在一个(K,V)的RDD上调用,K必须实现Ordered接口,返回一个按照key进行排序的(K,V)的RDD

package com.atguigu

import org.apache.spark.rdd.RDD
import org.apache.spark.{HashPartitioner, Partitioner, SparkConf, SparkContext}

object Trans {
  def main(args: Array[String]): Unit = {

    val conf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("Spark01_Partition")
    //构建spark上下文对象
    val sc = new SparkContext(conf)

    val rdd: RDD[(Int, String)] = sc.makeRDD(Array((3,"aa"),(6,"bb"),(1,"cc"),(4,"dd")))
    val rdd1: RDD[(Int, String)] = rdd.sortByKey()
    rdd1.collect().foreach(println)

    sc.stop()
  }
}

(1,cc)
(3,aa)
(4,dd)
(6,bb)

你可能感兴趣的:(sortByKey())