数仓之数据重刷机制
先抛出几个问题
-
存储是不是基石?
-
假如存储不挂,数据真的准确吗?
-
存储挂了,数据还准确吗?
-
如何校验是否正确?如何让其正确?机制是不是必须有?
注:sqoop抽数据,无error丢数据的概率很小
数据质量校验:数据量校验 count相同吗?count相同内容相同吗?
数据量相同–>数据量不同 重刷机制 补or删 spark 95%–>数据内容不同? 抽样 5%
模拟数据:
上游表a1
ID NAME AGE
1.00 pxj1 21.00
2.00 pxj2 22.00
3.00 pxj3 23.00
下游表:b1
ID NAME AGE
1.00 pxj1 21.00
2.00 pxj2 22.00
4.00 pxj4 24.00
数据重刷机制:用count校验上下游的数据不准确
引入重刷机制:通过对上下游的两个表求full outer join来对比字段的null值
select * from a1
full join b1
on a1.id=b1.id
ID NAME AGE ID NAME AGE
1.00 pxj1 21.00 1.00 pxj1 21.00
2.00 pxj2 22.00 2.00 pxj2 22.00
4.00 pxj4 24.00
3.00 pxj3 23.00
已a1表为基准:
select b1.id from a1
left join b1
on a1.id=b1.id
where a1.id is null
delete from b1 where id in (
select b1.id from a1
left join b1
on a1.id=b1.id
where a1.id is null
)
insert into b1 value(
select a1.id ,a1.name,a1.age from B1
right join a1
on a1.id=b1.id
where b1.id is null
)
B1表数据
ID NAME AGE
1.00 pxj1 21.00
2.00 pxj2 22.00
3.00 pxj3 23.00
深度思考:
full outer join 其实就是先 left join 和后 right join 的两个结果,为 null 的刚好是缺少的或者多的,而交集是上下游都有的数据,需要做的是 left join 为 null 做 insert 或者 delete,还是 right join 为 null 做 insert 或者 delete。