是否有方法为S3路径传递current_date to copy命令
例如:从AWS Redshift 中的's3/rootlocation/_current_date_/*.txt复制表名
John是正确的,不可能动态构建COPY
语句。然而,我找到了一种解决这个问题的方法,只使用SQL,只需再使用几个命令:
create temporary table _path as
select (
'{"entries":[{"url":"s3://bucket/customer' ||
getdate()::date ||
'.txt", "mandatory":true}]}'
)::varchar(255)
;
unload ('select * from _path') to 's3://bucket/customer.manifest'
credentials '' parallel off
;
copy customer from 's3://bucket/customer.manifest000' credentials '' manifest;
请参阅http://docs.aws.amazon.com/redshift/latest/dg/r_UNLOAD.html和http://docs.aws.amazon.com/redshift/latest/dg/loading-data-files-using-manifest.html.
我的实验表明,FROM参数需要是单个字符串,而不是计算值。因此,不可能这样做:
copy customer
from 's3://mybucket/customer' + CURRENT_DATE
credentials '<aws-auth-args>';
在将字符串发送到Redshift之前,您需要通过用于触发COPY
命令的任何系统来计算字符串。
另一种类似的方法是使用包含要加载的文件列表的清单文件。这在许多文件存储在给定目录中并且只需要加载一些文件的情况下非常有用。它还避免了负载中包含哪些文件的混淆。
如果有人遇到这个愚蠢的问题,我就是这样解决的:
create or replace procedure test(
p_date date
)
language plpgsql
as $$
declare
v_path varchar(255);
v_copy_command varchar(max);
begin
select 's3://bucket/path/date_stamp='||p_date into v_path;
raise notice 'Path is %', v_path;
select 'COPY table FROM ''' ||
v_path ||
''' REGION ''us-east-1'' IAM_ROLE ''arn:aws:iam::123456789:role/service-role/AmazonRedshift-CommandsAccessRole'' FORMAT as parquet;'
into v_copy_command;
raise notice 'Copy command: %', v_copy_command;
execute v_copy_command;
end;
$$