|NO.Z.00051|——————————|^^ 案例 ^^|——|Hadoop&PB级数仓.V09|——|PB数仓.v09|拉链表实现|拉链表回滚|

一、拉链表的回滚:数据
### --- 06-20拉链表数据(sh xxx.sh 2020-06-20;在2020-06-21日凌晨发出命令):
~~~     # 06-20拉链表数据(sh xxx.sh 2020-06-20;在2020-06-21日凌晨发出命令):

001 13551111111 2020-03-01 2020-06-20 9999-12-31
002 13561111111 2020-04-01 2020-06-20 9999-12-31
003 13571111111 2020-05-01 2020-06-20 9999-12-31
004 13581111111 2020-06-01 2020-06-20 9999-12-31
001 13551111111 2020-03-01 2020-06-20 9999-12-31 2
002 13561111111 2020-04-01 2020-06-20 9999-12-31 2
003 13571111111 2020-05-01 2020-06-20 9999-12-31 2
004 13581111111 2020-06-01 2020-06-20 9999-12-31 2
### --- 06-21拉链表数据(sh xxx.sh 2020-06-21):
~~~     # 06-21拉链表数据(sh xxx.sh 2020-06-21):

001 13551111111 2020-03-01 2020-06-20 9999-12-31
002 13561111111 2020-04-01 2020-06-20 2020-06-20
002 13562222222 2020-04-01 2020-06-21 9999-12-31
003 13571111111 2020-05-01 2020-06-20 9999-12-31
004 13581111111 2020-06-01 2020-06-20 2020-06-20
004 13582222222 2020-06-01 2020-06-21 9999-12-31
005 13552222222 2020-06-21 2020-06-21 9999-12-31
001 13551111111 2020-03-01 2020-06-20 9999-12-31 2
002 13561111111 2020-04-01 2020-06-20 2020-06-20 1
002 13562222222 2020-04-01 2020-06-21 9999-12-31 2
003 13571111111 2020-05-01 2020-06-20 9999-12-31 2
004 13581111111 2020-06-01 2020-06-20 2020-06-20 1
004 13582222222 2020-06-01 2020-06-21 9999-12-31 2
005 13552222222 2020-06-21 2020-06-21 9999-12-31 2
### --- 06-22拉链表数据:

001 13551111111 2020-03-01 2020-06-20 9999-12-31
002 13561111111 2020-04-01 2020-06-20 2020-06-20
002 13562222222 2020-04-01 2020-06-21 9999-12-31
003 13571111111 2020-05-01 2020-06-20 9999-12-31
004 13581111111 2020-06-01 2020-06-20 2020-06-20
004 13582222222 2020-06-01 2020-06-21 2020-06-21
004 13333333333 2020-06-01 2020-06-22 9999-12-31
005 13552222222 2020-06-21 2020-06-21 2020-06-21
005 13533333333 2020-06-21 2020-06-22 9999-12-31
006 13733333333 2020-06-22 2020-06-22 9999-12-31
001 13551111111 2020-03-01 2020-06-20 9999-12-31 2
002 13561111111 2020-04-01 2020-06-20 2020-06-20 1
002 13562222222 2020-04-01 2020-06-21 9999-12-31 2
003 13571111111 2020-05-01 2020-06-20 9999-12-31 2
004 13581111111 2020-06-01 2020-06-20 2020-06-20 1
004 13582222222 2020-06-01 2020-06-21 2020-06-21 1
004 13333333333 2020-06-01 2020-06-22 9999-12-31 2
005 13552222222 2020-06-21 2020-06-21 2020-06-21 1
005 13533333333 2020-06-21 2020-06-22 9999-12-31 2
006 13733333333 2020-06-22 2020-06-22 9999-12-31 2
001 13551111111 2020-03-01 2020-06-20 9999-12-31 2
002 13561111111 2020-04-01 2020-06-20 2020-06-20 1
002 13562222222 2020-04-01 2020-06-21 9999-12-31 2
003 13571111111 2020-05-01 2020-06-20 9999-12-31 2
004 13581111111 2020-06-01 2020-06-20 2020-06-20 1
004 13582222222 2020-06-01 2020-06-21 2020-06-21 1
004 13333333333 2020-06-01 2020-06-22 9999-12-31 2
005 13552222222 2020-06-21 2020-06-21 2020-06-21 1
005 13533333333 2020-06-21 2020-06-22 9999-12-31 2
006 13733333333 2020-06-22 2020-06-22 9999-12-31 2
### --- 06-23拉链表数据:

