arj
arj

Reputation: 983

How to change output CSV file name of AWS Athena select query

I have an Athena select query and the result will be saved in an s3 bucket location. Above scenario is working fine, but the file name will be a bunch of random characters. I need to save the result as a specific file name file name should be report.csv

My query executed from shell script.

aws athena start-query-execution \
    --query-string "select  user_id,case file_type from <table name> group by file_type,user_id" \
    --work-group "primary" \
    --query-execution-context Database=<database name>\
    --result-configuration "OutputLocation=s3://<bucket name>/report.csv"

Current output will be like this enter image description here

Does have any simple way to set the file name

Upvotes: 0

Views: 1459

Answers (1)

Theo
Theo

Reputation: 132862

You cannot change the name of the results. However, you can make a copy of the file once the query has finished:

aws s3 cp s3://<output_bucket>/9411<…>.csv s3://<other_bucket>/report.csv

aws athena start-query-execution only starts the query, it doesn't wait for the query to finish. You can either poll the status of the query with aws athena get-query-execution, or wait for the result file to appear on S3 with aws s3 wait object-exists.

If you want a shell script that runs a query, waits for it to finish, and handles error cases, see https://gist.github.com/iconara/447a569d00a7a9c4aff86e9e0b14ff11

Upvotes: 2

Related Questions