• Scala实现Mapreduce程序3----数据排序


    输入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+" "+x._2))
    }

    }
  • 相关阅读:
    vue 短期时间
    使用moment.js写一个倒计时
    使用moment.js
    js 共用文件内容应用
    什么是闭包?闭包的优缺点?
    卧龙阶段
    音阶
    初步理解1=C
    认识简谱的构造,唱名,音名

  • 原文地址:https://www.cnblogs.com/sunt9/p/6936412.html
Copyright © 2020-2023  润新知