Spark高級(jí)編程之二次排序

原始數(shù)據(jù):

2 6
3 7
1 5
2 4
3 6
1 3
2 1
3 1

1、Java版二次排序
首先定義排序的key

/**
 * 自定義的二次排序
 */
public class SecondarySortKey implements Ordered<SecondarySortKey>,Serializable{
   private static final long serialVersionUID = -236567544543677678L;

   //首先在自定義key里面,定義需要進(jìn)行排序的列
    private int first;
    private int second;

    public SecondarySortKey(int first, int second) {
        this.first = first;
        this.second = second;
    }

    @Override
    public boolean $greater(SecondarySortKey that) {
        if(this.first>that.getFirst()){
            return true;
        }else if(this.first==that.getFirst() &&
                this.second >that.getSecond()){
            return true;
        }
        return false;
    }

    @Override
    public boolean $greater$eq(SecondarySortKey that) {
        if(this.$greater(that)){
            return true;
        }else if(this.first==that.getFirst() &&
                this.second==that.getSecond()){
            return true;
        }
        return false;
    }

    @Override
    public boolean $less(SecondarySortKey that) {
        if(this.first<that.getFirst()){
            return true;
        }else if(this.first==that.getFirst() &&
                this.second <that.getSecond()){
            return true;
        }
        return false;
    }

    @Override
    public boolean $less$eq(SecondarySortKey that) {
        if(this.$less(that)){
            return true;
        }else if(this.first==that.getFirst() &&
                this.second== that.getSecond()){
            return true;
        }
        return false;
    }

    @Override
    public int compare(SecondarySortKey that) {
        if(this.first - that.getFirst() !=0){
            return this.first - that.getFirst();
        }else{
            return this.second - that.getSecond();
        }
    }



    @Override
    public int compareTo(SecondarySortKey that) {
        if(this.first - that.getFirst() !=0){
            return this.first - that.getFirst();
        }else{
            return this.second - that.getSecond();
        }
    }

    public int getFirst() {
        return first;
    }

    public void setFirst(int first) {
        this.first = first;
    }

    public int getSecond() {
        return second;
    }

    public void setSecond(int second) {
        this.second = second;
    }
}

然后實(shí)現(xiàn)二次排序

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.api.java.function.PairFunction;
import org.apache.spark.api.java.function.VoidFunction;
import scala.Tuple2;

/**
 * 二次排序
 */
public class SecondarySort {

    public static void main(String[] args) {
        String inputPath ="D:\\spark\\sortnumber.txt";
        long beginTime = System.currentTimeMillis();
        SparkConf conf = new SparkConf()
                .setAppName("SecondarySort")//應(yīng)用程序的名稱
                .setMaster("local");//本地運(yùn)行
        //2、創(chuàng)建JavaSparkContext對(duì)象
        JavaSparkContext sc = new JavaSparkContext(conf);
        JavaRDD<String> lines = sc.textFile(inputPath);
        JavaPairRDD<SecondarySortKey, String> pairRDD = lines.mapToPair(new PairFunction<String, SecondarySortKey, String>() {
            @Override
            public Tuple2<SecondarySortKey, String> call(String line) throws Exception {
                String[] split = line.split(" ");
                SecondarySortKey key = new SecondarySortKey(Integer.valueOf(split[0]), Integer.valueOf(split[1]));
                return new Tuple2<SecondarySortKey, String>(key, line);
            }
        });

        JavaPairRDD<SecondarySortKey, String> sortedPairs = pairRDD.sortByKey();
        JavaRDD<String> sortedLines = sortedPairs.map(new Function<Tuple2<SecondarySortKey, String>, String>() {
            @Override
            public String call(Tuple2<SecondarySortKey, String> v1) throws Exception {
                return v1._2;
            }
        });
        sortedLines.foreach(new VoidFunction<String>() {
            @Override
            public void call(String s) throws Exception {
                System.out.println(s);
            }
        });
        sc.close();
    }
}

2、Scala版二次排序
首先定義排序的key

class SecondarySortKey(val first:Int,val second:Int) extends Ordered[SecondarySortKey] with Serializable {
   def compare(that: SecondarySortKey): Int = {
     if(this.first- that.first !=0 ){
       this.first - that.first
     }else{
       this.second -that.second
     }
  }
}

然后實(shí)現(xiàn)二次排序

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

object SeconDarySort {

  def main(args: Array[String]): Unit = {
    val inputPath = "D:\\spark\\sortnumber.txt"
    val  conf = new SparkConf()
      .setAppName("SeconDarySort")
      .setMaster("local")
    val sc = new SparkContext(conf)
    val lines =sc.textFile(inputPath,1)
    val pairs = lines.map(line =>
      (new SecondarySortKey(line.split(" ")(0).toInt,line.split(" ")(1).toInt),line))
    val sortedPairs = pairs.sortByKey()
    val sortedList = sortedPairs.map(_._2)
    sortedList.foreach(println)
  }
}

排序結(jié)果一樣:

1 3
1 5
2 1
2 4

2 6
3 1
3 6
3 7

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.插入排序—直接插入排序(Straight Insertion Sort) 基本思想: 將一個(gè)記錄插入到已排序好...
    依依玖玥閱讀 1,356評(píng)論 0 2
  • 排序的基本概念 在計(jì)算機(jī)程序開(kāi)發(fā)過(guò)程中,經(jīng)常需要一組數(shù)據(jù)元素(或記錄)按某個(gè)關(guān)鍵字進(jìn)行排序,排序完成的序列可用于快...
    Jack921閱讀 1,574評(píng)論 1 4
  • 沒(méi)有駛向遠(yuǎn)方的輪船 就用軀體做前行的木筏 雙槳長(zhǎng)出知更鳥的翅膀 河流鋪平鯨魚的道路 胸膛里的一千只火把 在沒(méi)有星光...
    黃英雄閱讀 276評(píng)論 0 3
  • 榮故事:鉆石戒指 結(jié)婚的時(shí)候,老公帶我到銀座買了六福的首飾,選中了一顆鉆石。他選的我不喜歡,我又選了一顆大一點(diǎn)的鉆...
    寇榮2020閱讀 198評(píng)論 0 0
  • 沒(méi)有寶寶喜歡一生氣就開(kāi)始大吼大叫的爸媽,有研究表明父母的情緒越是穩(wěn)定孩子越具有幸福感和安全感。反之,則是對(duì)孩子的傷...
    孫老師說(shuō)閱讀 519評(píng)論 0 0

友情鏈接更多精彩內(nèi)容