1、创建SecondSortKey.scala
package SecondSort
/**
* Created by tg on 10/25/16.
*/
class SecondSortKey(val first:Int,val second:Int) extends Ordered[SecondSortKey] with Serializable {
override def compare(that: SecondSortKey): Int = {
//首先判断第一个列中的数是否相等
if(this.first-that.first!=0){
return this.first-that.first
}else{ //如果不相等,就执行else
return this.second-that.second
}
}
}
2、创建SecondSort.scala
package SecondSort
import org.apache.spark.{SparkConf, SparkContext}
/**
* Created by tg on 10/25/16.
*/
object SecondSort {
def main(args: Array[String]): Unit = {
val conf=new SparkConf().setAppName("SecondSort").setMaster("local")
val sc=new SparkContext(conf)
val lines=sc.textFile("hdfs://tgmaster:9000/in/nums")
val result=lines.map(line=>{
val info=line.split(" ")
(new SecondSortKey(info(0).toInt,info(1).toInt),line)
}).sortByKey().map(m=>m._2)
result.foreach(println _)
}
}