今天看了下二次排序的算法,虽然还是不懂源码内部是咋处理的,但至少会把二次排序改成三次,甚至更高纬度排序了,先会用,再慢慢研究吧!下面是scala版本的程序。
1、先写key
class thirdOrderKey (val first: Int, val second: Int, val third: Int) extends Ordered[thirdOrderKey] with Serializable
{
def compare(that: thirdOrderKey): Int =
{
if(this.first - that.first != 0)
{
this.first - that.first
}
else if (this.second -that.second!=0)
{
this.second - that.second
}
else
{
this.third - that.third
}
}
}
2、再写主程序
import org.apache.spark.SparkContext
import org.apache.spark.SparkConf
object thirdOrder
{
def main(args: Array[String]): Unit =
{
val conf = new SparkConf()
.setAppName("WordCount")
.setMaster("local")
val sc = new SparkContext(conf)
val lines =sc.textFile("C://Users/Administrator/Desktop/2次排序.txt", 1);
val pairs = lines.map { line => (new thirdOrderKey(line.split(" ")(0).toInt, line.split(" ")(1).toInt,line.split(" ")(2).toInt),line)}
val sortedPairs = pairs.sortByKey()
val sortedLines = sortedPairs.map(sortedPair => sortedPair._2)
sortedLines.foreach { sortedLine => println(sortedLine) }
}
}