• java实现spark常用算子之SortByKey




    import org.apache.spark.SparkConf;
    import org.apache.spark.api.java.JavaPairRDD;
    import org.apache.spark.api.java.JavaSparkContext;
    import org.apache.spark.api.java.function.Function2;
    import org.apache.spark.api.java.function.VoidFunction;
    import scala.Tuple2;

    import java.util.Arrays;
    import java.util.List;

    /**
    * sortbykey([ascending],[numTasks]) 算子:
    * 根据key进行排序操作
    * 第一个参数为true,则为升序,反之为降序
    * 第二个参数决定执行的task数目
    *
    */
    public class SortByKeyOperator {
    public static void main(String[] args){
    SparkConf conf = new SparkConf().setMaster("local").setAppName("sortByKey");
    JavaSparkContext sc = new JavaSparkContext(conf);

    List<Tuple2<String,Integer>> list = Arrays.asList(
    new Tuple2<String,Integer>("w1",1),
    new Tuple2<String,Integer>("w2",2),
    new Tuple2<String,Integer>("w3",3),
    new Tuple2<String,Integer>("w2",22),
    new Tuple2<String,Integer>("w1",11)
    );

    JavaPairRDD<String,Integer> pairRdd = sc.parallelizePairs(list);

    JavaPairRDD<String,Integer> result = pairRdd.sortByKey(true,2);

    result.foreach(new VoidFunction<Tuple2<String, Integer>>() {
    @Override
    public void call(Tuple2<String, Integer> stringIntegerTuple2) throws Exception {
    System.err.println(stringIntegerTuple2._1+":"+stringIntegerTuple2._2);
    }
    });

    }
    }

    微信扫描下图二维码加入博主知识星球,获取更多大数据、人工智能、算法等免费学习资料哦!

  • 相关阅读:
    树状数组基础
    Color the ball HDU1556
    敌兵布阵 HDU1166
    线段树基础
    T9 HDU1298
    7-6 Bandwidth UVA140
    测试方法:
    测试过程:
    爬天极网美女图片缩略图:
    爬天极网美女预览图版一:
  • 原文地址:https://www.cnblogs.com/guokai870510826/p/11635218.html
Copyright © 2020-2023  润新知