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

如何在Azure Synapse Spark笔记本中实现Data Lake Gen2挂载点动态化?

Azure Synapse Spark利用链接服务动态访问ADLS Gen2的实现方案

方案1:直接通过链接服务访问(无需挂载)

Synapse Spark支持直接基于链接服务访问ADLS Gen2,不用提前挂载存储。你可以用mssparkutils工具获取链接服务的配置信息,动态拼接访问路径:

# 获取目标链接服务的配置属性
ls_config = mssparkutils.linkedService.getProperties("ADLS_Gen2_Linked_Service")
# 提取存储账户和容器名(针对ADLS Gen2类型的链接服务)
storage_account = ls_config["properties"]["typeProperties"]["accountName"]
container = ls_config["properties"]["typeProperties"]["containerName"]
# 构建标准ABFSS访问路径
adls_base_path = f"abfss://{container}@{storage_account}.dfs.core.windows.net/"

# 直接用这个路径读写数据
df = spark.read.parquet(f"{adls_base_path}/raw_data/user_info")

这种方式下,测试和生产环境只需要在各自Synapse工作区配置同名的链接服务,指向对应存储账户,代码完全不用改,迁移零成本。

方案2:基于链接服务动态挂载存储

如果业务逻辑依赖挂载点,也可以用链接服务的配置动态执行挂载操作:

ls_config = mssparkutils.linkedService.getProperties("ADLS_Gen2_Linked_Service")
storage_account = ls_config["properties"]["typeProperties"]["accountName"]
container = ls_config["properties"]["typeProperties"]["containerName"]
# 提取链接服务配置的账户密钥(若用密钥认证)
account_key = ls_config["properties"]["typeProperties"]["accountKey"]

# 定义挂载点路径
mount_path = f"/mnt/{container}"
# 先检查是否已挂载,避免重复操作
if not any(mnt.mountPoint == mount_path for mnt in dbutils.fs.mounts()):
    dbutils.fs.mount(
        source=f"wasbs://{container}@{storage_account}.blob.core.windows.net/",
        mount_point=mount_path,
        extra_configs={f"fs.azure.account.key.{storage_account}.blob.core.windows.net": account_key}
    )

要是链接服务用的是MSI或服务主体认证,需要调整extra_configs的参数,比如MSI认证的话,配置项是fs.azure.account.oauth2.client.id等,具体可以根据链接服务的认证类型提取对应属性。

最佳实践

  • 统一链接服务命名:测试、生产环境用相同的链接服务名称(比如ADLS_Main_Storage),不同工作区配置不同的存储账户,代码无需修改。
  • 敏感信息全托管:所有认证密钥、凭证都通过链接服务管理,绝对不要在笔记本里硬编码。
  • 优先用工作区MSI认证:配置链接服务时选择工作区托管身份,不用维护密钥,安全性更高。
  • 封装通用逻辑:把获取链接服务路径、动态挂载的代码封装成通用函数或独立笔记本,其他业务笔记本直接调用,减少重复代码。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 14:02:24