测试数据
1 5
2 4
3 6
1 3
2 1
输出结果
1 3
1 5
2 1
2 4
3 6
实现思路:
1.实现自定义的key,要实现Ordered接口和Serializable接口,在key中实现自己对多个列的排序算法
2.将包含文本的RDD,映射成key为自定义key,value为文本的JavaPariRDD
3.使用sortByKey算子按照自定义的key进行排序
4.再次映射,剔除自定义的key,而只保留文本行
自定义的key:SecondarySortKey_12
package cn.spark.study.core;
import java.io.Serializable;
import scala.math.Ordered;
/**
* 自定义的二次排序key
* @author Administrator
*
*/
public class SecondarySortKey_12 implements Ordered<SecondarySortKey_12>,Serializable{
private static final long serialVersionUID = 1L;
//首先在自定义的key里面,定义需要进行排序的列
private int first;
private int second;
public SecondarySortKey_12(int first, int second) {
this.first = first;
this.second = second;
}
@Override
public boolean $greater(SecondarySortKey_12 other) {
if(this.first > other.getFirst()){
return true;
}
else if (