日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Spark _13_二次排序问题

發(fā)布時(shí)間:2024/2/28 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spark _13_二次排序问题 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

使用javaAPI(使用到對象排序)


數(shù)據(jù):

3 1 5 2 6 5 8 123 1 4 4 123 5 432 3 54 5 121 8 654 3 98

代碼:

package ddd;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.PairFunction; import org.apache.spark.api.java.function.VoidFunction; import scala.Tuple2;/*** @author George* @description**/ public class SecondSort {public static void main(String[] args) {SparkConf conf = new SparkConf().setAppName("ss").setMaster("local");JavaSparkContext sc = new JavaSparkContext(conf);JavaRDD<String> tf = sc.textFile("./data/secondSort.txt");JavaPairRDD<SecondSortKey, String> map = tf.mapToPair(new PairFunction<String, SecondSortKey, String>() {@Overridepublic Tuple2<SecondSortKey, String> call(String s) throws Exception {String[] strings = s.split(" ");int first = Integer.parseInt(strings[0]);int second = Integer.parseInt(strings[1]);SecondSortKey secondSortKey = new SecondSortKey(first, second);return new Tuple2<>(secondSortKey, s);}});map.sortByKey(false).foreach(new VoidFunction<Tuple2<SecondSortKey, String>>() {@Overridepublic void call(Tuple2<SecondSortKey, String> secondSortKeyStringTuple2) throws Exception {System.out.println(secondSortKeyStringTuple2._2);}});} }************************************************************************ package ddd;import java.io.Serializable;/*** @author George* @description**/ public class SecondSortKey implements Serializable,Comparable<SecondSortKey> {private int first;private int second;public SecondSortKey() {}public SecondSortKey(int first, int second) {this.first = first;this.second = second;}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;}@Overridepublic int compareTo(SecondSortKey o) {if (this.getFirst() - o.getFirst() == 0){return this.getSecond() - o.getSecond();}else{return this.getFirst()-o.getFirst();}} }

結(jié)果展示:

scalaAPI:

?

package suanziimport org.apache.spark.{SparkConf, SparkContext} // object SecondSort {def main(args: Array[String]): Unit = {val sparkConf = new SparkConf().setMaster("local").setAppName("SecondSort")val sc = new SparkContext(sparkConf)sc.setLogLevel("error")val rdd = sc.textFile("./data/secondSort.txt")val mapRdd = rdd.map(x => {(new SecondSortKey(x.split(" ")(0).toInt, x.split(" ")(1).toInt), null)}) // mapRdd.foreach(println)val sortRdd = mapRdd.sortByKey(false)sortRdd.map(_._1).foreach(println)/*** 8 654* 8 123* 6 5* 5 432* 5 121* 5 2* 4 123* 3 98* 3 54* 3 1* 1 4*/sc.stop()} } class SecondSortKey(val first:Int,val second:Int) extends Ordered[SecondSortKey] with Serializable{override def compare(that: SecondSortKey): Int = {if (this.first - that.first == 0){this.second - that.second}else{this.first - that.first}}override def toString: String = {this.first + " " +this.second} }

?

總結(jié)

以上是生活随笔為你收集整理的Spark _13_二次排序问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。