gpt4 book ai didi

java - 异常: SparkException: Task not serializable

转载 作者:行者123 更新时间:2023-12-02 09:17:56 30 4
gpt4 key购买 nike

为什么这段代码会产生这个异常?怎样才能避免呢

    SparkConf conf = new SparkConf().setAppName("startingSpark").setMaster("local[*]");
JavaSparkContext sc = new JavaSparkContext(conf);

List<Tuple2<Integer, Integer>> visitsRaw = new ArrayList<>();
visitsRaw.add(new Tuple2<>(4, 18));
visitsRaw.add(new Tuple2<>(6, 4));
visitsRaw.add(new Tuple2<>(10, 9));

List<Tuple2<Integer, String>> usersRaw = new ArrayList<>();
usersRaw.add(new Tuple2<>(1, "John"));
usersRaw.add(new Tuple2<>(2, "Bob"));
usersRaw.add(new Tuple2<>(3, "Alan"));
usersRaw.add(new Tuple2<>(4, "Doris"));
usersRaw.add(new Tuple2<>(5, "Marybelle"));
usersRaw.add(new Tuple2<>(6, "Raquel"));

JavaPairRDD<Integer, Integer> visits = sc.parallelizePairs(visitsRaw);
JavaPairRDD<Integer, String> users = sc.parallelizePairs(usersRaw);

JavaPairRDD<Integer, Tuple2<Integer, String>> joinedRdd = visits.join(users);

joinedRdd.foreach(System.out::println);
sc.close();

最佳答案

子句“System.out::println”不可序列化,可以更改为:

joinedRdd.foreach(v->System.out.println(v));

或者对于驱动程序节点上的打印值,可以使用这样的构造:

joinedRdd.collect().forEach(System.out::println);

关于java - 异常: SparkException: Task not serializable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58873321/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com