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

新Spark Session中无法找到Hive表/视图问题求助

问题描述

我通过df.write.saveAsTable('df')创建了Spark表,在当前Spark Session中使用spark.sql查询该表一切正常,且能在spark-warehouse目录下看到对应数据。但创建新的Spark Session后,执行查询时触发如下错误:

AnalysisException: [TABLE_OR_VIEW_NOT_FOUND] The table or view df cannot be found. Verify the spelling and correctness of the schema and catalog.
If you did not qualify the name with a schema, verify the current_schema() output, or qualify the name with the correct schema and catalog.
To tolerate the error on drop use DROP VIEW IF EXISTS or DROP TABLE IF EXISTS.; line 1 pos 14;
'Project [*]
+- 'UnresolvedRelation [df], [], false

我已在Spark Session构建时通过以下代码启用Hive支持:

spark = SparkSession.builder \
    .enableHiveSupport() \
    .getOrCreate()

我使用的是jovyan pyspark jupyter notebook镜像,对应的docker-compose.yml配置如下:

version: "3.3"

services:
  pyspark:
    container_name: spark_connector
    image: jupyter/pyspark-notebook:latest
    ports:
      - "8888:8888"
    volumes:
      - ./:/home/jovyan/work
解决方法
  • 显式指定表所属schema
    未指定schema时,创建的表默认归属于default库。新Session查询时,可直接用全限定表名查询:spark.sql("SELECT * FROM default.df"),或者先切换默认schema:spark.sql("USE default")再执行查询。

  • 配置共享Hive元数据存储
    启用enableHiveSupport()后,默认元数据存于临时目录,新Session无法读取。需在构建Session时指定元数据和warehouse的持久化路径(挂载到本地目录):

    spark = SparkSession.builder \
        .enableHiveSupport() \
        .config("hive.metastore.warehouse.dir", "/home/jovyan/work/spark-warehouse") \
        .config("javax.jdo.option.ConnectionURL", "jdbc:derby:;databaseName=/home/jovyan/work/metastore_db;create=true") \
        .getOrCreate()
    

    这样元数据会保存在挂载的本地目录中,新Session可正常读取。

  • 检查目录权限
    确保容器内spark-warehouse和metastore_db目录的读写权限属于jovyan用户,可在容器内执行:

    chown -R jovyan:users /home/jovyan/work/spark-warehouse /home/jovyan/work/metastore_db
    
  • 创建表时显式指定数据路径
    保存表时绑定数据存储路径,强化元数据与数据文件的关联:

    df.write \
      .option("path", "/home/jovyan/work/spark-warehouse/df") \
      .saveAsTable("df")
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 08:40:06