Spark項目練習(實現自定義排序)

轉載作者:Alen-Gao

轉載地址:https://blog.csdn.net/sonicgyq_gyq/article/details/79239946

  在實際操作中,sortBy算子往往不能滿足數據多種排序的需求,這就需要我們使用自定義排序來實現,以下是實現簡單的自定義排序的兩種方法,起到拋磚引玉的作用。

第一種方法:(Ordered:自定義一個函數)

[plain] view plain copy
  1. package cn.allengao.IpSearch  
  2. import org.apache.spark.{SparkConf, SparkContext}  
  3. /**  
  4.  * class_name:  
  5.  * package:  
  6.  * describe: 自定義排序(首先比較攻擊值,值高的排前面,如果攻擊值相等,比較防守值,攻擊值相同時,防守值高的排前面)  
  7.  * creat_user: Allen Gao  
  8.  * creat_date: 2018/2/2  
  9.  * creat_time: 11:01  
  10.  **/  
  11. object CustomSort {  
  12.   def main(args: Array[String]): Unit = {  
  13.     val conf = new SparkConf().setAppName("CustomSort").setMaster("local[*]")  
  14.     val sc = new SparkContext(conf)  
  15.     //球員信息,姓名name,攻擊值Attack,防守值defense。  
  16.     // (首先比較攻擊值,值高的排前面,如果攻擊值相等,比較防守值,攻擊值相同時,防守值高的排前面)  
  17.     val playerInfo = sc.parallelize(Array(("Kobe", 98, 95),("O'Neal" , 98, 96),("Harden",95 ,95)))  
  18.     val res = playerInfo.sortBy(x => Player(x._2, x._3),false)  
  19.     println(res.collect().toBuffer)  
  20.     //執行結果:ArrayBuffer((O'Neal,98,96), (Kobe,98,95), (Harden,95,95))  
  21.     sc.stop()  
  22.     }  
  23.   }  
  24. //第一種方式使用case class 模式匹配(Ordered 需要一個函數)  
  25. case class Player(val attack: Int, val defense: Int) extends Ordered[Player] with Serializable {  
  26.   override def compare(that: Player) :Int = {  
  27.     if (this.attack == that.attack) {  
  28.       this.defense - that.defense  
  29.     } else {  
  30.       this.attack - that.attack  
  31.     }  
  32.   }  
  33. }  

第二種方法:(Ordering:implicit隱式轉換值)

[plain] view plain copy
  1. package cn.allengao.IpSearch  
  2. import org.apache.spark.{SparkConf, SparkContext}  
  3. /**  
  4.   * class_name:  
  5.   * package:  
  6.   * describe: 自定義排序(首先比較攻擊值,值高的排前面,如果攻擊值相等,比較防守值,攻擊值相同時,防守值高的排前面)  
  7.   * creat_user: Allen Gao  
  8.   * creat_date: 2018/2/2  
  9.   * creat_time: 11:01  
  10.   **/  
  11. object MySort {  
  12.     implicit val playerOrdering = new Ordering[Player_1] {  
  13.  // implicit object PlayerOrdering extends Ordering[Player] {  
  14.     override def compare(x: Player_1, y: Player_1): Int = {  
  15.       if (x.attack > y.attack) 1  
  16.       else if (x.attack == y.attack) {  
  17.         if (x.defense > y.defense) 1 else -1  
  18.       } else -1  
  19.     }  
  20.   }  
  21. }  
  22. object CustomSort_1 {  
  23.   def main(args: Array[String]): Unit = {  
  24.     val conf = new SparkConf().setAppName("CustomSort").setMaster("local[*]")  
  25.     val sc = new SparkContext(conf)  
  26.     //球員信息,姓名name,攻擊值Attack,防守值defense。  
  27.     // (首先比較攻擊值,值高的排前面,如果攻擊值相等,比較防守值,攻擊值相同時,防守值高的排前面)  
  28.     val playerInfo = sc.parallelize(Array(("Kobe", 98, 95), ("O'Neal", 98, 96), ("Harden", 95, 95)))  
  29.     import MySort._  
  30.     val res = playerInfo.sortBy(x => Player_1(x._2, x._3), false)  
  31.     println(res.collect().toBuffer)  
  32.     //執行結果:ArrayBuffer((O'Neal,98,96), (Kobe,98,95), (Harden,95,95))  
  33.     sc.stop()  
  34.   }  
  35. }  
  36. //第二種方式:使用隱式轉換(Ordering需要一個隱式轉換值)  
  37. case class Player_1(attack: Int, defense: Int) extends Serializable  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章