输入n个数字,输出每一个数字以及其排名例如:

4

2

3

1

输出:

1  1

2  2

3  3

4  4

scala实现

package HadoopvsSpark.ScalaMap

import org.apache.spark.{HashPartitioner, SparkConf, SparkContext}

/**
* Created by Administrator on 2017/5/25.
*/
object Sorted {
def main(args: Array[String]): Unit = {
val conf=new SparkConf().setMaster("")
val sc=new SparkContext(conf)
val one=sc.textFile("")
var index=0;
//由于输入文件有多个,产生不同的分区,为了生产序号,使用HashPartitioner将中间的RDD归约到一起。
val line=one.filter(_.trim.length>0).map(num=>(num.trim.toInt,"")).partitionBy(new HashPartitioner(1)
).sortByKey().map(t=>{
index+=1
(index,t._1)
}).collect().foreach(x=>println(x._1+"\t"+x._2))
}

}
posted on 2017-06-03 10:10  流浪在伯纳乌  阅读(334)  评论(0编辑  收藏  举报