首页 > 解决方案 > 在 Spark 的驱动程序上捕获 Dataset foreachPartition() 函数中引发的异常?

问题描述

我正在尝试找到一种方法来捕获 Spark 在其驱动程序的 foreachPartition() 方法中引发的异常。在数据集上使用 foreachPartition() 时,我传递了一个跨多个工作人员执行的 lambda 函数。此过程可能会引发异常。但是我想不出一种方法来在驱动程序上捕获这个异常。看来这是设计使然。我能做些什么来改变这一点吗?这是我目前正在做的一个例子:

public static void driverClassExecute() {
    Dataset<ModelDTO> dataset = getSomeData();
    dataset.foreachPartition(AClass::methodCanThrowException);
    //How can I recover if the above throws an exception?
}
public static void methodCanThrowException(Iterator<ModelDTO> it) throws Exception {
    //do stuff. If bad, throw exception. This crashes the driver.
    throw new Exception("any exception");
}

我也在使用 Eclipse Oxygen IDE,以防编译器很重要。

标签: javaapache-sparkexceptiondriverapache-spark-dataset

解决方案


在这种情况下,foreachPartition 将引发异常,因此您可以将该调用包装在 try-catch 中并像任何其他异常一样处理,尽管 spark 作业已经失败。为避免作业失败,您必须在 methodCanThrowException 中处理异常。


推荐阅读