方式一
| public class SecondSortTest { |
| public static void main(String[] args) { |
| SparkConf conf = new SparkConf().setMaster("local").setAppName("secondSort"); |
| JavaSparkContext jsc = new JavaSparkContext(conf); |
| JavaRDD<String> lineRDD = jsc.textFile("data/secondSort"); |
| |
| lineRDD |
| .mapToPair(new PairFunction<String, SecondSortKey, String>() { |
| @Override |
| public Tuple2<SecondSortKey, String> call(String s) throws Exception { |
| String[] split = s.split(" "); |
| SecondSortKey secondSortKey = new SecondSortKey(Integer.parseInt(split[0]), Integer.parseInt(split[1])); |
| return new Tuple2<>(secondSortKey, s); |
| } |
| }) |
| .sortByKey() |
| .foreach(new VoidFunction<Tuple2<SecondSortKey, String>>() { |
| @Override |
| public void call(Tuple2<SecondSortKey, String> secondSortKeyStringTuple2) throws Exception { |
| System.out.println(secondSortKeyStringTuple2); |
| } |
| }); |
| |
| jsc.stop(); |
| } |
| } |
| |
| public class SecondSortKey implements Serializable, Comparable<SecondSortKey> { |
| private int first; |
| private int second; |
| |
| public SecondSortKey(int first, int second) { |
| this.first = first; |
| this.second = second; |
| } |
| |
| public SecondSortKey() { |
| } |
| |
| 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; |
| } |
| |
| @Override |
| public int compareTo(SecondSortKey other) { |
| if (first - other.getFirst() == 0) { |
| System.out.println("diaoyng 1"); |
| return second - other.getSecond(); |
| } else { |
| System.out.println("diaoyong 2"); |
| return first - other.getFirst(); |
| } |
| } |
| } |
方式二
| class secondSort { |
| def main(args: Array[String]): Unit = { |
| val conf = new SparkConf() |
| conf.setMaster("local").setAppName("secondsort") |
| val sc = new SparkContext(conf) |
| val lineRDD = sc.textFile("data/secondSort") |
| lineRDD.map(x=>{ |
| val strings = x.split(" ") |
| (new SecondSortKeyScala(strings(0).toInt, strings(1).toInt), x) |
| }) |
| .sortByKey() |
| .foreach(x=>{println(x._2)}) |
| |
| sc.stop() |
| } |
| } |
| |
| class SecondSortKeyScala(val first: Int, val second: Int) extends Serializable with Ordered[SecondSortKeyScala] { |
| override def compare(that: SecondSortKeyScala): Int = { |
| val i = this.first - that.first |
| if (i == 0) { |
| return this.second - that.second |
| } |
| i |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?