Skip to content

Unload data from redshift with CSV file type to S3

0

I am using the unload query as below, UNLOAD ('SELECT * FROM {table_name} where {where_clause}') TO 's3://{s3_bucket}/api_testing/{table_name}_{filter_filename}' iam_role 'arn:aws:iam::839052322807:role/RedshiftAccessRole' CSV DELIMITER AS ',' ALLOWOVERWRITE PARALLEL OFF This is creating the file in S3 with the file type as customer_ecs_stg_last_pay_date_20240314_site_0080000 and file name as staging.customer_ecs_stg_last_pay_date_20240314_site_0080000. I need te file type to be csv.

1 Answer
0

To ensure that your unloaded files have a .csv extension, you can modify your UNLOAD command by adding the EXTENSION option. Here's how you can adjust your query:

UNLOAD ('SELECT * FROM {table_name} where {where_clause}') TO 's3://{s3_bucket}/api_testing/{table_name}_{filter_filename}' IAM_ROLE 'arn:aws:iam::839052322807:role/RedshiftAccessRole' CSV DELIMITER AS ',' ALLOWOVERWRITE PARALLEL OFF EXTENSION 'csv'

By adding the EXTENSION 'csv' option at the end of your UNLOAD command, Amazon Redshift will append the .csv extension to your unloaded files. This will result in files with names like staging.customer_ecs_stg_last_pay_date_20240314_site_0080000.csv in your S3 bucket.

The EXTENSION option allows you to specify the file extension for the unloaded files, making it easier to identify them as CSV files and work with them in other applications that expect .csv file extensions.

Remember that the EXTENSION option is used to add the specified extension to the file names, not to change the file format itself. Your files are already in CSV format due to the CSV option in your UNLOAD command; this change just ensures the file names reflect that format with the appropriate extension.
Sources
UNLOAD - Amazon Redshift
Unloading data to Amazon S3 - Amazon Redshift

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.