首页 > 解决方案 > 在 hive 中实现 SCD1 的最佳方法

问题描述

我有一个主表(约 100 毫米记录),需要使用每天处理的每日增量更新/插入。

delta 的典型每日交易量为几十万条记录。这可以使用allfull join或窗口函数来实现row_number+union

但我的问题是,这两种方法中哪一种是使用 Hive 的更好方法(它在 Tez 上运行,版本是 2.1)。我们希望更新 master 中的所有字段以获取 delta 发生变化的记录,因此希望row_number+union全部使用并寻找一些优化策略。

标签: hivehiveql

解决方案


我认为一般的经验法则是 - 避免完全连接 - 总是。

我不知道您如何使用带有联合的窗口功能,但根据我的经验,以下工作非常好(假设连接匹配 1:1):

完全加入

select 
    coalesce(x.a, y.a) as a,
    coalesce(x.b, y.b) as b,
    coalesce(x.c, y.c) as c,
    coalesce(x.d, y.d) as d,
    x.xe,
    y.ye
from
    x
full outer join y 
    on x.a=y.a and x.b=y.b

联盟

select 
    a,
    b,
    coalesce(max(x_c), max(y_c)) as c,
    coalesce(max(x_d), max(y_d)) as d,
    max(xe) as xe,
    max(ye) as ye
from
    (select a,b,c as x_c,d as x_d, null as y_c, null as y_d, xe, null as ye from x
    union all 
     select a,b,null as x_c, null as x_d, c as y_c, d as y_d, null as xe, ye from y) main
group by 
    a,b

推荐阅读