简体   繁体   English

在Apache Spark中捕获自定义异常

[英]catch custom Exception in Apache Spark

I have a problem to catch custom exceptions in Apache Spark. 我在捕获Apache Spark中的自定义异常时遇到问题。

When I am doing a validation on a dataset in a foreach loop like this 当我在这样的foreach循环中对数据集进行验证时

ds.foreach(
        entry=> {
          validate(entry)
        })

The validate function throws a Custom Exception when the entry is not valid. 当输入无效时,validate函数将引发Custom Exception。

But in the catch block I am not able to catch my custom exception only a SparkException is thrown and can be catched: 但是在catch块中,我无法捕获自定义异常,仅抛出SparkException并可以将其捕获:

case customException : CustomException =>
    //is never catched
case exception : SparkException =>
    //can be catched

How can I deal with that? 我该如何处理? I need to catch different sort of exceptions which are all thrown by the validate method. 我需要捕获所有由validate方法引发的异常。 One way is to read the message of the SparkException which contains the origin exception but this is probably not a nice design. 一种方法是读取包含原始异常的SparkException消息,但这可能不是一个很好的设计。

Any ideas? 有任何想法吗?

Instead of matching base exception, try to match the cause: 而不是匹配基本异常,请尝试匹配原因:

import org.apache.spark.rdd.RDD

def ignoreArithmeticException(rdd: RDD[java.lang.Integer]) = try {
  rdd.foreach(1 / _)
} catch {
  case e: SparkException => e.getCause match  {
    case _: java.lang.ArithmeticException => 
      println("Ignoring ArithmeticException")
    case _ => throw e
  }
}

This would for catch: 这将引起注意:

Try(ignoreArithmeticException(sc.parallelize(Seq(0))))
00/00/00 00:00:00 ERROR Executor: Exception in task 3.0 in stage 35.0 (TID 143)
java.lang.ArithmeticException: / by zero
    at
    ...
Ignoring ArithmeticException
res42: scala.util.Try[Unit] = Success(())

(although in a pretty verbose way), but wouldn't catch: (尽管以非常冗长的方式),但无法捕获:

Try(ignoreArithmeticException(sc.parallelize(Seq(null))))
00/00/00 00:00:00 ERROR Executor: Exception in task 3.0 in stage 38.0 (TID 155)
java.lang.NullPointerException
    at 
   ...
res52: scala.util.Try[Unit] =
Failure(org.apache.spark.SparkException: Job aborted due to stage failure: Task 3 in stage 38.0 failed 1 times, most recent failure: Lost task 3.0 in stage 38.0 (TID 155, localhost, executor driver): java.lang.NullPointerException ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM