-1

我正在用java编写spark代码。当我使用foreachAsync火花失败并给我java.lang.IllegalStateException: Cannot call methods on a stopped SparkContext.

在这段代码中:

JavaSparkContext sparkContext = new JavaSparkContext("local","MyAppName");
    JavaPairRDD<String, String> wholeTextFiles = sparkContext.wholeTextFiles("somePath");
    wholeTextFiles.foreach(new VoidFunction<Tuple2<String, String>>() {
        public void call(Tuple2<String, String> stringStringTuple2) throws Exception {
            //do something
        }
    });

它工作正常。但在这段代码中:

JavaSparkContext sparkContext = new JavaSparkContext("local","MyAppName");
    JavaPairRDD<String, String> wholeTextFiles = sparkContext.wholeTextFiles("somePath");

    wholeTextFiles.foreachAsync(new VoidFunction<Tuple2<String, String>>() {
        public void call(Tuple2<String, String> stringStringTuple2) throws Exception {
            //do something
        }
    });

它返回错误。我哪里错了?

4

1 回答 1

3

这是因为foreachAsync返回一个 Future 对象,当你离开一个函数时,火花上下文是关闭的(因为它是在本地创建的)。

如果您调用get()foreachAsync()则主线程将等待 Future 完成。

于 2017-09-20T16:38:58.953 回答