第五章_Spark核心编程_Rdd_转换算子_Value型_sortBy

1. 定义

    /*
    * 1. 定义
    * def sortBy[K](
    *          f: (T) => K,
    *          ascending: Boolean = true,
    *          numPartitions: Int = this.partitions.length)
    *          (implicit ord: Ordering[K], ctag: ClassTag[K]): RDD[T]
    *    f: (T) => K : 指定要排序的key(按函数结果排序)
    *    ascending : true-升序 false-降序
    *    numPartitions : 指定排序后的分区数量
    *
    * 2. 功能
    *    对 Rdd元素按照指定规则 全局排序
    *
    * 3. note
    *    先全局排序,再对结果分区(中间存在Shuffle过程)
    * */
  object sortByTest extends App {

    val sparkconf: SparkConf = new SparkConf().setMaster("local").setAppName("distinctTest")

    val sc: SparkContext = new SparkContext(sparkconf)

    val rdd = sc.makeRDD(List(1, 2, 3, 4, 5, 6, 7, 8, 8, 2), 2)

    private val rdd1: RDD[Int] = rdd.sortBy(e => e,false,1)

    rdd1.saveAsTextFile("Spark_319/src/output/01")

    println(s"当前分区数 : ${rdd1.getNumPartitions}")

    println(rdd1.collect().mkString(","))

    sc.stop()
  }

 

posted @ 2022-03-23 16:11  学而不思则罔!  阅读(58)  评论(0)    收藏  举报