zoukankan      html  css  js  c++  java
  • 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))
    }

    }
  • 相关阅读:
    python返回函数与匿名函数
    Session&Cookie
    write RE validation
    hello2 source anaylis
    Filter
    Development descriptor
    web.xml配置详解
    Annotation
    injector
    container
  • 原文地址:https://www.cnblogs.com/sunt9/p/6936412.html
Copyright © 2011-2022 走看看