spark二次排序簡單例子(JAVA)

maven依賴:

<dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-core_2.10</artifactId>
    <version>1.6.0</version>
</dependency>
public class SecondarySortBean implements Ordered<SecondarySortBean>,Serializable {
    private int first;
    private int second;
    public SecondarySortBean(int first, int second){
        this.first = first;
        this.second = second;
    }
    public int compare(SecondarySortBean that) {
        if (this.first-that.getFirst() != 0){
            return this.first-that.getFirst();
        }else{
            return this.second - this.getSecond();
        }
    }

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

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

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

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

    public int compareTo(SecondarySortBean that) {
        if (this.first-that.getFirst() != 0){
            return this.first-that.getFirst();
        }else{
            return this.second - this.getSecond();
        }
    }
    /**
     * Getter method for property <tt>first</tt>
     *
     * @return property value of first
     */
    public int getFirst() {
        return first;
    }

    /**
     * Setter method for property <tt>first</tt>.
     *
     * @param first value to be assigned to property first
     */
    public void setFirst(int first) {
        this.first = first;
    }

    /**
     * Getter method for property <tt>second</tt>
     *
     * @return property value of second
     */
    public int getSecond() {
        return second;
    }

    /**
     * Setter method for property <tt>second</tt>.
     *
     * @param second value to be assigned to property second
     */
    public void setSecond(int second) {
        this.second = second;
    }
}

public class SecondarySortTest {
    public static void main(String[] args) {
        /**
         * 創建spark配置對象SparkConf,設置spark運行時配置信息,
         * 例如通過setMaster來設置程序要連接的集羣的MasterURL,如果設置爲local         * spark爲本地運行
         */
        SparkConf conf = new SparkConf().setAppName("My first spark").setMaster("local");
        /**
         * 創建JavaSparkContext對象
         * SparkContextspark所有功能的唯一入口,
         * SparkContext核心作用,初始化spark運行所需要的核心組件,同時還會負責spark程序在master的註冊。
         *
         */
        JavaSparkContext sc = new JavaSparkContext(conf);
        /**
         * 根據數據來源,通過JavaSparkContext來創建RDD
         */
        JavaRDD<String> lines = sc.textFile("E:/secondarysort.txt");
        JavaPairRDD<SecondarySortBean, String> pairs = lines.mapToPair(new PairFunction<String, SecondarySortBean, String>() {
            public Tuple2<SecondarySortBean, String> call(String s) throws Exception {
                String[] split = s.split(" ");
                return new Tuple2<SecondarySortBean, String>(
                        new SecondarySortBean(Integer.parseInt(split[0]), Integer.parseInt(split[1])), s);
            }
        });
        JavaPairRDD<SecondarySortBean, String> sorted = pairs.sortByKey();

        JavaRDD<String> map = sorted.map(new Function<Tuple2<SecondarySortBean, String>, String>() {
            public String call(Tuple2<SecondarySortBean, String> v1) throws Exception {
                return v1._2;
            }
        });
        map.foreach(new VoidFunction<String>() {
            public void call(String s) throws Exception {
                System.out.println(s);
            }
        });
        sc.stop();
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章