pyspark - 仅更新更改的行 pyspark delta table databricks
问题描述
与创建的数据框相比,需要仅更新现有表中更改的行。所以现在,我确实减去并获取更改的行,但不确定如何合并到现有表中。
old_df = spark.sql("select * from existing table")
diff = new_df.subtract(old_df)
现在必须插入差异数据框(如果是新行)或更新现有记录
(deltaTable.alias("full_df").merge(
merge_df.alias("append_df"),
"full_df.col1 = append_df.col1 OR full_df.col2 =append_df.col2")
.whenNotMatchedInsertAll()
.execute()
)
这不会更新现有记录(案例:col2 值已更改; col1 未更改)
解决方案
.whenMatchedUpdateAll()
接受可用于保留未更改行的条件:
(deltaTable.alias("full_df").merge(
merge_df.alias("append_df"),
"full_df.col1 = append_df.col1 OR full_df.col2 = append_df.col2")
.whenNotMatchedInsertAll()
.whenMatchedUpdateAll("full_df.col1 != append_df.col1 OR full_df.col2 != append_df.col2")
.execute()
)
推荐阅读
- php - 在 Laravel 中将数据从 mySQL 显示到视图时出错
- python - 在文本中搜索多个短语 python
- wordpress - Wordpress JWT 身份验证令牌问题
- events - 如何在 Nuxt.js 中的图标链接上正确实现悬停效果
- python-3.x - 我有一个 .CSV 文件,其中包含这些日期的日期和 gms 值。是否可以对此应用线性回归?
- python - 替换部分字符串 Python
- dart - 列中的定位小部件导致卡片溢出
- python - 如果需要多个标准输入,python asyncio 会死锁
- php - 仅比较数组的值并获得差异
- laravel - Laravel Notification 重复错误,通过(数据库,电子邮件)发送多个通知