Pat*_*son 7 merge apache-spark pyspark azure-databricks
我正在尝试与 Databricks 进行合并,但收到错误:
UnsupportedOperationException: Cannot perform Merge as multiple source rows matched and attempted to modify the same
target row in the Delta table in possibly conflicting ways.
Run Code Online (Sandbox Code Playgroud)
我在 SO 上多次看到这个问题,并且我知道如果源数据集的多行匹配并且合并尝试更新目标 Delta 表的相同行,则合并操作可能会失败。
简而言之,当源表有多行尝试更新同一目标行时,就会发生这种情况。如果更新表包含具有相同 ID 的记录,则可能会发生这种情况。就我而言,我认为这不适用。
有人可以看一下我的代码,如果他们能发现任何明显的东西,请告诉我
(deltadf.alias("t")
.merge(
df.alias("s"),
"s.primary_key_hash = t.primary_key_hash")
.whenMatchedUpdateAll("s.change_key_hash <> t.change_key_hash")
.whenNotMatchedInsertAll()
.execute()
)
Run Code Online (Sandbox Code Playgroud)
样本 deltadf
样本 df
请原谅这些图像。我正在努力使用标记语言添加数据
我尝试重现此问题,以下是增量表数据和输入数据帧数据。
hash_key输入数据框中存在重复项。当我尝试对这些增量表和源数据帧使用合并时,它会抛出错误。
deltadf.alias("t").merge(df.alias("s"),"s.hash_key = t.hash_key")\
.whenMatchedUpdateAll("s.change_key <> t.change_key")\
.whenNotMatchedInsertAll()\
.execute()
Run Code Online (Sandbox Code Playgroud)
错误:
java.lang.UnsupportedOperationException: Cannot perform Merge as multiple source rows matched and
attempted to modify the same target row in the Delta table in possibly conflicting ways. By SQL
semantics of Merge, when multiple source rows match on the same target row, the result may be
ambiguous as it is unclear which source row should be used to update or delete the matching target
row. You can preprocess the source table to eliminate the possibility of multiple matches.
Run Code Online (Sandbox Code Playgroud)
我们可以在错误中看到,我们需要对源进行预处理以消除多个匹配的可能性,就像这里的情况一样。因此,请尝试检查数据框中的重复值df并使用 删除它们df.dropDuplicates('primary_key_hash')。由于您使用 进行匹配primary_key_hash,请确保数据帧的此列中没有重复值。
当我第一次执行合并时,没有任何问题。但是,当我第二次执行合并而不更新输入数据时,出现错误。您认为这就是我遇到问题的原因吗?
不,如果合并操作第一次成功执行,并且您在第二次执行时也使用相同的输入,那么它将运行而不会出现任何错误,因为primary_key_hash将被匹配,并且change_key_hash这次将在没有任何插入的情况下更新(因为新的primary_key_hash是当不匹配时插入到先前的运行中)。
| 归档时间: |
|
| 查看次数: |
24801 次 |
| 最近记录: |