airflow-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] ashb commented on a change in pull request #4111: [AIRFLOW-3266] Add AWS Athena Operator and hook
Date Mon, 29 Oct 2018 14:18:25 GMT
ashb commented on a change in pull request #4111: [AIRFLOW-3266] Add AWS Athena Operator and
hook
URL: https://github.com/apache/incubator-airflow/pull/4111#discussion_r228943414
 
 

 ##########
 File path: airflow/contrib/hooks/aws_athena_hook.py
 ##########
 @@ -0,0 +1,147 @@
+# -*- coding: utf-8 -*-
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from time import sleep
+from airflow.contrib.hooks.aws_hook import AwsHook
+from uuid import uuid4
+
+INTERMEDIATE_STATES = ('QUEUED', 'RUNNING',)
+FINAL_STATES = ('SUCCEEDED', 'FAILED', 'CANCELLED',)
+
+
+class AWSAthenaHook(AwsHook):
+
+    def __init__(self, query, database, region_name, output_location, aws_conn_id=None, *args,
**kwargs):
+        self.query = query
+        self.database = database
+        self.region_name = region_name
+        self.outputLocation = output_location
+        self.aws_conn_id = aws_conn_id
+        if 'client_request_token' in kwargs:  # This ensures idempotency of query execution
+            self.client_request_token = kwargs['client_request_token']
+        else:
+            self.client_request_token = str(uuid4())
+        if 'query_execution_context' in kwargs:
+            self.query_execution_context = kwargs['query_execution_context']
+        else:
+            self.query_execution_context = {}
+        if 'result_configuration' in kwargs:
+            self.result_configuration = kwargs['result_configuration']
+        else:
+            self.result_configuration = {}
+        if 'sleep_time' in kwargs:
+            self.sleep_time = kwargs['sleep_time']
+        else:
+            self.sleep_time = 5  # Use this to poll query status in athena
+        self.query_execution_context['Database'] = self.database
+        self.result_configuration['OutputLocation'] = self.outputLocation
+        super(AWSAthenaHook, self).__init__(self.aws_conn_id, verify=None)
+        self.conn = self.get_conn()
+
+    def _check_query_status(self):
+        if self._queryExecutionId is None:
+            return None
+        return self.conn.get_query_execution(QueryExecutionId=self._queryExecutionId)
+
+    def _get_query_results(self):
+        if self._queryExecutionId is None:
+            return None
+        state = self.check_query_status()
+        if state is None or state in INTERMEDIATE_STATES:
+            self.log.error("Invalid Query state")
+            return None
+        elif state in INTERMEDIATE_STATES or state == 'CANCELLED':
+            self.log.info("Query is in {state} state. Cannot fetch results".format(state=state))
+            return None
+        return self.conn.get_query_results(QueryExecutionId=self._queryExecutionId)
+
+    def _stop_query(self):
+        if self._queryExecutionId is None:
+            return None
+        return self.conn.stop_query_execution(QueryExecutionId=self._queryExecutionId)
+
+    def _poll_query_status(self, sleep_time=None, max_tries=None):
+        try_number = 1
+        if sleep_time is None:
+            sleep_time = self.sleep_time
+        if max_tries is None:  # Retries forever until query reaches final state
+            retry_condition = True
+        else:
+            retry_condition = try_number <= max_tries
 
 Review comment:
   Ah I missed that, sorry
   
   Instead of evaluating the condition at the start (which would only fail if max_tries is
0) how about this:
   
   ```
   while True:
       # checks
   
      if try_number > max_tries:
        break
        # Or raise exception etc.
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message