zoukankan      html  css  js  c++  java
  • 关系型数据库的三种离线同步方式

    本文主要介绍关系型数据库的三种同步方式,并提供相关代码。这三种方案在我的实际工作中都有用到,欢迎在评论区交流!

    增量同步

    表设计

    表名:ods_{数据源缩写}_{原始表名},以订单为例:ods_ord_order
    表性质:每日全量分区表
    生命周期:保留近30天的历史数据

    抽取增量

    常规情况下,我们按照更新时间或者创建时间来抽取增量,如果是第一次初始化抽取,则应该是抽取全量。

    日志表和更新表的第一次初始化抽取

    select *
           , now() etl_time
    from   t_ord_order -- 原始表
    

    更新表常规抽取

    若数据源是更新表,则按照数据的更新时间来抽取。

    select *
           , now() as etl_time
    from   t_ord_order -- 原始表
    where  更新时间 >= DATE_SUB(curdate(),INTERVAL -1 DAY)
    ;
    

    日志表常规抽取

    若数据源是日志表,则按照数据的创建时间来抽取。

    select *
           , now() as etl_time
    from   t_ord_order -- 原始表
    where  创建时间 >= DATE_SUB(curdate(),INTERVAL -1 DAY)
    ;
    

    合并

    insert overwrite  ods_ord_order partition(ds = replace(days_sub(now(),1),'-',''))
    select id 
           , order_id
    	   ...
           , elt_time
    from (
          select *
                 , row_number() over(partition by id order by 更新时间/创建时间 desc, etl_time desc) as level
          from (
                 select *
                 from   ods_ord_order -- 数据仓库表
                 where  ds = replace(days_sub(now(),1),'-','') -- t-1的增量
                 union 
                 select *
                 from    ods_ord_order -- 数据仓库表
                 where  ds = replace(days_sub(now(),2),'-','') -- t-2的全量
          ) tmp
    ) tmp 
    where level = 1 -- 取最新的记录
    ;
    

    流程图

    增量同步方案

    全量同步

    增量合并全量

  • 相关阅读:
    loj6145. 「2017 山东三轮集训 Day7」Easy
    CF1019E Raining season
    CF1261F Xor-Set
    Python笔试——递归算法学习
    Python笔试——贪心算法
    Python笔试——万万没想到抓捕孔连顺
    Python笔试——雀魂启动
    Python学习——正则表达式
    Python笔试——毕业旅行问题
    Python笔试——Stern-Brocot tree
  • 原文地址:https://www.cnblogs.com/2sheep2simple/p/13423686.html
Copyright © 2011-2022 走看看