Skip to main content

mergeInto

Merges a set of updates, insertions, and deletions based on a source table into a target table.

Syntax

mergeInto(table: str, condition: Column)

Parameters

Parameter

Type

Description

table

str

Target table name to merge into.

condition

Column

The condition that determines whether a row in the target table matches one in the source DataFrame.

Returns

MergeIntoWriter: MergeIntoWriter to use further to specify how to merge the source DataFrame into the target table.

Examples

Python
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())