001 13551111111 2020-03-01 2020-06-20 2020-06-22
001 13554444444 2020-03-01 2020-06-23 9999-12-31
002 13561111111 2020-04-01 2020-06-20 2020-06-20
002 13562222222 2020-04-01 2020-06-21 9999-12-31
003 13571111111 2020-05-01 2020-06-20 2020-06-22
003 13574444444 2020-05-01 2020-06-23 9999-12-31
004 13581111111 2020-06-01 2020-06-20 2020-06-20
004 13582222222 2020-06-01 2020-06-21 2020-06-21
004 13333333333 2020-06-01 2020-06-22 9999-12-31
005 13552222222 2020-06-21 2020-06-21 2020-06-21
005 13533333333 2020-06-21 2020-06-22 2020-06-22
005 13555554444 2020-06-21 2020-06-23 9999-12-31
006 13733333333 2020-06-22 2020-06-22 9999-12-31
007 18600744444 2020-06-23 2020-06-23 9999-12-31
008 18600844444 2020-06-23 2020-06-23 9999-12-31
二、拉链回滚数据说明:拉链回滚数据概述
### --- 由于种种原因需要将拉链表恢复到 rollback_date 那一天的数据。此时有:

~~~     end_date < rollback_date,即结束日期 < 回滚日期。
~~~     表示该行数据在rollback_date 之前产生,这些数据需要原样保留
~~~     start_date <= rollback_date <= end_date,即开始日期 <= 回滚日期 <= 结束日期。
~~~     这些数据是回滚日期之后产生的,但是需要修改。将end_date 改为 9999-12-31
~~~     其他数据不用管
三、操作实例:按以上方案进行编码:
### --- 处理 end_date < rollback_date 的数据,保留

hive (default)> select userid, mobile, regdate, start_date, end_date, 
'1' as tag from test.userhis where end_date < '2020-06-22';
~~~输出参数
userid  mobile  regdate start_date  end_date    tag
002 13561111111 2020-04-01  2020-06-20  2020-06-20  1
004 13581111111 2020-06-01  2020-06-20  2020-06-20  1
004 13582222222 2020-06-01  2020-06-21  2020-06-21  1
005 13552222222 2020-06-21  2020-06-21  2020-06-21  1
### --- 处理 start_date <= rollback_date <= end_date 的数据,设置end_date=9999-12-31

hive (default)> select userid, mobile, regdate, start_date, 
'9999-12-31' as end_date, '2' as tag from test.userhis
where start_date <= '2020-06-22' and end_date >= '2020-06-22';
~~~输出参数
userid  mobile  regdate start_date  end_date    tag
001 13551111111 2020-03-01  2020-06-20  9999-12-31  2
003 13571111111 2020-05-01  2020-06-20  9999-12-31  2
002 13562222222 2020-04-01  2020-06-21  9999-12-31  2
004 13333333333 2020-06-01  2020-06-22  9999-12-31  2
005 13533333333 2020-06-21  2020-06-22  9999-12-31  2
006 13733333333 2020-06-22  2020-06-22  9999-12-31  2
### --- 将前面两步的数据写入临时表tmp(拉链表)

drop table test.tmp;

create table test.tmp as
select userid, mobile, regdate, start_date, end_date, 
'1' as tag from test.userhis where end_date < '2020-06-22' union all
select userid, mobile, regdate, start_date, 
'9999-12-31' as end_date, '2' as tag from test.userhis 
where start_date <= '2020-06-22' 
and end_date >= '2020-06-22';
### --- 查看临时表tmp表中的数据

