如何将外部位置作为 csv.gz 进行 CTAS



我有近 90 GB 的数据需要上传到具有特定命名约定的 S3 存储桶。

如果我将 CTAS 查询与external_location一起使用,它不会为我提供为文件指定特定名称的选项。此外,对于formatcsv不是一种选择。

CREATE TABLE ctas_csv_partitioned 
WITH (
format = 'TEXTFILE',  
external_location = 's3://my_athena_results/ctas_csv_partitioned/', 
partitioned_by = ARRAY['key1']
) 
AS SELECT name1, address1, comment1, key1
FROM tables1

我想上传输出文件,使其看起来像sample_file.csv.gz

最简单的方法是什么?

不幸的是,仅使用 Athena 无法指定文件名或扩展名。此外,使用 CTAS 查询创建的文件根本不会有任何文件扩展名。但是,您可以使用 S3 的 CLI 直接重命名文件。

aws s3 ls s3://path/to/external/location/ --recursive 
| awk '{cmd="aws s3 mv s3://path/to/external/location/"$4 " s3://path/to/external/location/"$4".csv.gz"; system(cmd)}'

只是尝试了这个片段,一切正常。但是,有时也会创建一个空文件s3://path/to/external/location/.csv.gz注意我没有为aws s3 mv包括--recursive选项,因为它也会产生奇怪的结果。

format字段而言,您只需要在WITH子句中添加field_delimiter=','即可。

CREATE TABLE ctas_csv_partitioned 
WITH (
format = 'TEXTFILE',
field_delimiter=','  
external_location = 's3://my_athena_results/ctas_csv_partitioned/', 
partitioned_by = ARRAY['key1']
) 
AS SELECT name1, address1, comment1, key1
FROM tables1

最新更新