Created
June 8, 2021 22:50
-
-
Save alfallouji/4b999a83193f7f7c5cc2c0f0f84f3022 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Query AWS Athena | |
def query_aws_athena(params): | |
# Creating the Client for Athena | |
client = boto3.client('athena', region_name=params['region']) | |
# This function executes the query and returns the query execution ID | |
response_query_execution_id = client.start_query_execution( | |
QueryString = params['query'], | |
QueryExecutionContext = { | |
'Database' : params['database'] | |
}, | |
ResultConfiguration = { | |
'OutputLocation': 's3://' + params['bucket'] + '/' + params['path'] | |
} | |
) | |
# This function takes query execution id as input and returns the details of the query executed | |
response_get_query_details = client.get_query_execution( | |
QueryExecutionId = response_query_execution_id['QueryExecutionId'] | |
) | |
# Condition for checking the details of response | |
status = 'RUNNING' | |
iterations = 10 | |
while (iterations > 0): | |
iterations = iterations - 1 | |
response_get_query_details = client.get_query_execution(QueryExecutionId = response_query_execution_id['QueryExecutionId']) | |
status = response_get_query_details['QueryExecution']['Status']['State'] | |
print(status) | |
if (status == 'FAILED') or (status == 'CANCELLED') : | |
print(response_get_query_details) | |
return False, False | |
elif status == 'SUCCEEDED': | |
location = response_get_query_details['QueryExecution']['ResultConfiguration']['OutputLocation'] | |
# Function to get output results | |
response_query_result = client.get_query_results( | |
QueryExecutionId = response_query_execution_id['QueryExecutionId'] | |
) | |
result_data = response_query_result['ResultSet'] | |
return location, result_data | |
else: | |
time.sleep(2) | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment