码迷,mamicode.com
首页 > 编程语言 > 详细

sspark自定义排序

时间:2017-03-19 13:18:55      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:val   style   flow   creat   print   ble   err   park   ring   

/**
  * Created by willian on 2017/3/19.
  * 自定义排序,例如 年龄相同 再比较颜值
  */
object CustomSort {
  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf().setAppName("flow_analysis").setMaster("local")
    val sc = new SparkContext(conf)
    val person_rdd: RDD[(String, Int, Int)] = sc.parallelize(List(("zhangweilun",20,18),("lixueping",20,19)))
    val sorted_rdd: RDD[(String, Int, Int)] = person_rdd.sortBy(item =>{
      Person(item._3,item._2,item._1)
    },ascending = false)
    println(sorted_rdd.collect().toBuffer)
  }
}

//注意:必须实现Serializable接口,并且集成orderd,重写比较方法
case class Person(var look:Int,var age:Int,var name:String) extends Ordered[Person] with Serializable{
  override def compare(that: Person): Int = {
    if (this.look == that.look){
      that.age - that.age
    }else{
      this.look - that.look
    }
  }
}

如上,加入存储数据的类,并重写比较方法即可

sspark自定义排序

标签:val   style   flow   creat   print   ble   err   park   ring   

原文地址:http://www.cnblogs.com/zhangweilun/p/6579723.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!