如何捕捉火花异常并做点什么?

时间:2016-11-07 12:24:51

标签: scala hadoop apache-spark

当我将数据保存到hdfs失败时,如何捕获异常并在catch块中执行某些操作。像这样:

try {
    item.map(                    
       r => doSome(r).saveAsTextFiles(outputPath + "/data")
} catch {
      case e: Exception => {
      val failMessage = "Exception from output part" + e.getClass + "\t" + e.getMessage
      println("The exception is executed")
      update(aaa)
      }
} finally {
      mc.close()
}

我想在保存操作抛出异常时更新某些状态。怎么做?

1 个答案:

答案 0 :(得分:1)

使用NonFatal希望它能解决您的问题

import scala.util.control.NonFatal

try {
    item.map(                    
       r => doSome(r).saveAsTextFiles(outputPath + "/data")
} catch {
  case NonFatal(error) => {
     val failMessage = s"Exception from output part $error"
     println("The exception is executed")
     update(aaa)
  }
} finally {
      mc.close()
}