项目---累积型快照事实表sql

本文详细介绍了如何构建和操作累积型快照事实表,包括优惠券表和订单事实表的实例。涉及ODS和DWD层的设计,使用动态分区、全外连接和日期格式化函数。关键步骤包括数据处理、SQL语句分析,以及在Hive中使用concat、concat_ws、group_concat和str_to_map等函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

 

例一:优惠券表

ODS

DWD

 思路 

SQL

用到的知识点

1.动态分区参数:

2.Where语句

3.dt是一个ods层设置的分区

4.老表没有的数据用新表的,新表没有的用老表的,老表新表都有的用新表的。

6.full outer join  ...on    全外连接

7.DATE_FORMAT(date,format)函数

例二:订单事实表(累积型快照事实表)

ODS

订单表(增量及更新)

订单状态表(增量)

活动订单关联表(增量)

DWD

订单事实表

分析:

1.dwd中用到了三张表,分别是ods_order_info,ods_order_status_log,ods_activity_order;

2.ods_order_status_log需要做一些处理,将他的order_status ,operate_time进行更细致的划分,取出其中的数据,方便动态分区等sql语句调用;

3.根据创建时间分组,其他支付时间,退款时间等之后会变化,所以使用动态分区;

4.full outer join 全连接,new表有的用new表,old表有的用old表,两个表都有的用new表更新的;

5.关键sql语句分析: 将ods_order_status_log 表中的 order_status 和operate_time里的创建时间,支付时间,取消时间,完成时间,退款时间,退款完成时间对应的1001等数字与所对应的时间弄成一个按创建id分组的大表的sql语句,方便分区时调用

6.为什么要加on?

7.为什么用where?

sql实现

用到的hivesql函数:


例一:优惠券表

ODS

新增及变化 --》 每天分区里面存放的是新增的与变化的数据 

drop table if exists ods_coupon_use;
create external table ods_coupon_use(
    `id` string COMMENT '编号',
    `coupon_id` string  COMMENT '优惠券ID',
    `user_id` string  COMMENT 'skuid',
    `order_id` string  COMMENT 'spuid',
    `coupon_status` string  COMMENT '优惠券状态',
    `get_time` string  COMMENT '领取时间',
    `using_time` string  COMMENT '使用时间(下单)',
    `used_time` string  COMMENT '使用时间(支付)'
) COMMENT '优惠券领用表'
PARTITIONED BY (`dt` string)
row format delimited fields terminated by '\t'

DWD

累积型的事实表 

drop table if exists dwd_fact_coupon_use;
create external table dwd_fact_coupon_use(
    `id` string COMMENT '编号',
    `coupon_id` string  COMMENT '优惠券ID',
    `user_id` string  COMMENT 'userid',
    `order_id` string  COMMENT '订单id',
    `coupon_status` string  COMMENT '优惠券状态',
    `get_time` string  COMMENT '领取时间',
    `using_time` string  COMMENT '使用时间(下单)',
    `used_time` string  COMMENT '使用时间(支付)'
) COMMENT '优惠券领用事实表'
PARTITIONED BY (`dt` string)
row format delimited fields terminated by '\t'         //行格式分隔符“/t”
location '/warehouse/gmall/dwd/dwd_fact_coupon_use/';

两个表进行join
new(新增与变化) 和 old(这个事实表)
new有 old  有  ---》 需要更新
new 有 old 没有 --》  插入到当天的分区
new 没有 old 有  --》 保留 


 思路 

1.get_time:之前时间发生,以他为分区,因为这两个useing_time和used_time发生时间不确定,所以使用动态分区。

useing_time:使用优惠券的时间不确定。

used_time:使用优惠券支付的时间不确定。

2.新表有的,用新表的,新表没有的用旧表的 ,旧表有的,用旧表的,旧表没有的用新表的,新表旧表都有的用新表的;

3.new(新增与变化) 和 old(事实表)

SQL

set hive.exec.dynamic.partition.mode=nonstrict;
insert overwrite table dwd_fact_coupon_use partition(dt)
select
    if(new.id is null,old.id,new.id),
    if(new.coupon_id is null,old.coupon_id,new.coupon_id),
    if(new.user_id is null,old.user_id,new.user_id),
    if(new.order_id is null,old.order_id,new.order_id),
    if(new.coupon_status is null,old.coupon_status,new.coupon_status),
    if(new.get_time is null,old.get_time,new.get_time),
    if(new.using_time is null,old.using_time,new.using_time),
    if(new.used_time is null,old.used_time,new.used_time),	
    date_format(if(new.get_time is null,old.get_time,new.get_time),'yyyy-MM-dd')  // 获取到动态分区
from
(
    select
        id,
        coupon_id,
        user_id,
        order_id,
        coupon_status,
        get_time,
        using_time,
        used_time
    from dwd_fact_coupon_use
    where dt in
    (
        select
            date_format(get_time,'yyyy-MM-dd')
        from ods_coupon_use
        where dt='2020-10-30'
    )
)old
full outer join
(
    select
        id,
        coupon_id,
        user_id,
        order_id,
        coupon_status,
        get_time,
        using_time,
        used_time
    from ods_coupon_use
    where dt='2020-10-30'
)new
on old.id=new.id;

用到的知识点

1.动态分区参数:

https://blog.youkuaiyun.com/qq_16590169/article/details/103464349

2.Where语句

  •     使用WHERE子句,将不满足条件的行过滤掉
  •     WHERE子句紧随FROM子句
  •     案例实操 查询出薪水大于1000的所有员工

            hive (default)> select * from emp where sal >1000;

    注意:where子句中不能使用字段别名。


3.dt是一个ods层设置的分区

4.老表没有的数据用新表的,新表没有的用老表的,老表新表都有的用新表的。

6.full outer join  ...on    全外连接

FULL OUTER JOIN 关键字只要左表(table1)和右表(table2)其中一个表中存在匹配,则返回行.

FULL OUTER JOIN 关键字结合了 LEFT JOIN 和 RIGHT JOIN 的结果。

7.DATE_FORMAT(date,format)函数

date 参数是合法的日期,format 规定日期/时间的输出格式。
DATE_FORMAT(NOW(),'%m-%d-%Y')            --mysql

例二:订单事实表(累积型快照事实表)

ODS

订单表(增量及更新)

hive (gmall)>
drop table if exists ods_order_info;
create extern
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值