· 8 years ago · Jul 12, 2018, 04:34 AM
1# We can separate create Athena table, but for saving time . I added them in this script.
2 sql_query = """
3 CREATE DATABASE IF NOT EXISTS
4 """ + database_name
5 response = client_athena.start_query_execution(
6 QueryString=sql_query,
7 ResultConfiguration={
8 'OutputLocation': s3_result_path
9 }
10 )
11 # Create table
12 sql_query = """
13 CREATE EXTERNAL TABLE IF NOT EXISTS """ + database_name + "." + table_name + """ (
14 `From` STRING,
15 `Date` STRING,
16 `Subject` STRING,
17 `To` STRING
18 )
19 ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
20 WITH SERDEPROPERTIES (
21 'serialization.format' = '1'
22 )
23 LOCATION '""" + s3_data_path + """';
24 """
25
26 response = client_athena.start_query_execution(
27 QueryString=sql_query,
28 ResultConfiguration={
29 'OutputLocation': s3_result_path
30 }
31 )
32 # Wait for query execution finish.
33 time.sleep(3)
34 # Get query result
35 query_result = client_athena.get_query_execution(
36 QueryExecutionId=response['QueryExecutionId']
37 )
38 if query_result['QueryExecution']['Status']['State'] == 'FAILED':
39 raise Exception(query_result['QueryExecution']['Status']['StateChangeReason'])
40 else:
41 print("Create Athena DB Succesfully")