MAPREDUCE服务 MRS-MERGE INTO:示例

时间:2024-07-02 16:39:36

示例

  • 部分字段更新
    create table h0(id int, comb int, name string, price int) using hudi options(primaryKey = 'id', preCombineField = 'comb');
    create table s0(id int, comb int, name string, price int) using hudi options(primaryKey = 'id', preCombineField = 'comb');
    insert into h0 values(1, 1, 1, 1);
    insert into s0 values(1, 1, 1, 1);
    insert into s0 values(2, 2, 2, 2);
    //写法1
    merge into h0 using s0
    on h0.id = s0.id
    when matched then update set h0.id = s0.id, h0.comb = s0.comb, price = s0.price * 2;
    //写法2
    merge into h0 using s0
    on h0.id = s0.id
    when matched then update set id = s0.id,
    name = h0.name,
    comb = s0.comb + h0.comb,
    price = s0.price + h0.price;
  • 缺省字段更新和插入
    create table h0(id int, comb int, name string, price int, flag boolean) using hudi options(primaryKey = 'id', preCombineField = 'comb');
    create table s0(id int, comb int, name string, price int, flag boolean) using hudi options(primaryKey = 'id', preCombineField = 'comb');
    insert into h0 values(1, 1, 1, 1, false);
    insert into s0 values(1, 2, 1, 1, true);
    insert into s0 values(2, 2, 2, 2, false);
    
    merge into h0 as target
    using (
    select id, comb, name, price, flag from s0
    ) source
    on target.id = source.id
    when matched then update set *
    when not matched then insert *;
  • 多条件更新和删除
    create table h0(id int, comb int, name string, price int, flag boolean) using hudi options(primaryKey = 'id', preCombineField = 'comb');
    create table s0(id int, comb int, name string, price int, flag boolean) using hudi options(primaryKey = 'id', preCombineField = 'comb');
    insert into h0 values(1, 1, 1, 1, false);
    insert into h0 values(2, 2, 1, 1, false);
    insert into s0 values(1, 1, 1, 1, true);
    insert into s0 values(2, 2, 2, 2, false);
    insert into s0 values(3, 3, 3, 3, false);
    
    merge into h0
    using (
    select id, comb, name, price, flag from s0
    ) source
    on h0.id = source.id
    when matched and flag = false then update set id = source.id, comb = h0.comb + source.comb, price = source.price * 2
    when matched and flag = true then delete
    when not matched then insert *;
support.huaweicloud.com/cmpntguide-lts-mrs/mrs_01_24274.html