amazon-s3 - 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);
谢谢你。
解决方案
为防止出现冗余数据,您必须在语句中使用 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
"""
推荐阅读
- go - 如何使用适用于 Windows 的 GitLab 管道进行构建
- c# - 基于 Specflow 目录中的测试 json 文件创建的不必要的 .feature.cs 文件
- java - 如何设置多语言文本转语音
- forms - 使用表单发布值并显示前端
- python-3.x - 读取 csv 文件时出错,逗号分隔文件大小 65MB
- scala - 使用返回 Future 的二元运算折叠序列
- java - PDF 保存在 Oracle DB 中:错误 - java.sql.SQLException:数据大小大于此类型的最大大小:71365
- node.js - Type Error: Cannot read property 'hasListCollectionsCommand' of null
- php - 在 laravel 选择查询中获取附加数据
- python - 是否有来自 mysql 的任何 python 代码允许我选择第一行观察结果?