zoukankan      html  css  js  c++  java
  • 拉链表

    1.数据准备

    create table sospdm.tmp_ods_user
    (
         cust_num   string comment '会员编码'
        ,mbl_phone  string comment '会员手机号'
    )partitioned by (statis_date string comment '统计时间')
    stored as rcfile 
    ;
    
    set hive.exec.dynamic.partition=true;   
    set hive.exec.dynamic.partition.mode=nonstrict;
    insert overwrite table sospdm.tmp_ods_user partition (statis_date)
    select '001' as cust_num,'111111' as mbl_phone,'20170101' as statis_date from sospdm.dual union all
    select '002' as cust_num,'222222' as mbl_phone,'20170101' as statis_date from sospdm.dual union all
    select '003' as cust_num,'333333' as mbl_phone,'20170101' as statis_date from sospdm.dual union all
    select '004' as cust_num,'444444' as mbl_phone,'20170101' as statis_date from sospdm.dual union all
    
    select '001' as cust_num,'111111' as mbl_phone,'20170102' as statis_date from sospdm.dual union all
    select '002' as cust_num,'233333' as mbl_phone,'20170102' as statis_date from sospdm.dual union all
    select '003' as cust_num,'333333' as mbl_phone,'20170102' as statis_date from sospdm.dual union all
    select '004' as cust_num,'432432' as mbl_phone,'20170102' as statis_date from sospdm.dual union all
    select '005' as cust_num,'555555' as mbl_phone,'20170102' as statis_date from sospdm.dual union all
    
    select '001' as cust_num,'111111' as mbl_phone,'20170103' as statis_date from sospdm.dual union all
    select '002' as cust_num,'233333' as mbl_phone,'20170103' as statis_date from sospdm.dual union all
    select '003' as cust_num,'333333' as mbl_phone,'20170103' as statis_date from sospdm.dual union all
    select '004' as cust_num,'654321' as mbl_phone,'20170103' as statis_date from sospdm.dual union all
    select '005' as cust_num,'115115' as mbl_phone,'20170103' as statis_date from sospdm.dual union all
    select '006' as cust_num,'666666' as mbl_phone,'20170103' as statis_date from sospdm.dual union all
    
    select '001' as cust_num,'111111' as mbl_phone,'20170104' as statis_date from sospdm.dual union all
    select '002' as cust_num,'233333' as mbl_phone,'20170104' as statis_date from sospdm.dual union all
    select '003' as cust_num,'333333' as mbl_phone,'20170104' as statis_date from sospdm.dual union all
    select '004' as cust_num,'654321' as mbl_phone,'20170104' as statis_date from sospdm.dual union all
    select '005' as cust_num,'115115' as mbl_phone,'20170104' as statis_date from sospdm.dual union all
    select '006' as cust_num,'666767' as mbl_phone,'20170104' as statis_date from sospdm.dual;

    2.拉链逻辑

    -- 全量拉链
    -- 初始化拉链表
    create table sospdm.tmp_dwd_his
    (
         cust_num string comment '会员编码'
        ,mbl_phone string comment '手机号'
        ,start_date string comment '开始时间'
    )partitioned by (end_date string comment '结束时间')
    ;
    
    insert overwrite table sospdm.tmp_dwd_his partition(end_date)
    select
         cust_num
        ,mbl_phone
        ,'20170101' as statis_date
        ,'99991231' as end_date
    from tmp_ods_user where statis_date='20170101'
    ;
    
    -- 取开链数据与今日全量数据对比 找出变化的
    
    -- 字段改变或者不变的
    insert overwrite table sospdm.tmp_dwd_his partition(end_date)
    select 
         t1.cust_num
        ,t1.mbl_phone
        ,'${statis_date}' as start_date
        ,'99991231' as end_date
    from
    (
        select
             cust_num
            ,mbl_phone
        from tmp_ods_user where statis_date='${statis_date}'
    ) t1
    left join 
    (
        select
             cust_num
            ,mbl_phone
        from tmp_dwd_his where start_date<='${statis_date}' and end_date>='${statis_date}'
    ) t2 
    on t1.cust_num=t2.cust_num
    where t2.cust_num is null -- 新增
    or t1.mbl_phone <> t2.mbl_phone -- 改变
    
    union all 
    
    select 
         t1.cust_num
        ,t1.mbl_phone
        ,start_date
        ,'${statis_date}' as end_date   -- 改变的进行闭链
    from
    (
        select
             cust_num
            ,mbl_phone
        from tmp_ods_user where statis_date='${statis_date}'
    ) t1 
    inner join 
    (
        select
             cust_num
            ,mbl_phone
            ,start_date
            ,end_date
        from tmp_dwd_his where start_date<='${statis_date}' and end_date>='${statis_date}'
    ) t2 
    on t1.cust_num=t2.cust_num  
    where t1.mbl_phone <> t2.mbl_phone-- 改变
    
    union all 
    
    select 
         t1.cust_num
        ,t1.mbl_phone
        ,start_date
        ,end_date
    from
    (
        select
             cust_num
            ,mbl_phone
        from tmp_ods_user where statis_date='${statis_date}'
    ) t1 
    inner join 
    (
        select
             cust_num
            ,mbl_phone
            ,start_date
            ,end_date
        from tmp_dwd_his where start_date<='${statis_date}' and end_date>='${statis_date}'
    ) t2 
    on t1.cust_num=t2.cust_num and t1.mbl_phone = t2.mbl_phone -- 不变
    ;

    3.拉链回滚

    -- 回滚
    -- 回滚日期之前的闭链 还是闭链
    
    select 
         cust_num
        ,mbl_phone
        ,start_date
        ,end_date
    from sospdm.tmp_dwd_his where end_date < '20170103'
    --回滚日期之前的开链
    union all 
    select 
         cust_num
        ,mbl_phone
        ,start_date
        ,'99991231' as end_date
    from sospdm.tmp_dwd_his where end_date >= '20170103'
    ;
  • 相关阅读:
    不要为自己找借口,你可以做到的--职场实用做人法则
    sql server 利用发布订阅方式实现数据库同步问题
    关于免费空间的寻找
    数据自定义格式化
    C++字符串string类常用操作详解(一)【初始化、遍历、连接】
    C++ 命名空间
    gcc/g++ 如何支持c11 / c++11标准编译
    正确的C++/C堆栈
    linux下清空c++ cin无效流的方式
    32位64位下各种数据类型大小的对比
  • 原文地址:https://www.cnblogs.com/yin-fei/p/10840986.html
Copyright © 2011-2022 走看看