如何处理火花map()函数的异常? [英] how to handle the Exception in spark map() function?

查看:411
本文介绍了如何处理火花map()函数的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想忽略map()函数的异常,例如:

I want to ignore Exception in map() function , for example:

rdd.map(_.toInt)

其中RDD是一个 RDD [字符串]

,但如果它满足非数字字符串,就会失败。

but if it meets non-number string, it will failed.

什么是忽略任何异常,而忽略该行的easist方式吗? (我不希望使用过滤器来处理异常,因为可能有很多其他的例外......)

what is the easist way to ignore any Exception and ignore that line? (I do not want to use filter to handle exception, because there may be so many other exceptions...)

推荐答案

您可以使用<一组合href=\"http://www.scala-lang.org/files/archive/nightly/docs/library/index.html#scala.util.Try\">Try和图/过滤器。

You can use a combination of Try and map/filter.

尝试将包裹你的计算致胜,如果他们表现不如预期,或失败,如果有异常被抛出。然后你就可以过滤你想要的 - 在这种情况下,成功的计算,但你也可以过滤错误情况进行日志记录,例如:

Try will wrap your computation into Success, if they behave as expected, or Failure, if an exception is thrown. Then you can filter what you want - in this case the successful computations, but you could also filter the error cases for logging purposes, for example.

以下code是一个可能的出发点。您可以运行,并探索它 scastie.org ,看它是否符合您的需求。

The following code is a possible starting point. You can run and explore it in scastie.org to see if it fits your needs.

import scala.util.Try

object Main extends App {

  val in = List("1", "2", "3", "abc")

  val out1 = in.map(a => Try(a.toInt))
  val results = out1.filter(_.isSuccess).map(_.get)

  println(results)

}

这篇关于如何处理火花map()函数的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