hive (default)> select * from test.tmp cluster by userid, start_date;
~~~输出参数
tmp.userid  tmp.mobile  tmp.regdate tmp.start_date  tmp.end_date    tmp.tag
001 13551111111 2020-03-01  2020-06-20  9999-12-31  2
002 13561111111 2020-04-01  2020-06-20  2020-06-20  1
002 13562222222 2020-04-01  2020-06-21  9999-12-31  2
003 13571111111 2020-05-01  2020-06-20  9999-12-31  2
004 13581111111 2020-06-01  2020-06-20  2020-06-20  1
004 13582222222 2020-06-01  2020-06-21  2020-06-21  1
004 13333333333 2020-06-01  2020-06-22  9999-12-31  2
005 13552222222 2020-06-21  2020-06-21  2020-06-21  1
005 13533333333 2020-06-21  2020-06-22  9999-12-31  2
006 13733333333 2020-06-22  2020-06-22  9999-12-31  2
四、创建模拟脚本
### --- 模拟脚本
~~~     逐天回滚,检查数据;
~~~     方案二:保存一段时间的增量数据(userinfo),定期对拉链表做备份(如一个月做一次备份);
~~~     如需回滚,直接在备份的拉链表上重跑增量数据。处理简单

[root@hadoop02 ~]# vim /data/yanqidw/data/zippertmp.sh
#!/bin/bash

source /etc/profile

if [ -n "$1" ] ;
then
    do_date=$1
else
    do_date=`date -d "-1 day" +%F`
fi

sql="
drop table test.tmp;
create table test.tmp as
select userid, mobile, regdate, start_date, end_date, '1' as tag
from test.userhis
where end_date < '$do_date'
union all
select userid, mobile, regdate, start_date, '9999-12-31' as
end_date, '2' as tag
from test.userhis
where start_date <= '$do_date' and end_date >= '$do_date';
"

hive -e "$sql"
### --- 回滚到2020-06-20号数据

[root@hadoop02 ~]# sh /data/yanqidw/data/zippertmp.sh 2020-06-20
~~~     # 查看数据是否回退完成

hive (default)> select * from test.tmp cluster by userid, start_date;
tmp.userid  tmp.mobile  tmp.regdate tmp.start_date  tmp.end_date    tmp.tag
001 13551111111 2020-03-01  2020-06-20  9999-12-31  2
002 13561111111 2020-04-01  2020-06-20  9999-12-31  2
003 13571111111 2020-05-01  2020-06-20  9999-12-31  2
004 13581111111 2020-06-01  2020-06-20  9999-12-31  2
~~~     # 回退到2020-06-22号数据

[root@hadoop02 ~]# sh /data/yanqidw/data/zippertmp.sh 2020-06-22
# 查看数据是否回退完成
tmp.userid  tmp.mobile  tmp.regdate tmp.start_date  tmp.end_date    tmp.tag
001 13551111111 2020-03-01  2020-06-20  9999-12-31  2
002 13561111111 2020-04-01  2020-06-20  2020-06-20  1
002 13562222222 2020-04-01  2020-06-21  9999-12-31  2
003 13571111111 2020-05-01  2020-06-20  9999-12-31  2
004 13581111111 2020-06-01  2020-06-20  2020-06-20  1
004 13582222222 2020-06-01  2020-06-21  2020-06-21  1
004 13333333333 2020-06-01  2020-06-22  9999-12-31  2
005 13552222222 2020-06-21  2020-06-21  2020-06-21  1
005 13533333333 2020-06-21  2020-06-22  9999-12-31  2
006 13733333333 2020-06-22  2020-06-22  9999-12-31  2
~~~     # 回退到2020-06-21号数据

hive (default)> select * from test.tmp cluster by userid, start_date;
# 查看数据是否回退完成
tmp.userid  tmp.mobile  tmp.regdate tmp.start_date  tmp.end_date    tmp.tag
001 13551111111 2020-03-01  2020-06-20  9999-12-31  2
002 13561111111 2020-04-01  2020-06-20  2020-06-20  1
002 13562222222 2020-04-01  2020-06-21  9999-12-31  2
003 13571111111 2020-05-01  2020-06-20  9999-12-31  2
004 13581111111 2020-06-01  2020-06-20  2020-06-20  1
004 13582222222 2020-06-01  2020-06-21  9999-12-31  2
005 13552222222 2020-06-21  2020-06-21  9999-12-31  2

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(56)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示