方法 spark.catalog.tableExists("newDb.newTable") 抛出 NoSuchDatabaseException 而不是返回 false("newDb" 不存在)

The method spark.catalog.tableExists("newDb.newTable") throws NoSuchDatabaseException instead of returning false ("newDb" does not exist)

我有一段代码检查当前 spark 会话中是否存在给定的配置单元“database.table”。

val tableExists = spark.catalog.tableExists("newDb.newTable")

我想将结果存储在布尔值中,因此,如果 table 或 数据库 不存在,我想得到 .

我 运行 在不同的环境中使用完全相同的代码并且它工作得很好,但现在我 运行 使用 Scalatest 并得到 NoSuchDatabaseException 当数据库(在本例中为“newDb”)不存在时抛出。

知道为什么会这样吗?

你应该试试这个

spark.catalog._jcatalog.tableExists("schema_name.table_name")

问题是我正在扩展的 DataframeSuiteBase class (from Holdenkarau's spark-testing-base package) 提供的默认 SparkSession 中未启用配置单元支持。

要解决它,通过将 enableHiveSupport() 方法添加到SparkSession 构建链:

override def beforeAll(): Unit = {
  SparkSessionProvider._sparkSession = SparkSession.builder()
    .master("local") // add whatever other configurations...
    .enableHiveSupport()
    .getOrCreate()
}