ahajib
ahajib

Reputation: 13510

Scala Cannot resolve symbol AnalysisException

Scala newbie here. I am trying to catch some exceptions while reading files from S3 using spark and want my code to do nothing if Path does not exist exception occurs. For that I have a code like the following:

import org.apache.spark.sql.{Row, SQLContext, SparkSession, AnalysisException}

var partnerData: org.apache.spark.sql.DataFrame = null
if (fileType == "csv"){
  try{
    partnerData = spark.read
      .format("csv")
      .schema(inputSchema)
      .load(inputPath)
  } catch (AnalysisException e) {

  }
} else {
  partnerData = spark.read
    .format("parquet")
    .load(inputPath)
}

Although I have imported the AnalysisException, I keep seeing this message:

Cannot resolve symbol AnalysisException

Anything that I'm missing here? I am planning to detect the exception type first and then decide what to do next based on message text. I'd be grateful if you could point me to the right direction in case this doesn't make any sense as I've read multiple threads and haven't been able to find a better solution. This is much easier to do in python though.

Upvotes: 0

Views: 240

Answers (1)

Tim
Tim

Reputation: 27366

Your catch syntax is wrong, it should be

try {
  ???
} catch {
  case e: AnalysisException => // Handler for AnalysisException
  case _ => // Default handler
}

Upvotes: 2

Related Questions