How to Change the Name of the Athena Results Stored in S3

How to change the name of the Athena results stored in S3?

unfortunately no (at least not yet)! the best way to do this as of now is to write a script to go through all the results of each run and rename (moving+deleting) all the files in that s3 bucket!

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

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

Change output CSV file name of AWS Athena queries

This question is already posted here

client = boto3.client('athena')
s3 = boto3.resource("s3")

# Run query
queryStart = client.start_query_execution(
# PUT_YOUR_QUERY_HERE
QueryString = '''
SELECT *
FROM "db_name"."table_name"
WHERE value > 50
''',
QueryExecutionContext = {
# YOUR_ATHENA_DATABASE_NAME
'Database': "covid_data"
},
ResultConfiguration = {
# query result output location you mentioned in AWS Athena
"OutputLocation": "s3://bucket-name-X/folder-Y/"
}
)

# Executes query and waits 3 seconds
queryId = queryStart['QueryExecutionId']
time.sleep(3)

# Copies newly generated csv file with appropriate name
# query result output location you mentioned in AWS Athena
queryLoc = "bucket-name-X/folder-Y/" + queryId + ".csv"

# Destination location and file name
s3.Object("bucket-name-A", "report-2018.csv").copy_from(CopySource = queryLoc)

# Deletes Athena generated csv and it's metadata file
response = s3.delete_object(
Bucket='bucket-name-A',
Key=queryId+".csv"
)
response = s3.delete_object(
Bucket='bucket-name-A',
Key=queryId+".csv.metadata"
)
print('{file-name} csv generated')

How to specify file name when executing query via Athena API client (Boto3)?

The file name is the Athena query ID. See Identifying Query Output Files.
You can use that to pass on the S3 path or object name to other applications.

Currently its not possible to give a custom name for the query results file.
You could build a custom process around Athena queries to move and rename the S3 objects after the query completes.

AWS Lambda Query Athena and chose name of result file

There isn't a way to select the name output file, the name is formed as <QueryExecutionId>.csv. You can then rename the output file using the s3 api.

Set different output result location for different tables in aws athena

I assume you are talking about the QueryResultLocation in S3:

QueryResultsLocationInS3 is the query result location specified either by workgroup settings or client-side settings.

You can find more detailed information on how to set the location in Specifying a Query Result Location:

The query result location that Athena uses is determined by a combination of workgroup settings and client-side settings. Client-side settings are based on how you run the query.

  • If you run the query using the Athena console, the Query result location entered under Settings in the navigation bar determines the client-side setting.
  • If you run the query using the Athena API, the OutputLocation parameter of the StartQueryExecution action determines the client-side setting.
  • If you use the ODBC or JDBC drivers to run queries, the S3OutputLocation property specified in the connection URL determines the client-side setting.


Related Topics



Leave a reply



Submit