我目前的ETL架构如下:
s3 --> staging table --> python dataframe --> destination 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时,分期表有9项记录,而目标表则有17项记录(在上午11点,目的地表中共有8项重复记录)。
如何确保目标表中的总记录与暂存表中的记录匹配?
(我不能删除分期表。现在,我正在过滤目标表,只选择唯一的记录。有更好的方法吗?)
发布于 2018-05-10 15:26:43
您的stage表和目标表都在Postgres中,所以只需编写逻辑,将stage表中的数据与dest表中的数据进行比较,并删除已经存在于dest表中的任何记录。
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
https://stackoverflow.com/questions/50276248
复制相似问题