首页 > 解决方案 > redshift 卸载操作导致冗余数据

问题描述

我们使用 UNLOAD 命令在基于 s3 的外部表上运行一些转换,并将数据以 PARQUET 格式发布到不同的 s3 存储桶中。

如果文件已经存在,我在卸载操作中使用 ALLOWOVERWRITE 选项来替换文件。这在大多数情况下都可以正常工作,但有时会为相同的数据插入重复文件,这会导致外部表显示重复的数字。

例如,如果分区中的 parquet 是 0000_part_00.parquet,其中包含完整的数据。在下一次运行中,卸载预计会覆盖此文件,而是插入新文件 0000_part_01.parquet,使总输出加倍。

如果我只是清理整个分区并重新运行,这将不再重复。这种不一致使我们的系统变得不可靠。

unload (<simple select statement>)
to 's3://<s3 bucket>/<prefix>/'
iam_role '<iam-role>' allowoverwrite
PARQUET
PARTITION BY (partition_col1, partition_col2);

谢谢你。

标签: amazon-s3amazon-redshiftamazon-redshift-spectrum

解决方案


为防止出现冗余数据,您必须在语句中使用 Redshift 的CLEANPATH选项。请注意文档UNLOAD中的区别(也许 AWS 可以更清楚一点):

ALLOWOVERWRITE
By default, UNLOAD fails if it finds files that it would possibly overwrite. If ALLOWOVERWRITE is specified, UNLOAD overwrites existing files, including the manifest file.

CLEANPATH
The CLEANPATH option removes existing files located in the Amazon S3 path specified in the TO clause before unloading files to the specified location.
If you include the PARTITION BY clause, existing files are removed only from the partition folders to receive new files generated by the UNLOAD operation.
You must have the s3:DeleteObject permission on the Amazon S3 bucket. For information, see Policies and Permissions in Amazon S3 in the Amazon Simple Storage Service Console User Guide. Files that you remove by using the `CLEANPATH` option are permanently deleted and can't be recovered.
You can't specify the `CLEANPATH` option if you specify the `ALLOWOVERWRITE` option.

因此,正如@Vzzarr所说,ALLOWOVERWRITE仅覆盖与传入文件名同名的文件。对于不需要过去数据的状态保持不变的重复卸载操作,您必须使用CLEANPATH.

请注意,您不能在同一个 UNLOAD 语句中同时使用ALLOWOVERWRITE和。CLEANPATH

这是一个例子:

f"""
UNLOAD ('{your_query}')
TO 's3://{destination_prefix}/'
iam_role '{IAM_ROLE_ARN}'
PARQUET
MAXFILESIZE 4 GB
MANIFEST verbose
CLEANPATH
"""

推荐阅读