数据仓库Hive在简书数据仓库DW

数据仓库中如何做增量处理

2017-05-08  本文已影响126人  FxData
  1. 为什么做增量
    数据量大,只需要增量最新被更改的数据。

  2. 如何做增量
    (1)insert into
    比如行为数据,发生一条记录就插入一条,数据不会被update。
    严格T+1,初始化时候限定created_at的时间。否则凌晨之后的数据会被重复插入。
    (2)insert overwrite
    初始化的时候不限定时间。
    增量的时候where to_date(a.updated_at) >= '%s'
    (选择updated_at 做为增量字段,接近凌晨创建的数据2016-12-20 23:59:58的更新时间是T日,此数据不会被增量进,接近严格T+1 。以updated_at做为初始化时候时间的限制字段,会将T日凌晨之后更新创建于几日前的数据剔除,此为bug。)

增量的方法步骤如下:
1)建表
use default;
create table if not exists loan_f_order_info
(
ord_no string comment "订单号",
uid string comment "用户ID",
crt_tim timestamp comment "创建时间",
upd_tim timestamp comment "更新时间"
)
COMMENT "test表";

2)初始化
insert overwrite table loan_f_order_info
select order_no as ord_no,
uid as uid,
created_at as crt_tim,
updated_at as upd_tim
from ods_loan.ods_loan_dsloan_loan_stage_order

3)增量
a)insert into table default.loan_f_order_info
select order_no as ord_no,
uid as uid,
created_at as crt_tim,
updated_at as upd_tim
from ods_loan.ods_loan_dsloan_loan_stage_order
where to_date(updated_at) >= date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1)

b)insert overwrite table default.loan_f_order_info
select ord_no,
uid,
crt_tim,
upd_tim
from (
select *,
row_number() over(partition by uid, ord_no order by upd_tim desc) as row_num
from default.loan_f_order_info
) za
where row_num = '1'

上一篇下一篇

猜你喜欢

热点阅读