You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "ASF GitHub Bot (JIRA)" <ji...@apache.org> on 2018/12/01 21:15:00 UTC

[jira] [Commented] (AIRFLOW-3403) create athena sensor

    [ https://issues.apache.org/jira/browse/AIRFLOW-3403?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16706006#comment-16706006 ] 

ASF GitHub Bot commented on AIRFLOW-3403:
-----------------------------------------

kaxil closed pull request #4244: [AIRFLOW-3403] Create Athena sensor
URL: https://github.com/apache/incubator-airflow/pull/4244
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/airflow/contrib/sensors/aws_athena_sensor.py b/airflow/contrib/sensors/aws_athena_sensor.py
new file mode 100644
index 0000000000..af864d3a36
--- /dev/null
+++ b/airflow/contrib/sensors/aws_athena_sensor.py
@@ -0,0 +1,79 @@
+# -*- 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 airflow.exceptions import AirflowException
+from airflow.utils.decorators import apply_defaults
+from airflow.contrib.hooks.aws_athena_hook import AWSAthenaHook
+from airflow.sensors.base_sensor_operator import BaseSensorOperator
+
+
+class AthenaSensor(BaseSensorOperator):
+    """
+    Asks for the state of the Query until it reaches a failure state or success state.
+    If it fails, failing the task.
+
+    :param query_execution_id: query_execution_id to check the state of
+    :type query_execution_id: str
+    :param max_retires: Number of times to poll for query state before
+        returning the current state, defaults to None
+    :type max_retires: int
+    :param aws_conn_id: aws connection to use, defaults to 'aws_default'
+    :type aws_conn_id: str
+    :param sleep_time: Time to wait between two consecutive call to
+        check query status on athena, defaults to 10
+    :type sleep_time: int
+    """
+
+    INTERMEDIATE_STATES = ('QUEUED', 'RUNNING',)
+    FAILURE_STATES = ('FAILED', 'CANCELLED',)
+    SUCCESS_STATES = ('SUCCEEDED',)
+
+    template_fields = ['query_execution_id']
+    template_ext = ()
+    ui_color = '#66c3ff'
+
+    @apply_defaults
+    def __init__(self,
+                 query_execution_id,
+                 max_retires=None,
+                 aws_conn_id='aws_default',
+                 sleep_time=10,
+                 *args, **kwargs):
+        super(BaseSensorOperator, self).__init__(*args, **kwargs)
+        self.aws_conn_id = aws_conn_id
+        self.query_execution_id = query_execution_id
+        self.hook = None
+        self.sleep_time = sleep_time
+        self.max_retires = max_retires
+
+    def poke(self, context):
+        self.hook = self.get_hook()
+        self.hook.get_conn()
+        state = self.hook.poll_query_status(self.query_execution_id, self.max_retires)
+
+        if state in self.FAILURE_STATES:
+            raise AirflowException('Athena sensor failed')
+
+        if state in self.INTERMEDIATE_STATES:
+            return False
+        return True
+
+    def get_hook(self):
+        return AWSAthenaHook(self.aws_conn_id, self.sleep_time)
diff --git a/docs/code.rst b/docs/code.rst
index 80ec76193f..145e1f2c1b 100644
--- a/docs/code.rst
+++ b/docs/code.rst
@@ -201,6 +201,7 @@ Operators
 Sensors
 ^^^^^^^
 
+.. autoclass:: airflow.contrib.sensors.aws_athena_sensor.AthenaSensor
 .. autoclass:: airflow.contrib.sensors.aws_redshift_cluster_sensor.AwsRedshiftClusterSensor
 .. autoclass:: airflow.contrib.sensors.bash_sensor.BashSensor
 .. autoclass:: airflow.contrib.sensors.bigquery_sensor.BigQueryTableSensor
diff --git a/tests/contrib/sensors/test_athena_sensor.py b/tests/contrib/sensors/test_athena_sensor.py
new file mode 100644
index 0000000000..6a8d713e3f
--- /dev/null
+++ b/tests/contrib/sensors/test_athena_sensor.py
@@ -0,0 +1,71 @@
+# -*- 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.
+
+import unittest
+from airflow import configuration, AirflowException
+from airflow.contrib.sensors.aws_athena_sensor import AthenaSensor
+from airflow.contrib.hooks.aws_athena_hook import AWSAthenaHook
+
+try:
+    from unittest import mock
+except ImportError:
+    try:
+        import mock
+    except ImportError:
+        mock = None
+
+
+class TestAthenaSensor(unittest.TestCase):
+
+    def setUp(self):
+        configuration.load_test_config()
+
+        self.sensor = AthenaSensor(task_id='test_athena_sensor',
+                                   query_execution_id='abc',
+                                   sleep_time=5,
+                                   max_retires=1,
+                                   aws_conn_id='aws_default')
+
+    @mock.patch.object(AWSAthenaHook, 'poll_query_status', side_effect=("SUCCEEDED",))
+    def test_poke_success(self, mock_poll_query_status):
+        self.assertTrue(self.sensor.poke(None))
+
+    @mock.patch.object(AWSAthenaHook, 'poll_query_status', side_effect=("RUNNING",))
+    def test_poke_running(self, mock_poll_query_status):
+        self.assertFalse(self.sensor.poke(None))
+
+    @mock.patch.object(AWSAthenaHook, 'poll_query_status', side_effect=("QUEUED",))
+    def test_poke_queued(self, mock_poll_query_status):
+        self.assertFalse(self.sensor.poke(None))
+
+    @mock.patch.object(AWSAthenaHook, 'poll_query_status', side_effect=("FAILED",))
+    def test_poke_failed(self, mock_poll_query_status):
+        with self.assertRaises(AirflowException) as context:
+            self.sensor.poke(None)
+        self.assertIn('Athena sensor failed', str(context.exception))
+
+    @mock.patch.object(AWSAthenaHook, 'poll_query_status', side_effect=("CANCELLED",))
+    def test_poke_cancelled(self, mock_poll_query_status):
+        with self.assertRaises(AirflowException) as context:
+            self.sensor.poke(None)
+        self.assertIn('Athena sensor failed', str(context.exception))
+
+
+if __name__ == '__main__':
+    unittest.main()


 

----------------------------------------------------------------
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


> create athena sensor
> --------------------
>
>                 Key: AIRFLOW-3403
>                 URL: https://issues.apache.org/jira/browse/AIRFLOW-3403
>             Project: Apache Airflow
>          Issue Type: Improvement
>            Reporter: tal
>            Assignee: tal
>            Priority: Minor
>
> It will be nice to have an Athena sensor to monitor the progress of the query



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)