从Azure Synapse PySpark Notebook写入Delta表到ADLS Gen2报错求助
问题:Azure Synapse PySpark写入ADLS Gen2 Delta表时触发400错误
错误信息
Py4JJavaError: An error occurred while calling o3898.save. : Operation failed: "An HTTP header that's mandatory for this request is not specified.", 400, PUT,
执行代码
初始无Delta表,执行else分支,df_eventLog加载无报错:
if (DeltaTable.isDeltaTable(spark, source_path)): print('Existing delta table') # Read the existing Delta Table delta_table = DeltaTable.forPath(spark, source_path) # Merge new data into existing table delta_table.alias("existing").merge( source = df_eventLog.alias("updates"), condition = " AND ".join(conditions_list) ).whenMatchedUpdateAll( ).whenNotMatchedInsertAll( ).execute() else: print('New delta table') # Create new delta table with new data df_eventLog.write.format('delta').save(source_path)
排查与解决方法
校验ADLS Gen2路径格式与权限
- 确保
source_path使用ADLS Gen2的DFS端点格式:abfss://<容器名>@<存储账户名>.dfs.core.windows.net/<路径>,禁止使用旧的wasbs://格式,Delta Lake依赖DFS端点特性完成写入。 - 确认Synapse工作区的托管身份(或服务主体)对目标ADLS Gen2容器/路径拥有Storage Blob Data Contributor(或更高)权限,权限缺失或身份配置错误会导致请求缺少必要验证头,触发400错误。
- 确保
检查Spark会话的ADLS身份验证配置
- 若使用服务主体访问ADLS,需在Notebook开头添加以下配置(替换占位符):
spark.conf.set("fs.azure.account.auth.type.<存储账户名>.dfs.core.windows.net", "OAuth") spark.conf.set("fs.azure.account.oauth.provider.type.<存储账户名>.dfs.core.windows.net", "org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider") spark.conf.set("fs.azure.account.oauth2.client.id.<存储账户名>.dfs.core.windows.net", "<客户端ID>") spark.conf.set("fs.azure.account.oauth2.client.secret.<存储账户名>.dfs.core.windows.net", "<客户端密钥>") spark.conf.set("fs.azure.account.oauth2.client.endpoint.<存储账户名>.dfs.core.windows.net", "https://login.microsoftonline.com/<租户ID>/oauth2/token") - 若使用托管身份,配置应为:
spark.conf.set("fs.azure.account.auth.type.<存储账户名>.dfs.core.windows.net", "OAuth") spark.conf.set("fs.azure.account.oauth.provider.type.<存储账户名>.dfs.core.windows.net", "org.apache.hadoop.fs.azurebfs.oauth2.MsiTokenProvider")
- 若使用服务主体访问ADLS,需在Notebook开头添加以下配置(替换占位符):
确认Delta Lake版本兼容性
- 不要手动安装第三方Delta Lake包,使用Synapse Spark池内置的Delta版本,第三方包可能与Synapse的ADLS集成逻辑不兼容,引发协议错误。
检查路径拼写准确性
- 核对
source_path中的存储账户名(必须小写)、容器名、子路径的大小写与拼写是否完全正确,ADLS Gen2的容器和路径区分大小写,拼写错误会导致请求无法定位资源,触发400错误。
- 核对
内容的提问来源于stack exchange,提问作者practicalGuy
相关产品推荐
相关产品推荐

