Swadeshi
Swadeshi

Reputation: 1672

Spark CSV Dataframe Load ,File not found

How to catch exception , if my file is not found in given location in spark csv read?

try {
    val df = sqlCxt.read.format("csv")
    .option("delimiter", "|")
    .load("C:/Users/swapnil.shirke/Downloads/dataset/u.item");

}
catch {

case ex: FileNotFoundException => {
println(" file missing exception")
}
case ex: Exception => ex.printStackTrace()
}

given try catch condition not worked.

Upvotes: 3

Views: 1675

Answers (1)

Ramesh Maharjan
Ramesh Maharjan

Reputation: 41987

In scala, if the file path is not correct then the exception is org.apache.spark.sql.AnalysisException: Path does not exist: .... so the correct way is

try {
  val df = sqlCxt.read.format("csv")
    .option("delimiter", "|")
    .load("/Users/swapnil.shirke/Downloads/dataset/u.item");

}
catch {
  case ex: AnalysisException => {
    println(" file missing exception")
  }
  case ex: Exception => ex.printStackTrace()
}

I hope the answer is helpful

Upvotes: 4

Related Questions