设为首页 加入收藏

TOP

spark异常处理
2018-12-02 17:52:42 】 浏览:83
Tags:spark 异常 处理
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/refuil/article/details/78486741

spark 异常处理
java
scala可以写成类似java的异常处理模式,如果是读取本地文件,
import java.io.FileReaderimport
java.io.FileNotFoundException
import java.io.IOException
object Demo {
def main(args: Array[String]) {
try {
val f = new FileReader("input.txt")
} catch {
case ex: FileNotFoundException =>{
println("Missing file exception")
} case ex: IOException => {
println("IO Exception") } } }}

简单的 判断文件是否存在
val sc: SparkContext = eachRdd.sparkContext
val hadoopConf: Configuration = sc.hadoopConfiguration
val fs: FileSystem = org.apache.hadoop.fs.FileSystem.get(hadoopConf)
// 这里是否不需要collect?
val lines: Array[(String, FtpMap)] = oiddRdd.collect()
// 文件名流转化为文件数据流
lines.foreach {
eachFileJson: (String, FtpMap) => {
  val topic: String = eachFileJson._1
  printLog.info("topic: " + topic)
  val fileJson = eachFileJson._2
  val filePath = fileJson.file_path
  val fileExists: Boolean = try {
    fs.exists(new org.apache.hadoop.fs.Path(filePath))
  } catch {
    case e: Exception => {
      printLog.error("Exception: filePath:" + filePath + " e:" + e)
      false
    }
  }
  if (fileExists) {
    val lines: RDD[String] = sc.textFile(filePath)
  }
}


Try和两个子类 Success[T] 和Failture[T]
但是如果是spark代码,读hdfs时是lazy模式,所以即使使用try-catch运行时一旦某行格式错误也会报错:
尤其针对读取 hdfs文件时报错 Caused by: java.lang.ArrayIndexOutOfBoundsException
重要方法是将string在 split的时候 Try 或的 success或failure 类型。
val tokens = lines.flatMap(_ split " ")
   .map (s => Try(s(10)))
得到Try的子类Success或者Failure,如果计算成功,返回Success的实例,如果抛出异常,返回Failure并携带相关信息
import scala.util.{Try, Success, Failure}
def divideBy(x: Int, y: Int): Try[Int] = {     //该步骤获得Success[Int] 或Failure[Int]
  Try(x / y)
}
println(divideBy(1, 1).getOrElse(0)) // 1    //该步骤重新获取Try[T]中的T类型
println(divideBy(1, 0).getOrElse(0)) //0
详细的Option,Either和Try数据处理参考:http://blog.csdn.net/jasonding1354/article/details/46822417
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇spark上部署微服务 下一篇SPARK模型实例:两种方法实现随机..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目