forked from raoofnaushad/AWSathena_s3_python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
athena_from_s3.py
60 lines (48 loc) · 1.94 KB
/
athena_from_s3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import boto3
import pandas
import csv
import time
def query_results(session, params):
## Creating the Client for Athena
client = boto3.client('athena')
## 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']
)
print(response_get_query_details)
# time.sleep(1)
## Condition for checking the details of response
status = 'RUNNING'
iterations = 5
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') :
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']
print("location: ", location)
print("data: ", result_data)
return location, result_data
else:
time.sleep(1)
return False