mergeInto

基于源表将一组更新、插入和删除合并到目标表中。

Syntax

mergeInto(table: str, condition: Column)

参数

参数 类型 说明
table str 要合并到的目标表名称。
condition 确定目标表中的行是否与源数据帧中的行匹配的条件。

退货

MergeIntoWriter:MergeIntoWriter,用于进一步指定如何将源数据帧合并到目标表中。

示例

from pyspark.sql.functions import expr
source = spark.createDataFrame(
    [(14, "Tom"), (23, "Alice"), (16, "Bob")], ["id", "name"])
(source.mergeInto("target", "id")
    .whenMatched().update({ "name": source.name })
    .whenNotMatched().insertAll()
    .whenNotMatchedBySource().delete()
    .merge())