-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoadPartition.py
54 lines (41 loc) · 1.55 KB
/
LoadPartition.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
import json
import boto3
from datetime import datetime
import os
def lambda_handler(event, context):
#get current date and hour
now = datetime.now()
dt = now.strftime("%Y-%m-%d-%H")
# athena client
client = boto3.client('athena')
# athena settings
database = os.environ['DATABASE']
athena_result_bucket = os.environ['ATHENA_QUERY_RESULTS_LOCATION']
SourceTable = os.environ['SOURCE_TABLE']
#database = "sensorDB"
#athena_result_bucket = "s3://sensor-athena-blog/athena_results"
LoadPartionQuery="""
ALTER TABLE %s.%s
ADD IF NOT EXISTS
PARTITION (dt='%s');
""" % (database,SourceTable,dt)
LoadPartitionresponse = client.start_query_execution(
QueryString=LoadPartionQuery,
QueryExecutionContext={
'Database': database
},
ResultConfiguration={
'OutputLocation': athena_result_bucket,
}
)
query_execution_id = LoadPartitionresponse["QueryExecutionId"]
query_status = client.get_query_execution(QueryExecutionId=query_execution_id)
query_execution_status = query_status["QueryExecution"]["Status"]["State"]
while True:
if query_execution_status == 'SUCCEEDED':
print ("Done")
break
if query_execution_status == 'FAILED':
raise Exception("STATUS:" + query_execution_status)
query_status = client.get_query_execution(QueryExecutionId=query_execution_id)
query_execution_status = query_status["QueryExecution"]["Status"]["State"]