unload导出数据

UNLOAD命令用于将Redshift的数据导出到S3。例如:

unload ('select * from venue')
to 's3://mybucket/unload/' 
iam_role 'arn:aws:iam::0123456789012:role/MyRedshiftRole';

如果想导出成csv:

unload ('select * from venue')
to 's3://mybucket/unload/' 
iam_role 'arn:aws:iam::0123456789012:role/MyRedshiftRole'
CSV;

导出电影查询结果

继续使用上一节的sql查询语句,将其导出到S3。在执行unload时,使用$$转义

unload ($$select
  title.title,
  title.language,
  title_basics.primarytitle,
  title_basics.startyear,
  title_basics.genres,
  principles.nconst,
  name_basics.primaryname,
  name_basics.primaryprofession
from imdb.title
  join imdb.title_basics on title.titleid = title_basics.tconst
  join spectrum_schema.principles on title.titleid = principles.tconst
  join imdb.name_basics on name_basics.nconst = principles.nconst
where title.language = 'en'
and title_basics.startyear is not null
order by title_basics.startyear desc
$$)
to 's3://redshift-kpf/unload/' 
iam_role 'arn:aws:iam::145197526627:role/redshift-access-s3';

query editor中执行unload命令:

image-20221031213532976

执行完成后,在S3对应目录下看到结果文件:

image-20221031213504327

下载文件,查看里面的数据:

image-20221031213453654


参考: https://docs.aws.amazon.com/redshift/latest/dg/r_UNLOAD_command_examples.html