使用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
相关产品推荐
相关产品推荐

