更新时间:2022-12-14 GMT+08:00

MERGE INTO

命令功能

通过MERGE INTO命令,根据一张表或子查询的连接条件对另外一张表进行查询,连接条件匹配上的进行UPDATE或DELETE,无法匹配的执行INSERT。这个语法仅需要一次全表扫描就完成了全部同步工作,执行效率要高于INSERT+UPDATE。

命令格式

MERGE INTO tableIdentifier AS target_alias

USING (sub_query | tableIdentifier) AS source_alias

ON <merge_condition>

[ WHEN MATCHED [ AND <condition> ] THEN <matched_action> ]

[ WHEN MATCHED [ AND <condition> ] THEN <matched_action> ]

[ WHEN NOT MATCHED [ AND <condition> ] THEN <not_matched_action> ]

<merge_condition> =A equal bool condition

<matched_action> =

DELETE |

UPDATE SET * |

UPDATE SET column1 = expression1 [, column2 = expression2 ...]

<not_matched_action> =

INSERT * |

INSERT (column1 [, column2 ...]) VALUES (value1 [, value2 ...])

参数描述

表1 UPDATE参数

参数

描述

tableIdentifier

在其中执行MergeInto操作的Hudi表的名称。

target_alias

目标表的别名。

sub_query

子查询。

source_alias

源表或源表达式的别名。

merge_condition

将源表或表达式和目标表关联起来的条件

condition

过滤条件,可选。

matched_action

当满足条件时进行Delete或Update操作

not_matched_action

当不满足条件时进行Insert操作

注意事项

  1. merge-on condition当前只支持主键列。
  2. 当前仅支持对COW表进行部分字段的更新。参考:
    merge into h0 using s0
    on h0.id = s0.id
    when matched then update set price = s0.price * 2
  3. 当前仅支持对COW表进行更新时,目标表的字段出现在更新表达式的右值。参考:
    merge into h0 using s0
    on h0.id = s0.id
    when matched then update set id = s0.id,
    name = h0.name,
    price = s0.price + h0.price

示例

merge into h0 as target
using (
select id, name, price, flag from s
) source
on target.id = source.id
when matched then update set *
when not matched then insert *;

merge into h0
using (
select id, name, price, flag from s
) source
on h0.id = source.id
when matched and flag != 'delete' then update set id = source.id, name = source.name, price = source.price * 2
when matched and flag = 'delete' then delete
when not matched then insert (id,name,price) values(source.id, source.name, source.price);

merge into t0 as target
using s0 source
on target.id = source.id
when matched then update set *
when not matched then insert *;

系统响应

可在driver日志和客户端中查看命令运行成功或失败。