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))
    }

    }
  • 相关阅读:
    小批量随机梯度下降
    查询文档
    自动求梯度
    数据操作
    Hadoop 入门
    Matplotlib 二维图像绘制方法
    Pandas 数据处理基础
    NumPy 数值计算基础课程
    关于 Shell 脚本
    语法分析
  • 原文地址:https://www.cnblogs.com/sunt9/p/6936412.html
Copyright © 2011-2022 走看看