关于javascript:MaxCompute-实现增量数据推送全量比对增量逻辑

1次阅读

共计 2522 个字符,预计需要花费 7 分钟才能阅读完成。

ODPS 2.0 反对了很多新的汇合命令 (专有云降级到 3 版本后陆续反对),简化了日常工作中求汇合操作的繁琐水平。减少的 SQL 语法包含:UNOIN ALL、UNION DISTINCT 并集,INTERSECT ALL、INTERSECT
DISTINCT 交加,EXCEPT ALL、EXCEPT DISTINCT 补集。
语法格局如下:

select_statement UNION ALL select_statement;
select_statement UNION [DISTINCT] select_statement;
select_statement INTERSECT ALL select_statement;
select_statement INTERSECT [DISTINCT] select_statement;
select_statement EXCEPT ALL select_statement;
select_statement EXCEPT [DISTINCT] select_statement;
select_statement MINUS ALL select_statement;
select_statement MINUS [DISTINCT] select_statement;

用处:别离求两个数据集的并集、交加以及求第二个数据集在第一个数据集中的补集。
参数阐明:
• UNION:求两个数据集的并集,行将两个数据汇合并成一个数据集。
• INTERSECT:求两个数据集的交加。即输入两个数据集均蕴含的记录。
• EXCEPT:求第二个数据集在第一个数据集中的补集。即输入第一个数据集蕴含而第二个数据集不
蕴含的记录。
• MINUS:等同于 EXCEPT。

具体语法参考:
https://help.aliyun.com/document_detail/73782.html?spm=5176.11065259.1996646101.searchclickresult.718d3520fmmOJ0

理论我的项目中有一个利用两日全量数据,比对出增量的需要(推送全量数据速度很慢,ADB/DRDS 等产品数据量超过 1 亿,倡议试用增量同步)。我依照旧的 JOIN 办法和新的汇合办法做了下比对验证,试用了下新的汇合命令 EXCEPT ALL。
测试

-- 办法一:JOIN
-- other_columns 代表很多列
create table tmp_opcode1 as
select * from(
select uuid,other_columns,opcode2
from(
-- 今日新增 + 今日变动
select
 t1.uuid
,t1.other_columns
,case when t2.uuid is null then 'I' else 'U' end AS opcode2
  from            prject1.table1 t1
  left outer join prject1.table1 t2
    on t1.uuid=t2.uuid
   and t2.dt='20200730'
 where t1.dt='20200731'
   and(t2.uuid is null
    or coalesce(t1.other_columns,'')<>coalesce(t2.other_columns,''))

union all
-- 今日删除
select
 t2.uuid
,t2.other_columns
,'D' as opcode2
  from            prject1.table1 t2
  left outer join prject1.table1 t1
    on t1.uuid=t2.uuid
   and t1.dt='20200731'
 where t2.dt='20200730'
   and t1.uuid is null)t3)t4
;
Summary:
resource cost: cpu 13.37 Core * Min, memory 30.48 GB * Min
inputs:
prject1.table1/dt=20200730: 32530802 (946172216 bytes)
prject1.table1/dt=20200731: 32533538 (947161664 bytes)
outputs:
prject1.tmp_opcode1: 4506 (271632 bytes)
Job run time: 26.000
-- 办法二:汇合
-- other_columns 代表很多列
create table  tmp_opcode2 as
select * from(
select t3.*
from(
-- 今日新增 + 今日变动
select uuid,other_columns,'I' as opcode2
from(
select uuid,other_columns
from prject1.table1
where dt = '20200731'

except all
select uuid,other_columns
from prject1.table1
where dt = '20200730')t

union all
-- 今日删除
select
 t2.uuid
,t2.other_columns
,'D' as opcode2
  from            prject1.table1 t2
  left outer join prject1.table1 t1
    on t1.uuid=t2.uuid
   and t1.dt='20200731'
 where t2.dt='20200730'
   and t1.uuid is null)t3)t4
;
Summary:
resource cost: cpu 35.92 Core * Min, memory 74.26 GB * Min
inputs:
prject1.table1/rfq=20200730: 32530802 (946172216 bytes)
prject1.table1/rfq=20200731: 32533538 (947161664 bytes)
outputs:
prject1.tmp_opcode2: 4506 (259416 bytes)
Job run time: 66.000

性能
汇合的办法比 JOIN 的办法,在资源(1 倍)应用和工夫(1 倍)上都有较多的劣势。倡议理论应用 JOIN 办法。
后果
通过多种办法比对验证,两种办法的增量辨认均正确,能够向上游提供增量数据。

原文链接
本文为阿里云原创内容,未经容许不得转载。

正文完
 0