ETL 过程中数据库中的重复记录问题

Issue with duplicate records in database during ETL process

我目前的ETL架构如下:

s3 --> staging table --> python dataframe --> destination table
  1. 来自 s3 的记录被加载到暂存中 table
  2. Python 脚本已连接到暂存 table
  3. Python脚本是运行每隔一小时做一些复杂的 转换
  4. 来自python的结果数据帧被上传到目的地 table

但是,我在目标 table:

中遇到重复记录的问题
| Time | New records (S3) | Redshift staging table (postgre) | Python DataFrame | Redshift Destination Table (postgre) | Duplicate records |
|------|------------------|----------------------------------|------------------|--------------------------------------|-------------------|
| 9am  | 3 records        | 3 records                        | 3 records        | 3 records                            | 0 (3-3)           |
| 10am | 2 records        | 5 (3+2) records                  | 5 records        | 8 (3+5) records                      | 3 (8-5)           |
| 11am | 4 records        | 9 (5+4) records                  | 9 records        | 17 (9+8) records                     | 8 (17-9)          |

所以在上午 11 点,分段 table 有 9 条记录,但目的地 table 有 17 条记录(上午 11 点在目的地 table 共有 8 条重复记录)

如何确保目标 table 中的总记录与暂存 table

中存在的记录相匹配

(我无法消除暂存 table。现在,我正在将目标 table 过滤到 select 只有唯一记录。有没有更好的方法来做到这一点?)

您的阶段和目标 table 都在 Postgres 中,因此只需编写逻辑来比较阶段 table 中的数据与目标 table 中的数据并从阶段中删除任何记录已经存在于目的地 table。

DELETE FROM staging
WHERE EXISTS(SELECT 1 FROM dest WHERE dest.id = staging.id);

https://www.postgresql.org/docs/8.1/static/functions-subquery.html