amazon-s3 - redshift 卸载操作导致冗余数据

标签 amazon-s3 amazon-redshift amazon-redshift-spectrum

我们使用 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);

谢谢。

最佳答案

为防止冗余数据,您必须在 UNLOAD 语句中使用 Redshift 的 CLEANPATH 选项。注意区别,来自 the documentation (也许 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 语句中同时使用 ALLOWOVERWRITECLEANPATH

这是一个例子:

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

关于amazon-s3 - redshift 卸载操作导致冗余数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62818511/

相关文章:

mysql - 与同表的增量数据进行Join

amazon-web-services - AWS : Custom SSL certificate option is disabled in CloudFront, 但我使用 AWS Certificate Manager 创建了一个 SSL 证书

amazon-redshift - NUMERIC(8,4)的Redshift错误溢出

amazon-s3 - "the trustAnchors parameter must be non-empty"是什么意思?

r - 从 R 连接到 Redshift 数据库

binary - 计算 Redshift 列中每个位位置中 '1' 值的数量

amazon-redshift - AWS Redshift - 无法将外部表合并到本地目录

mysql - Redshift 的 PERCENTILE_DISC 的 Presto 等效项

c# - 什么是 C# 中的处置对象?

amazon-web-services - amazon s3 中的 CSV 文件到 amazon SQL Server rds