Skip to content

Commit 8d76041

Browse files
committed
Now s3_output is optional on Athena.run_query().
1 parent 8994531 commit 8d76041

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

awswrangler/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
__title__ = "awswrangler"
22
__description__ = "Utility belt to handle data on AWS."
3-
__version__ = "0.0b28"
3+
__version__ = "0.0b29"
44
__license__ = "Apache License 2.0"

awswrangler/athena.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,31 @@ def get_query_dtype(self, query_execution_id):
5151
logger.debug(f"parse_dates: {parse_dates}")
5252
return dtype, parse_dates
5353

54-
def run_query(self, query, database, s3_output):
54+
def create_athena_bucket(self):
55+
"""
56+
Creates the default Athena bucket if not exists
57+
:return: Bucket s3 path (E.g. s3://aws-athena-query-results-ACCOUNT-REGION/)
58+
"""
59+
account_id = (self._session.boto3_session.client(
60+
service_name="sts",
61+
config=self._session.botocore_config).get_caller_identity().get(
62+
"Account"))
63+
session_region = self._session.boto3_session.region_name
64+
s3_output = f"s3://aws-athena-query-results-{account_id}-{session_region}/"
65+
s3_resource = self._session.boto3_session.resource("s3")
66+
s3_resource.Bucket(s3_output)
67+
return s3_output
68+
69+
def run_query(self, query, database, s3_output=None):
70+
"""
71+
Run a SQL Query against AWS Athena
72+
:param query: SQL query
73+
:param database: AWS Glue/Athena database name
74+
:param s3_output: AWS S3 path
75+
:return: Query execution ID
76+
"""
77+
if not s3_output:
78+
s3_output = self.create_athena_bucket()
5579
response = self._client_athena.start_query_execution(
5680
QueryString=query,
5781
QueryExecutionContext={"Database": database},

awswrangler/pandas.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -398,13 +398,7 @@ def read_sql_athena(self,
398398
:return: Pandas Dataframe or Iterator of Pandas Dataframes if max_result_size != None
399399
"""
400400
if not s3_output:
401-
account_id = (self._session.boto3_session.client(
402-
service_name="sts", config=self._session.botocore_config).
403-
get_caller_identity().get("Account"))
404-
session_region = self._session.boto3_session.region_name
405-
s3_output = f"s3://aws-athena-query-results-{account_id}-{session_region}/"
406-
s3_resource = self._session.boto3_session.resource("s3")
407-
s3_resource.Bucket(s3_output)
401+
s3_output = self._session.athena.create_athena_bucket()
408402
query_execution_id = self._session.athena.run_query(
409403
query=sql, database=database, s3_output=s3_output)
410404
query_response = self._session.athena.wait_query(

0 commit comments

Comments
 (0)