首页 > 编程语言 > 详细

spark中自定义多维度排序

时间:2016-07-13 19:56:37      阅读:204      评论:0      收藏:0      [点我收藏+]

在spark中,原始的sortByKey是以map为操作对象,按照key进行排序,value跟随

如果我们要设置多维排序,就需要自定义这个key对象

该class需要 extends Ordered[T] with Serializable , 然后将这个类的对象作为sortByKey的第一个key参数,进行sort

 

val conf = new SparkConf()
conf.setAppName("thirdSort")
conf.setMaster("local")
val sc = new SparkContext(conf)

val lines = sc.textFile("d:/third.txt")

val a = lines.map(line => (new ThirdOrderKey(line.split(" ")(0).toInt,line.split(" ")(1).toInt,line.split(" ")(2).toInt),line))     // 将每行的key分别装入到ThirdOrderKey对象

a.sortByKey(false).map(x=>x._2).collect.foreach (println)                           //  sortByKey的结果会自动添加一行key,结果是value

  

下面是ThirdOrderKey的定义,2个关键点,一个是extends Ordered,另外一个是实现compare方法

class  ThirdOrderKey(val first:Int,val second:Int,val thrid:Int) extends Ordered[ThirdOrderKey] with Serializable {

  def compare(other:ThirdOrderKey):Int ={

    if(this.first-other.first!=0) {
      this.first-other.first
    }
    else if(this.second - other.second !=0)
    {
      this.second-other.second
    }
    else
    {
      this.thrid - other.thrid
    }
  }
}

  

spark中自定义多维度排序

原文:http://www.cnblogs.com/jackie2016/p/5667025.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!