标签:
scala相较于java,代码就精简很多了:
import org.apache.spark._ import SparkContext._ object SecondarySort { def main(args: Array[String]) { val sparkConf = new SparkConf().setAppName(" Secondary Sort ") sparkConf.set("mapreduce.framework.name", "yarn"); sparkConf.set("spark.rdd.compress", "true"); sparkConf.set("spark.serializer","org.apache.spark.serializer.KryoSerializer"); sparkConf.set("spark.storage.memoryFraction", "0.5"); sparkConf.set("spark.akka.frameSize", "100"); sparkConf.set("spark.default.parallelism", "1"); val sc = new SparkContext(sparkConf) val file = sc.textFile("hdfs://namenode:9000/test/secsortdata") val rdd = file.map(line => line.split("\t")). map(x => (x(0),x(1))).groupByKey(). sortByKey(true).map(x => (x._1,x._2.toList.sortWith(_>_))) val rdd2 = rdd.flatMap{ x => val len = x._2.length val array = new Array[(String,String)](len) for(i <- 0 until len) { array(i) = (x._1,x._2(i)) } array } sc.stop() } }
是不是非常简单,相对于mapreduce那几百行的代码真是方便快捷
标签:
原文地址:http://my.oschina.net/u/2010330/blog/493908