Spark的高级排序(二次排序)

为了多维的排序,需要考虑多个条件,这要求我们自定义key
1 23
3 22
3 31
1 12
2 11
4 45

二、使用java实现
2.1、自定义key
使用scala.math.Ordered接口,实现Serializable接口
package com.chb.sparkDemo.secondarySort;

import java.io.Serializable;

import scala.math.Ordered;
/**
 * Spark 二次排序自定义key
 * 使用scala.math.Ordered接口
 * @author 12285
 */
public class MyKey implements Ordered<MyKey>, Serializable{
    private int firstKey;
    private int secondKey;

    public MyKey(int firstKey, int secondKey) {
        super();
        this.firstKey = firstKey;
        this.secondKey = secondKey;
    }   

    public int getFirstKey() {
        return firstKey;
    }
    public int getSecondKey() {
        return secondKey;
    }
    public void setFirstKey(int firstKey) {
        this.firstKey = firstKey;
    }
    public void setSecondKey(int secondKey) {
        this.secondKey = secondKey;
    }


    public boolean $greater(MyKey other) {
        if (this.getFirstKey() > other.getFirstKey()) {
            return true;
        }else if(this.getFirstKey() == other.getFirstKey() && this.getSecondKey() > other.getSecondKey()){
            return true;
        }else {
            return false;
        }
    }
    public boolean $greater$eq(MyKey other) {
        if ($greater(other) || this.getFirstKey()==other.getFirstKey() && this.getSecondKey() == other.getSecondKey()) {
            return true;
        }
        return false;
    }
    public boolean $less(MyKey other) {
        if (this.getFirstKey() < other.getFirstKey()) {
            return true;
        }else if(this.getFirstKey() == other.getFirstKey() && this.getSecondKey() < other.getSecondKey()){
            return true;
        }else {
            return false;
        }
    }
    public boolean $less$eq(MyKey other) {
        if ($less(other) || this.getFirstKey()==other.getFirstKey() && this.getSecondKey() == other.getSecondKey()) {
            return true;
        }
        return false;
    }
    public int compare(MyKey other) {
        if (this.getFirstKey() != other.getFirstKey()) {
            return this.getFirstKey()-other.getFirstKey();
        }else {
            return this.getSecondKey() - other.getSecondKey();
        }
    }
    public int compareTo(MyKey other) {
        if (this.getFirstKey() != other.getFirstKey()) {
            return this.getFirstKey()-other.getFirstKey();
        }else {
            return this.getSecondKey() - other.getSecondKey();
        }
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + firstKey;
        result = prime * result + secondKey;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        MyKey other = (MyKey) obj;
        if (firstKey != other.firstKey)
            return false;
        if (secondKey != other.secondKey)
            return false;
        return true;
    }
}

2.2、具体实现步骤
第一步: 自定义key 实现scala.math.Ordered接口,和Serializeable接口 
第二步:将要进行二次排序的数据加载,按照<key,value>格式的RDD 
第三步:使用sortByKey 基于自定义的key进行二次排序 
第四步:去掉排序的key,只保留排序的结果

2.2.1、 第一步: 自定义key 实现scala.math.Ordered接口,和Serializeable接口
        JavaPairRDD<MyKey, String> mykeyPairs = lines.mapToPair(new PairFunction<String, MyKey, String>() {

            private static final long serialVersionUID = 1L;

            public Tuple2<MyKey, String> call(String line) throws Exception {
                int firstKey = Integer.valueOf(line.split(" ")[0]);
                int secondKey = Integer.valueOf(line.split(" ")[1]);
                MyKey mykey = new MyKey(firstKey, secondKey);
                return new Tuple2<MyKey, String>(mykey, line);
            }
        });

2.2.2、第三步:使用sortByKey 基于自定义的key进行二次排序
    JavaPairRDD<MyKey, String> sortPairs = mykeyPairs.sortByKey();


2.2.3、第四步:去掉排序的key,只保留排序的结果
JavaRDD<String> result = sortPairs.map(new Function<Tuple2<MyKey,String>, String>() {
            private static final long serialVersionUID = 1L;

            public String call(Tuple2<MyKey, String> tuple) throws Exception {
                return tuple._2;//line
            }
        });
        //打印排序好的结果
        result.foreach(new VoidFunction<String>() {

            private static final long serialVersionUID = 1L;

            public void call(String line) throws Exception {
                System.out.println(line);
            }
        });


三、完整代码
package com.chb.sparkDemo.secondarySort;

import io.netty.handler.codec.http.HttpContentEncoder.Result;

import java.awt.image.RescaleOp;

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;

/**
 * Spark二次排序的具体实现步骤:
 * 第一步: 自定义key 实现scala.math.Ordered接口,和Serializeable接口
 * 第二步:将要进行二次排序的数据加载,按照<key,value>格式的RDD
 * 第三步:使用sortByKey 基于自定义的key进行二次排序
 * 第四步:去掉排序的key,只保留排序的结果
 * @author 12285
 *
 */
