java的火花+:org.apache.spark.SparkException:作业已中止:任务不序列化:java.io.NotSerializableException [英] java+spark: org.apache.spark.SparkException: Job aborted: Task not serializable: java.io.NotSerializableException

查看:548
本文介绍了java的火花+:org.apache.spark.SparkException:作业已中止:任务不序列化:java.io.NotSerializableException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的火花,并试图运行示例JavaSparkPi.java,它运行良好,但由于我在另一个Java s到使用这是我从主所有的东西复制到一个方法的类并尝试要调用的方法主,它甾体抗炎药

I'm new to spark, and was trying to run the example JavaSparkPi.java, it runs well, but because i have to use this in another java s I copy all things from main to a method in the class and try to call the method in main, it saids

org.apache.spark.SparkException:作业已中止:任务不序列化:
  java.io.NotSerializableException

org.apache.spark.SparkException: Job aborted: Task not serializable: java.io.NotSerializableException

在code是这样的:

public class JavaSparkPi {

public void cal(){
    JavaSparkContext jsc = new JavaSparkContext("local", "JavaLogQuery");
    int slices = 2;
    int n = 100000 * slices;

    List<Integer> l = new ArrayList<Integer>(n);
    for (int i = 0; i < n; i++) {
        l.add(i);
    }

    JavaRDD<Integer> dataSet = jsc.parallelize(l, slices);

    System.out.println("count is: "+ dataSet.count());
    dataSet.foreach(new VoidFunction<Integer>(){
        public void call(Integer i){
            System.out.println(i);
        }
    });

    int count = dataSet.map(new Function<Integer, Integer>() {
        @Override
        public Integer call(Integer integer) throws Exception {
            double x = Math.random() * 2 - 1;
            double y = Math.random() * 2 - 1;
            return (x * x + y * y < 1) ? 1 : 0;
        }
    }).reduce(new Function2<Integer, Integer, Integer>() {
        @Override
        public Integer call(Integer integer, Integer integer2) throws Exception {
            return integer + integer2;
        }
    });

    System.out.println("Pi is roughly " + 4.0 * count / n);
}

public static void main(String[] args) throws Exception {

    JavaSparkPi myClass = new JavaSparkPi();
    myClass.cal();
}
}

人有这样的想法?谢谢!

anyone have idea on this? thanks!

推荐答案

的嵌套函数抱到包含对象的引用( JavaSparkPi )。因此,这个对象将获得序列化。对于这个工作,它需要可序列化。简单的事情:

The nested functions hold a reference to the containing object (JavaSparkPi). So this object will get serialized. For this to work, it needs to be serializable. Simple to do:

public class JavaSparkPi implements Serializable {
  ...

这篇关于java的火花+:org.apache.spark.SparkException:作业已中止:任务不序列化:java.io.NotSerializableException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