You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Scala Spark代码循环执行时抛出NoSuchElementException异常求助

Hey there, let's figure out why that NoSuchElementException: None.get is popping up when calling FiloDriver.shutdown(), even though your aggregation loops run perfectly fine.

What's Causing the Issue?

Looking at the error stack trace, the problem originates in FiloSetup.scala:31 when trying to access config.get. This means the FiloDriver's internal configuration wasn't properly initialized when you triggered the shutdown.

Breaking down your code flow:

  • When iDate is not set to "0001-01-01 00:00:00", you skip the FiloDB read operation entirely. This means FiloDriver never gets initialized (its internal config remains None).
  • When you call FiloDriver.shutdown() unconditionally in the finally block, the code tries to access the uninitialized config, which triggers the None.get exception.

Fixes to Try

Here are two straightforward solutions to resolve this:

1. Only Shutdown FiloDriver If It Was Actually Used

Add a flag to track whether you initialized FiloDB, then conditionally call shutdown:

// Add this at the start of your try block
var filodbWasInitialized = false

val vBillMaxDate = if (iDate == "0001-01-01 00:00:00") {
  filodbWasInitialized = true // Mark that FiloDB was initialized
  hc.read.format("filodb.spark").option("database","*****").option("dataset","*****").load().filter(s"""yr_mon in ( ${vMonthCheck} )""").agg(max("*****").alias("max_date")).map {
    case Row (value) => value.asInstanceOf[java.sql.Timestamp];
  }.first()
} else {
  new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(iDate)
}

// Update your finally block
finally{
  if (filodbWasInitialized) {
    FiloDriver.shutdown()
  }
  sc.stop()
  sys.exit(0)
}

2. Defensively Catch the Exception During Shutdown

If you prefer a safety net approach, wrap the shutdown call in a try-catch to handle cases where FiloDriver wasn't initialized:

finally{
  try {
    FiloDriver.shutdown()
  } catch {
    case e: java.util.NoSuchElementException =>
      println("Skipping FiloDriver shutdown: it was not initialized during this run.")
  }
  sc.stop()
  sys.exit(0)
}

Why This Works

Both approaches avoid calling shutdown() on an uninitialized FiloDriver. The first solution is cleaner (only runs shutdown when necessary), while the second acts as a fallback for edge cases where initialization state might be unclear.

内容的提问来源于stack exchange,提问作者Rajdip

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:57:47