public class SecordSortTest {
    public static void main(String[] args) {
        SparkConf conf = new SparkConf().setMaster("local").setAppName("WordCount");
        //内部实际调用的SparkContext
        JavaSparkContext jsc = new JavaSparkContext(conf);
        //读取文件,将每行数据转换为
        JavaRDD<String> lines = jsc.textFile("C:\\Users\\12285\\Desktop\\test");//hadoopRDD
        //第二步:将要进行二次排序的数据加载,按照<key,value>格式的RDD
        JavaPairRDD<MyKey, String> mykeyPairs = lines.mapToPair(new PairFunction<String, MyKey, String>() {

            private static final long serialVersionUID = 1L;

            public Tuple2<MyKey, String> call(String line) throws Exception {
                int firstKey = Integer.valueOf(line.split(" ")[0]);
                int secondKey = Integer.valueOf(line.split(" ")[1]);
                MyKey mykey = new MyKey(firstKey, secondKey);
                return new Tuple2<MyKey, String>(mykey, line);
            }
        });
        //第三步:使用sortByKey 基于自定义的key进行二次排序
        JavaPairRDD<MyKey, String> sortPairs = mykeyPairs.sortByKey();

        //第四步:去掉排序的key,只保留排序的结果

        JavaRDD<String> result = sortPairs.map(new Function<Tuple2<MyKey,String>, String>() {
            private static final long serialVersionUID = 1L;

            public String call(Tuple2<MyKey, String> tuple) throws Exception {
                return tuple._2;//line
            }
        });
        //打印排序好的结果
        result.foreach(new VoidFunction<String>() {

            private static final long serialVersionUID = 1L;

            public void call(String line) throws Exception {
                System.out.println(line);
            }
        });


    }
}


结果:
1 12
1 23
2 11
3 22
3 31
4 45

四、使用scala实现
4.1、自定义key

class SecordSortKey(val firstKey: Int, val secondKey: Int)extends Ordered[SecordSortKey] with Serializable{
    override def compare(that: SecordSortKey):Int = {
      if(this.firstKey != that.firstKey) {
        this.firstKey - that.firstKey
      }else {
        this.secondKey - that.secondKey
      }
    }  
  }
4.2、具体实现

import org.apache.spark.SparkConf
import org.apache.spark.SparkContext

object SecordSortTest {
    def main(args: Array[String]): Unit = {
      val conf = new SparkConf().setMaster("local[2]").setAppName("SecordSort")
      val sc = new SparkContext(conf);

      val lines = sc.textFile("C:\\Users\\12285\\Desktop\\test");
      //第二步:将要进行二次排序的数据加载,按照<key,value>格式的RDD
      val pairSortKey = lines.map { line => (
        new SecordSortKey(line.split(" ")(0).toInt, line.split(" ")(1).toInt),
        line
      ) };
      //第三步:使用sortByKey 基于自定义的key进行二次排序
     val sortPair = pairSortKey.sortByKey(false);

     val sortResult = sortPair.map(line=>line._2);

     sortResult.collect().foreach { x => print(x) };

    }
}

原文:https://blog.csdn.net/wuxintdrh/article/details/72809156 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会给你解答关于Spark RDD实现二次排序的问题。 首先,对于二次排序(Secondary Sort),我们需要根据两个维度来对数据进行排序,比如要按照学生的学号(第一维)和成绩(第二维)进行排序。在Spark中,可以使用sortByKey函数对数据进行排序,但默认只能按照一个维度排序。 为了实现二次排序,我们需要借助Scala的Tuple(元组)类型,将学生的学号和成绩组成一个二元组,作为RDD中的元素。然后,我们可以通过对元组进行比较,实现二次排序的效果。 下面是一个Scala实现二次排序的示例代码: ```scala // 生成包含二元组的RDD val data = sc.parallelize(Seq( (1, 85), (2, 92), (3, 76), (1, 90), (2, 88), (3, 80) )) // 按照第一维升序、第二维降序排序 val sortedData = data .sortByKey() // 按照第一维升序排序 .map(_.swap) // 交换元组的顺序,变成(成绩, 学号) .sortByKey(false) // 按照第二维降序排序 .map(_.swap) // 再次交换元组的顺序,变成(学号, 成绩) // 输出排序结果 sortedData.foreach(println) ``` 在以上示例代码中,我们首先生成一个包含二元组的RDD,其中第一维为学号,第二维为成绩。然后,我们先按照第一维升序排序,再交换元组的顺序,变成(成绩, 学号),再按照第二维降序排序,最后再次交换元组的顺序,得到最终的排序结果。 这就是Spark RDD实现二次排序的基本方法,希望能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值