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

使用DeltaTable执行Upsert时报错:DeltaMergeBuilder无WhenNotMatchedInsert属性

解决DeltaTable Upsert操作的AttributeError错误

错误根源

你的代码存在两处大小写错误,导致了AttributeError:

  • Delta Lake的DeltaMergeBuilder类中,处理未匹配行插入的方法是小写开头的whenNotMatchedInsert,而非你写的大写开头WhenNotMatchedInsert
  • 插入操作的参数需要用小写的values,不是大写的Values

修正后的完整代码

from delta.tables import *
delta_df = DeltaTable.forPath(spark,"dbfs:/user/hive/warehouse/FileStore/tables/stream_write2")

delta_df.alias("t").merge(
    df.alias("s"),
    "t.empid = s.empid"  # 建议直接使用定义的别名t和s,避免歧义
).whenMatchedUpdate(
    set = {
        "name": "s.name",
        "city": "s.city",
        "country": "s.country",
        "contactno": "s.contactno"
    }
).whenNotMatchedInsert(
    values = {
        "empid": "s.empid",
        "name": "s.name",
        "city": "s.city",
        "country": "s.country",
        "contactno": "s.contactno"
    }
).execute()

补充提示

Delta Lake的Python API严格遵循PEP8命名规范,所有方法名均为小写开头的驼峰格式(如whenMatchedUpdate),不要使用大写开头的写法。另外合并条件里用你定义的别名(t和s)对应,代码可读性会更高。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 22:31:57