使用UNLOAD命令将AWS Redshift数据导出到S3时输出不正确

时间:2018-04-21 17:38:55

标签: amazon-redshift

我不是数据库人,我刚刚开始探索Redshift。我创建了一个集群并使用他们的示例脚本来填充表。我想将数据从Redshift复制到Postgres。我首先想将数据导出到S3,然后导入Postgres。运行以下命令后,在我的S3存储桶中生成了一个文件:

UNLOAD ('select * from date')
TO 's3://sample-dwh-data/date_' credentials
'aws_access_key_id=******;aws_secret_access_key=*************'
PARALLEL OFF;

这是我用来在Redshift中创建date表的脚本:

create table date(
    dateid smallint not null distkey sortkey,
    caldate date not null,
    day character(3) not null,
    week smallint not null,
    month character(5) not null,
    qtr character(5) not null,
    year smallint not null,
    holiday boolean default('N'));

问题是,当我打开S3中存在的导出文件时,我看到每行的最后一列的值都设置为ft。导出文件的示例输出:

2070|2008-09-01|MO|36|SEP|3|2008|t
2071|2008-09-02|TU|36|SEP|3|2008|f

但是,如果我查询Redshift表,则最后一列值为truefalse。那么,为什么UNLOAD命令会修剪最后一列的值?

更新: @Yankee:当我运行以下命令时: 'SELECT dateid,caldate,day,week,month,qtr,year,CASE WHEN holiday = true THEN 'TRUE' ELSE 'FALSE' END AS holiday from date',我收到了这个错误:

An error occurred when executing the SQL command:
UNLOAD ('SELECT dateid,caldate,day,week,month,qtr,year,CASE WHEN holiday = true THEN 'TRUE' ELSE 'FALSE' END AS holiday from date')
TO 's3://sample-dwh-data/date_...

[Amazon](500310) Invalid operation: syntax error at or near "TRUE" 
Position: 87;

UNLOAD ('SELECT dateid,caldate,day,week,month,qtr,year,CASE WHEN holiday = true THEN 'TRUE' ELSE 'FALSE' END AS holiday from date')
                                                                                      ^                                                                                
1 statement failed.

1 个答案:

答案 0 :(得分:1)

您可以使用CASE IF ELSE条件,而不是执行SELECT * FROM DATE,而是可以指定列名称,并可以自定义数据的导出方式。

SELECT dateid,caldate,day,week,CASE WHEN holiday = true THEN \'TRUE\' ELSE \'FALSE\' END AS holiday from date;