9.5 拉鍊表製作過程
9.5.1 步驟0:初始化拉鍊表(首次獨立執行)
1)生成10條原始訂單數據
CALL init_data(‘2019-02-13’,10,5,10,TRUE);
[atguigu@hadoop102 bin]$ sqoop_import.sh all 2019-02-13
[atguigu@hadoop102 bin]$ ods_db.sh 2019-02-13
[atguigu@hadoop102 bin]$ dwd_db.sh 2019-02-13
2)建立拉鍊表
hive (gmall)>
drop table if exists dwd_order_info_his;
create table dwd_order_info_his(
id
string COMMENT ‘訂單編號’,
total_amount
decimal(10,2) COMMENT ‘訂單金額’,
order_status
string COMMENT ‘訂單狀態’,
user_id
string COMMENT ‘用戶id’ ,
payment_way
string COMMENT ‘支付方式’,
out_trade_no
string COMMENT ‘支付流水號’,
create_time
string COMMENT ‘創建時間’,
operate_time
string COMMENT ‘操作時間’,
start_date
string COMMENT ‘有效開始日期’,
end_date
string COMMENT ‘有效結束日期’
) COMMENT ‘訂單拉鍊表’
stored as parquet
location ‘/warehouse/gmall/dwd/dwd_order_info_his/’
tblproperties (“parquet.compression”=“snappy”);
3)初始化拉鍊表
hive (gmall)>
insert overwrite table dwd_order_info_his
select
id,
total_amount,
order_status,
user_id,
payment_way,
out_trade_no,
create_time,
operate_time,
‘2019-02-13’,
‘9999-99-99’
from ods_order_info oi
where oi.dt=‘2019-02-13’;
4)查詢拉鍊表中數據
hive (gmall)>select * from dwd_order_info_his limit 2;
9.5.2步驟1:製作當日變動數據(包括新增,修改)每日執行
1)如何獲得每日變動表
(1)最好表內有創建時間和變動時間(Lucky!)
(2)如果沒有,可以利用第三方工具監控比如canal,監控MySQL的實時變化進行記錄(麻煩)。
(3)逐行對比前後兩天的數據, 檢查md5(concat(全部有可能變化的字段))是否相同(low)
(4)要求業務數據庫提供變動流水(人品,顏值)
2)因爲dwd_order_info本身導入過來就是新增變動明細的表,所以不用處理
(1)2019-02-14日新增2條訂單數據
CALL init_data(‘2019-02-14’,2,5,10,TRUE);
(2)通過Sqoop把2019-02-14日所有數據導入
sqoop_import.sh all 2019-02-14
(3)ODS層數據導入
ods_db.sh 2019-02-14
(4)DWD層數據導入
dwd_db.sh 2019-02-14
9.5.3 步驟2:先合併變動信息,再追加新增信息,插入到臨時表中
1)建立臨時表
hive (gmall)>
drop table if exists dwd_order_info_his_tmp;
create external table dwd_order_info_his_tmp(
id
string COMMENT ‘訂單編號’,
total_amount
decimal(10,2) COMMENT ‘訂單金額’,
order_status
string COMMENT ‘訂單狀態’,
user_id
string COMMENT ‘用戶id’ ,
payment_way
string COMMENT ‘支付方式’,
out_trade_no
string COMMENT ‘支付流水號’,
create_time
string COMMENT ‘創建時間’,
operate_time
string COMMENT ‘操作時間’,
start_date
string COMMENT ‘有效開始日期’,
end_date
string COMMENT ‘有效結束日期’
) COMMENT ‘訂單拉鍊臨時表’
stored as parquet
location ‘/warehouse/gmall/dwd/dwd_order_info_his_tmp/’
tblproperties (“parquet.compression”=“snappy”);
2)導入腳本
hive (gmall)>
insert overwrite table dwd_order_info_his_tmp
select * from
(
select
id,
total_amount,
order_status,
user_id,
payment_way,
out_trade_no,
create_time,
operate_time,
‘2019-02-14’ start_date,
‘9999-99-99’ end_date
from dwd_order_info where dt=‘2019-02-14’
union all
select oh.id,
oh.total_amount,
oh.order_status,
oh.user_id,
oh.payment_way,
oh.out_trade_no,
oh.create_time,
oh.operate_time,
oh.start_date,
if(oi.id is null,oh.end_date, date_add(oi.dt,-1)) end_date
from dwd_order_info_his oh left join
(
select
*
from dwd_order_info
where dt=‘2019-02-14’
) oi
on oh.id=oi.id and oh.end_date=‘9999-99-99’
)his
order by his.id,start_date;
9.5.4步驟3:把臨時表覆蓋給拉鍊表
1)導入數據
hive (gmall)>
insert overwrite table dwd_order_info_his
select * from dwd_order_info_his_tmp;
2)查詢導入數據
hive (gmall)>select * from dwd_order_info_his;
9.5.5 整理爲每日腳本