Skip to content

Athena Unload does not specify correct metadata in S3

0

In Athena, when using the unload command to output text files to an s3 location such as:

UNLOAD (SELECT * FROM some_view) TO 's3://results_folder/some_run/' WITH ( format = 'textfile', compression='GZIP' );

The outputted files do not have the correct metadata set.

Expected metadata:

Content-Encoding: gzip

Content-Type: text/plain

Actual metadata:

Content-Type: application/octet-stream

This means that other applications that use this data do not receive it as uncompressed data and may not know how to handle it. For instance, importing those files into postrgres using aws_s3.table_import_from_s3 fails with the original metadata but works correctly when the expected metadata is set.

You can manually set the metadata but that means you have to copy the original file, which kind of defeats the purpose of having the data in the format you need where you need it, which is what unload does.

Can i use some configuration option so the correct metadata is set?

asked 2 years ago549 views

1 Answer
0

Unfortunately, there isn't a direct configuration option in Athena's UNLOAD statement to set specific metadata like Content-Encoding and Content-Type for the output files in S3. The UNLOAD statement focuses primarily on the data output format and compression, but it doesn't provide granular control over the S3 object metadata.

The behavior you're experiencing, where the Content-Type is set to application/octet-stream for GZIP compressed files, is a known limitation of the UNLOAD process in Athena. This occurs because Athena doesn't automatically set the Content-Encoding to 'gzip' or the Content-Type to 'text/plain' for compressed text files.

To work around this limitation, you have a few options:

  1. Post-processing: After the UNLOAD operation, you could use AWS Lambda or another AWS service to update the metadata of the generated files in S3. This would allow you to set the correct Content-Encoding and Content-Type without having to copy the files.

  2. S3 bucket settings: You could configure default metadata settings on your S3 bucket for specific file extensions. However, this might not be ideal if you have various types of files in the same bucket.

  3. Custom ETL process: Instead of using UNLOAD, you could create a custom ETL process using AWS Glue or EMR that reads from Athena and writes to S3 with the correct metadata.

  4. Alternative query output method: Consider using a CREATE TABLE AS SELECT (CTAS) query with the desired output format and compression, then use AWS CLI or SDK to copy the data to your desired location with the correct metadata.

While these workarounds require additional steps, they can help ensure that the files have the correct metadata for downstream processes like importing into PostgreSQL using aws_s3.table_import_from_s3.

It's worth noting that this limitation has been recognized by AWS, and there may be improvements to the UNLOAD functionality in future updates to Athena. However, as of now, there isn't a built-in configuration option to set the specific metadata you need directly through the UNLOAD statement.
Sources
UNLOAD - Amazon Athena
Troubleshoot issues in Athena - Amazon Athena
Find query output files in Amazon S3 - Amazon Athena

answered 2 years ago

EXPERT

reviewed 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.