@@ -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 },
0 commit comments