###############################################
数据如下:
年份:气温
2005 19
2005 40
2006 30
2008 50
2007 35
2007 32
实现效果:
(2005,40)
(2006,30)(2007,35)
(2008,50)
import org.apache.spark.{SparkConf, SparkContext} object WordCount { def main(args: Array[String]): Unit = { val conf = new SparkConf(); conf.setMaster("local") conf.setAppName("wangjk") conf.set("spark.testing.memory", "2147480000") val sc = new SparkContext(conf); val r1=sc.textFile("file:///D:/docment/spark3.txt") val r3=r1.map(e=>(e.split(" ")(0),e.split(" ")(1))) //e 为当前文本的一行,按照空格切开,成为元组(2005,19) .groupBy(e=>e._1) //按照key聚合 (2005,CompactBuffer((2005,19), (2005,40))) .map(e=>(e._2.toList.sorted.reverse(0))).sortByKey() //将第二个元素转成list 排序 倒排序 取第一个元素 r3.foreach(println) } }