在数据仓库的数据模型设计过程中,经常会遇到下面这种表的设计:
优点:
缺点:无历史数据。
优点:
缺点:存储空间占用太大
优点:兼顾了历史数据和存储空间,既能获取历史数据也能筛选最新数据。
缺点:在数据量较大且资源有限的情况下对数据的合并耗时且表的设计有一定的要求(分区)
drop table if exists ods_user_info_inc;
create external table if not exists ods_user_info_inc(
id string comment '主键',
name string comment '用户名',
phone_num string comment '手机号码',
create_time string comment '创建日期',
operate_time string comment '修改日期'
)
PARTITIONED BY (dt STRING)
ROW FORMAT delimited fields terminated by ','
LOCATION '/tmp/hive_test/ods/ods_user_info_inc/';
drop table if exists dim_user_info_zip;
create external table if not exists dim_user_info_zip(
id string comment '主键',
name string comment '用户名',
phone_num string comment '手机号码',
create_time string comment '创建日期',
operate_time string comment '修改日期',
start_time string comment '开始时间',
end_time string comment '结束时间'
)
PARTITIONED BY (dt STRING)
stored as orc
LOCATION '/tmp/hive_test/dim/dim_user_info_zip/'
TBLPROPERTIES ('orc.compress' = 'snappy');
insert overwrite table dim_user_info_zip partition (dt='9999-12-31')
select
id ,
name ,
phone_num ,
create_time,
operate_time,
"2022-01-01" as start_time,
"9999-12-31" as end_time
from ods_user_info_inc
where dt='2022-01-01';
with tmp as (
select
old.id as old_id,
old.name as old_name,
old.phone_num as phone_num ,
old.create_time as old_create_time,
old.operate_time as old_operate_time,
old.start_time as old_start_time,
old.end_time as old_end_time,
new.id as new_id,
new.name as new_name,
new.phone_num as phone_num ,
new.create_time as new_create_time,
new.operate_time as new_operate_time,
new.start_time as new_start_time,
new.end_time as new_end_time
from
(select
id ,
name ,
phone_num ,
create_time,
operate_time,
start_time,
end_time
from dim_user_info_zip where dt='9999-12-31') old
full join
(select
id ,
name ,
phone_num ,
create_time,
operate_time,
'2022-01-02' as start_time,
'9999-12-31' as end_time
from ods_user_info_inc where dt='2022-01-02') new
on old.id = new.id
)
insert overwrite table dim_user_info_zip partition (dt)
select
if(new_id is not null,new_id,old_id),
if(new_id is not null,new_name,old_name),
if(new_id is not null,new_name,phone_num ),
if(new_id is not null,new_create_time,old_create_time),
if(new_id is not null,new_operate_time,old_operate_time),
if(new_id is not null,new_start_time,old_start_time),
if(new_id is not null,new_end_time,old_end_time),
if(new_id is not null,new_end_time,old_end_time) dt
from tmp
union all
select
old_id,
old_name,
old.phone_num ,
old_create_time,
old_operate_time,
old_start_time,
cast(date_sub('2022-01-02',1) as string) as old_end_time,
cast(date_sub('2022-01-02',1) as string) as dt
from tmp
where old_id is not null and new_id is not null;
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/181340.html原文链接:https://javaforall.cn