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

Azure Synapse PySpark写入Blob时HTTP必填头缺失400错误解决

解决ADLS Gen2中Synapse写Delta Lake报HTTP头缺失的问题

核心原因及修复步骤

1. 修正存储路径端点(最常见触发错误的原因)

启用分层命名空间的Blob存储属于ADLS Gen2,必须使用dfs.core.windows.net端点,而非blob.core.windows.net。原代码中路径用了blob端点,会导致协议层面的请求头缺失错误。

修正路径格式:

# 读取和写入路径都改为dfs端点
file_path =f'abfss://config@businessblobtut.dfs.core.windows.net/config_excel/{file_name}'
save_path =f'abfss://config@businessblobtut.dfs.core.windows.net/config_delta/ADF_CONFIG_TABLES'

2. 配置正确的ADLS Gen2认证

在Synapse笔记本开头添加认证配置,确保Spark有权限访问存储:

  • 如果使用Synapse托管标识:
spark.conf.set("fs.azure.account.auth.type.businessblobtut.dfs.core.windows.net", "OAuth")
spark.conf.set("fs.azure.account.oauth.provider.type.businessblobtut.dfs.core.windows.net", "org.apache.hadoop.fs.azurebfs.oauth2.MsiTokenProvider")
spark.conf.set("fs.azure.account.oauth2.client.id.businessblobtut.dfs.core.windows.net", "<你的Synapse托管标识ID>")
spark.conf.set("fs.azure.account.oauth2.client.endpoint.businessblobtut.dfs.core.windows.net", "https://login.microsoftonline.com/<你的租户ID>/oauth2/token")
  • 如果使用服务主体:
spark.conf.set("fs.azure.account.auth.type.businessblobtut.dfs.core.windows.net", "OAuth")
spark.conf.set("fs.azure.account.oauth.provider.type.businessblobtut.dfs.core.windows.net", "org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider")
spark.conf.set("fs.azure.account.oauth2.client.id.businessblobtut.dfs.core.windows.net", "<服务主体ID>")
spark.conf.set("fs.azure.account.oauth2.client.secret.businessblobtut.dfs.core.windows.net", "<服务主体密钥>")
spark.conf.set("fs.azure.account.oauth2.client.endpoint.businessblobtut.dfs.core.windows.net", "https://login.microsoftonline.com/<租户ID>/oauth2/token")

3. 确认权限配置

确保访问存储的身份(托管标识/服务主体)对目标容器config拥有Storage Blob Data Contributor权限——普通的Blob Contributor权限不适用于ADLS Gen2的文件系统操作。

4. 完整修正后的代码示例

# 先配置ADLS Gen2认证
spark.conf.set("fs.azure.account.auth.type.businessblobtut.dfs.core.windows.net", "OAuth")
spark.conf.set("fs.azure.account.oauth.provider.type.businessblobtut.dfs.core.windows.net", "org.apache.hadoop.fs.azurebfs.oauth2.MsiTokenProvider")
spark.conf.set("fs.azure.account.oauth2.client.id.businessblobtut.dfs.core.windows.net", "<你的Synapse托管标识ID>")
spark.conf.set("fs.azure.account.oauth2.client.endpoint.businessblobtut.dfs.core.windows.net", "https://login.microsoftonline.com/<你的租户ID>/oauth2/token")

file_name = 'config_transport_excel.xlsx'
file_path =f'abfss://config@businessblobtut.dfs.core.windows.net/config_excel/{file_name}'
save_path =f'abfss://config@businessblobtut.dfs.core.windows.net/config_delta/ADF_CONFIG_TABLES'

pandas_df = pd.read_excel(file_path)
delta_table = spark.createDataFrame(pandas_df)
delta_table.write.format('delta').mode('append').save(save_path)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 05:16:11