優惠券使用表(累積型快照事實表案例)


1.1 ods層的數據(增量數據,將新增和修改的數據導入ods)

sqoop語法是每天將get_time 或者using_time或者used_time為當前分區時間,或者優惠券狀態發生改變的數據導入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 'userid',
`order_id` string COMMENT 'orderid',
`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'
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
location '/warehouse/gmall/ods/ods_coupon_use/';
1.2 分析ods數據
使用到ods表的字段:
coupon_id
user_id
order_id
get_time
using_time
used_time
status

1025優惠券業務數據庫中的數據
1,1001, user01, order_001, 2020-10-25 10:00:00, null, null, 1

1026優惠券業務數據庫中的數據
1,1001, user01, order_001, 2020-10-25 10:00:00, 2020-10-26 00:01:43, 2020-10-26 00:02:00, 1
2,2001, user10, order_10, 2020-10-26 9:01:00, 2020-10-26 11:20:00, null , 1
3,3001, user30, order_30, 2020-10-26 10:00:00, null, null , 1

1027優惠券業務數據庫中的數據
1,1001, user01, order_001, 2020-10-25 10:00:00, 2020-10-26 00:01:43, 2020-10-26 00:02:00 , 0
2,2001, user10, order_10, 2020-10-26 9:01:00, 2020-10-26 11:20:00, 2020-10-27 11:20:00 , 1
3,3001, user30, order_30, 2020-10-26 10:00:00, 2020-10-27 11:20:00, 2020-10-27 13:00:00 , 1
4,4001, user40, order_40, 2020-10-27 08:08:00, null, null, 1
1.3 創建累積型快照事實表
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)
stored as parquet
location '/warehouse/gmall/dwd/dwd_fact_coupon_use/'
tblproperties ("parquet.compression"="lzo");
--dt是按照優惠券領用時間get_time做為分區
1.4 待驗證的方式
--使用動態分區 根據get_time進行動態分區,默認以查詢出來的最后一列為分區字段
set hive.exec.dynamici.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;
insert overwrite table dwd_fact_coupon_use partition (dt)
select 
      if(b.id is not null , b.id, a.id) as id, 
      if(b.coupon_id is not  null, b.coupon_id, a.coupon_id) as coupon_id, 
      if(b.user_id is not null, b.user_id, a.user_id) as user_id,
      if(b.order_id is not null, b.order_id, a.order_id) as order_id,
      if(b.using_time is not null, b.using_time, a.using_time) as using_time,
      if(b.used_time is not null, b.used_time, a.used_time) as used_time,
      if(b.flag is not null, b.flag, a.flag) as flag,
	  if(b.get_time is not null, b.get_time, a.get_time) as get_time,
	 from(
      select 
            id, 
		    coupon_id,
		    user_id, 
		    order_id, 
		    get_time, 
		    using_time, 
		    used_time,
		    flag
           from dwd_fact_coupon_use where dt in (
            select 
                  get_time 
				 from ods_coupon_use 
				 where dt = '2020-10-26')  a 
		   full outer join (
		    select 
                  id,
				  coupon_id,
				  user_id, 
				  order_id, 
				  get_time, 
				  using_time, 
				  used_time,
				  flag
                 from ods_coupon_use where dt ='2020-10-26') b 
		   on a.id = b.id


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM