如何保证所有的 Spark worker 都停止了?

How to guarantee that all Spark workers have been stopped?

我的目标是将每个 Spark worker 的所有数据累积到一个文件中。

我阅读了一些 article 的解决方案,其中作者鼓励使用 org.apache.hadoop.fs.FileUtil#copyMerge 方法来解决类似问题。我决定在我的项目中实现它,这就是我所拥有的:

 try (JavaSparkContext sparkCtx = new JavaSparkContext(sparkConf)) {
     // reading, transforming and storing RDDs to the text files
     FileUtil.copyMerge(...) // merge them altogether into the single file
 } // 'try-with-resources' eventually closes spark context

在实施这种方法时我感到困惑:如果我 运行 这段代码,我最终会 运行 它在每个工作实例上,它们将相互覆盖。如果某些工人不完成工作会怎样?每个工人都会有自己的副本最终单个文件?

我意识到我需要找到一些place/method来保证所有的worker都停止执行并且我可以从哪里开始数据积累。

如何实现?我的猜测是 运行 这个数据在 try-with-resources 块之后累积,对吗?

FileUtil 完全独立于 Spark,不使用 Spark worker 或 executor。

如果您想确保它在 Spark 应用程序完成后执行,您可以在停止上下文后立即调用它。

sparkCtx.stop();
FileUtil.copyMerge(...)