zoukankan      html  css  js  c++  java
  • Spark IMF传奇行动第19课:spark排序总结

    今晚听了王家林老师的Spark IMF传奇行动第19课:spark排序,作业是:1、scala 实现二次排序,使用object apply 2;自己阅读RangePartitioner 

    代码如下:

    /**
     * Created by 王家林 on 2016/1/10.
     */
    object SecondarySortApp {
      def main(args: Array[String]){
        val conf = new SparkConf()  //创建SparkConf对象
        conf.setAppName("SecondarySortApp")    //设置应用程序名称,程序运行的监控界面可以看到名称
        conf.setMaster("local")    //此时,程序在本地运行,不需要安装Spark集群
    
        val sc = new SparkContext(conf)  //通过传入SparkConf实例来定制Spark运行具体参数和配置信息来创建SparkContext对象
    
        val lines = sc.textFile("src/Scdfile")   //读取一个本地文件
    
        val pairWithKey = lines.map(line => (
          new SecondarySortKey(line.split(" ")(0).toInt,line.split(" ")(1).toInt) , line
        ))
    
        val sorted = pairWithKey.sortByKey(false)
    
        val sortedResult = sorted.map(line => line._2)
    
        sortedResult.collect.foreach(println)
    
        sc.stop()
    
      }
    
    class SecondarySortKey(val first:Int,val second:Int) extends Ordered[SecondarySortKey] with Serializable{
      def compare(other:SecondarySortKey):Int ={
        if(this.first - other.first !=0){
          this.first - other.first
        }else{
          this.second - other.second
        }
      }
    }
    
    

    排序结果为:

    8 7
    4 3
    4 1
    3 2
    2 3
    2 1

    后续课程可以参照新浪微博 王家林_DT大数据梦工厂:http://weibo.com/ilovepains

    王家林  中国Spark第一人,微信公共号DT_Spark

    转发请写明出处。

  • 相关阅读:
    Ignoring HTTPS certificates
    利用Httponly提升web应用程序安全性
    HttpUrlConnection java.net.SocketException: Software caused connection abort: recv failed
    DISPOSE_ON_CLOSE 和 EXIT_ON_CLOSE 的区别
    Swing多线程
    攒机知识积累
    数组最大子数组和
    fork()详解
    理解Socket编程【转载】
    STM32F407_LED代码
  • 原文地址:https://www.cnblogs.com/haitianS/p/5156270.html
Copyright © 2011-2022 走看看