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

Databricks中如何获取Spark会话级配置变量?spark.conf.getAll()报错

问题描述

在Databricks中设置会话级配置变量后,无法通过Spark上下文配置找到该变量,且调用spark.conf.getAll()时出现属性错误:

spark.conf.set(f"dataset.bookstore", '123') # 设置会话级变量
spark.conf.get(f"dataset.bookstore") # 返回'123'
scf = spark.sparkContext.getConf()
allc = scf.getAll()
scf.contains(f"dataset.bookstore") # 返回False

执行all_session_vars = spark.conf.getAll()时报错:

AttributeError: 'RuntimeConfig' object has no attribute 'getAll'

需获取所有会话级配置变量。

解决方案

Databricks中spark.conf对应的RuntimeConfig无直接的getAll()方法,但可通过执行SQL的SET命令获取所有会话级配置:

# 执行SET命令并收集结果
session_configs = spark.sql("SET").collect()

# 转换为字典格式便于使用
session_config_dict = {row.key: row.value for row in session_configs}

# 验证自定义配置是否存在
print(session_config_dict.get("dataset.bookstore")) # 输出'123'

补充说明

  • SET命令会返回当前会话的所有配置变量,包含通过spark.conf.set设置的自定义变量,以及Spark默认的会话级配置。
  • Spark上下文(spark.sparkContext.getConf())对应集群/上下文级配置,不会包含会话级自定义配置,这也是之前无法在其中找到dataset.bookstore的原因。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 15:45:57