spark常用RDD算子 - SortByKey

SortByKey用于对pairRDD按照key进行排序,第一个参数可以设置true或者false,默认是true
 

List> data = new ArrayList<>();
data.add(new Tuple2<>("A", 10));
data.add(new Tuple2<>("A", 20));
data.add(new Tuple2<>("B", 2));
data.add(new Tuple2<>("B", 3));
data.add(new Tuple2<>("C", 5));

JavaPairRDD stringIntegerJavaPairRDD = javaSparkContext.parallelizePairs(data);

//升序
System.out.println(stringIntegerJavaPairRDD.sortByKey().collect());
//返回的结果
//[(A,10), (A,20), (B,2), (B,3), (C,5)]

//降序
System.out.println(stringIntegerJavaPairRDD.sortByKey(false).collect());
//返回的结果
//[(C,5), (B,2), (B,3), (A,10), (A,20)]

 

你可能感兴趣的:(#,spark,spark)