【发布时间】:2017-12-20 08:30:24
【问题描述】:
如何在 Spark 中进行异常处理 - Scala 中的无效记录 这是我的代码:
val rawData = sc.textFile(file)
val rowRDD = rawData.map(line => Row.fromSeq(line.split(",")))
val rowRDMapped = rowRDD.map { x => x.get(1), x.get(10) }
val DF = rowRDMapped.toDF("ID", "name" )
如果输入数据正常,一切正常,如果我没有足够的字段,我会得到 ArrayIndexOutOfBoundException。
我正在尝试使用 try-catch,但我无法通过 try catch 跳过包含无效数据的记录
val rowRDMapped = rowRDD.map { try {
x => x.get(1), x.get(10)
}catch {
println("Invalid Data")
//Here it expects to return ROW, but I am not sure what to do here, since I dont want any data to be returned.
}
}
请让我知道如何使用 try catch 解决问题,如果有更好的解决方案,那也会有很大帮助
【问题讨论】:
-
你想要什么?跳线太短?
-
@Zernike,感谢您的关注,是的,我需要跳过短行,这会导致 Index out of bound 异常。
标签: scala apache-spark exception-handling try-catch