generated from palewire/python-open-source-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathathena.py
254 lines (208 loc) · 6.7 KB
/
athena.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
"""Utilities for working with Amazon Athena."""
from __future__ import annotations
import io
import os
import time
import boto3
import pandas as pd
def create_database(
database_name: str,
verbose: bool = False,
) -> str:
"""Create Amazon Athena database.
Args:
database_name : str
name of the database
verbose : bool
whether to print verbose output
Returns:
str : query execution id
Example:
>>> create_database("my_database", verbose=True)
"""
if verbose:
print(f"Creating {database_name} if it doesn't exist")
return query(f"CREATE DATABASE IF NOT EXISTS {database_name}", verbose=verbose)
def drop_database(
database_name: str,
verbose: bool = False,
) -> str:
"""Drop Amazon Athena database.
Args:
database_name : str
name of the database
verbose : bool
whether to print verbose output
Returns:
str : query execution id
Example:
>>> drop_database("my_database", verbose=True)
"""
if verbose:
print(f"Dropping {database_name} if it exists")
return query(f"DROP DATABASE IF EXISTS {database_name}", verbose=verbose)
def create_table(
database_name: str,
table_name: str,
field_list: list[list[str, str]],
location: str,
verbose: bool = False,
) -> str:
"""Create Amazon Athena table.
Args:
database_name : str
name of the database
table_name : str
name of the table
field_list : list[list[str, str]]
list of field names and types. e.g. [["id", "INT"], ["name", "STRING"]].
Reference for field types available at https://docs.aws.amazon.com/athena/latest/ug/data-types.html
location : str
s3 location of the data inside AWS S3 bucket. e.g. 's3://my-bucket/my-folder/' would be /my-folder/
verbose : bool
whether to print verbose output
Returns:
str : query execution id
Example:
>>> create_table(
... "my_database",
... "my_table",
... [["id", "INT"], ["name", "STRING"]],
... "/my-folder/",
... verbose=True,
... )
"""
# Create the SQL statement
sql = f"CREATE EXTERNAL TABLE IF NOT EXISTS {database_name}.{table_name} (\n"
for field in field_list:
sql += f" {field[0]} {field[1]},\n"
sql = sql[:-2] + "\n)\n"
sql += "ROW FORMAT DELIMITED\n"
sql += "FIELDS TERMINATED BY ','\n"
sql += "STORED AS TEXTFILE\n"
sql += f"LOCATION 's3://{os.getenv('AWS_S3_BUCKET_NAME')}{location}'\n"
sql += "TBLPROPERTIES ('skip.header.line.count'='1')"
# Run the query
if verbose:
print(f"Creating Athena table: {database_name}.{table_name}")
return query(sql, verbose=verbose)
def drop_table(
database_name: str,
table_name: str,
verbose: bool = False,
) -> str:
"""Drop Amazon Athena table.
Args:
database_name : str
name of the database
table_name : str
name of the table
verbose : bool
whether to print verbose output
Returns:
str : query execution id
Example:
>>> drop_table("my_database", "my_table", verbose=True)
"""
# Drop the table if it exists
if verbose:
print(f"Dropping {database_name}.{table_name} if it exists")
return query(f"DROP TABLE IF EXISTS {database_name}.{table_name}", verbose=verbose)
def get_dataframe(
sql: str,
verbose: bool = False,
**kwargs,
) -> pd.DataFrame:
"""Get pandas DataFrame from Amazon Athena query.
Args:
sql : str
formatted string containing athena sql query
verbose : bool
whether to print verbose output
**kwargs
additional keyword arguments to pass to the dataframe
Returns:
pd.DataFrame : pandas DataFrame containing query results
Example:
>>> sql = "SELECT COUNT(*) FROM my_database.my_table"
>>> df = get_dataframe(sql, verbose=True)
>>> print(df.head())
"""
# Run the query
job_id = query(sql, verbose=verbose)
# Connect to Amazon S3
client = boto3.client(
"s3",
aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"),
aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"),
region_name=os.getenv("AWS_REGION_NAME"),
)
# Download the file created by our query
response = client.get_object(
Bucket=os.getenv("AWS_S3_BUCKET_NAME"),
Key=f"query-output/{job_id}.csv",
)
# Convert it to the file object
file_obj = io.BytesIO(response["Body"].read())
# Read the file into a pandas DataFrame
if kwargs is None:
kwargs = {}
df = pd.read_csv(file_obj, **kwargs)
# Return the DataFrame
return df
def query(
sql: str,
wait: int = 10,
verbose: bool = False,
) -> str:
"""Execute SQL query on Amazon Athena.
Args:
sql : str
formatted string containing athena sql query
wait : int
number of seconds to wait between efforts to check the query status
verbose : bool
whether to print verbose output
Returns:
str : query execution id
Example:
>>> query("SELECT COUNT(*) FROM my_database.my_table", verbose=True)
"""
# Create the Athena client
client = boto3.client("athena", region_name=os.getenv("AWS_REGION_NAME"))
# Set the destination as our temporary S3 workspace folder
s3_destination = f"s3://{os.getenv('AWS_S3_BUCKET_NAME')}/query-output/"
# Execute the query
if verbose:
print(f"Running query: {sql}")
request = client.start_query_execution(
QueryString=sql,
ResultConfiguration={
"OutputLocation": s3_destination,
},
)
# Get the query execution id
query_id = request["QueryExecutionId"]
if verbose:
print(f"Query ID: {query_id}")
# Wait for the query to finish
retry_count = 0
while True:
# Get the query execution state
response = client.get_query_execution(QueryExecutionId=query_id)
state = response["QueryExecution"]["Status"]["State"]
# If it's still running, wait a little longer
if state in ["RUNNING", "QUEUED"]:
if verbose:
print(f"Query state: {state}. Waiting {wait} seconds...")
time.sleep(wait)
retry_count += 1
# If it failed, raise an exception
else:
break
# Make sure it finished successfully
if verbose:
print(f"Query finished with state: {state}")
assert state == "SUCCEEDED", f"query state is {state}"
# Return the query id
return query_id