You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2021/11/02 02:58:03 UTC

[GitHub] [airflow] josh-fell commented on a change in pull request #19356: Add influxdb operator

josh-fell commented on a change in pull request #19356:
URL: https://github.com/apache/airflow/pull/19356#discussion_r740674861



##########
File path: airflow/providers/influxdb/example_dags/example_influxdb_operator.py
##########
@@ -0,0 +1,39 @@
+# 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 datetime import datetime
+
+from airflow.models.dag import DAG
+from airflow.providers.influxdb.operators.influxdb import InfluxDBOperator
+
+dag = DAG(
+    'example_influxdb_operator',
+    start_date=datetime(2021, 1, 1),
+    default_args={'influxdb_conn_id': 'influxdb_conn_id'},

Review comment:
       Since there is only one task in this pipeline, you should just set the `influxdb_conn_id` arg directly in the operator call rather than in `default_args`.

##########
File path: airflow/providers/influxdb/operators/influxdb.py
##########
@@ -0,0 +1,60 @@
+#
+# 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 typing import Dict, Iterable, Mapping, Optional, Union
+
+from airflow.models import BaseOperator
+from airflow.providers.influxdb.hooks.influxdb import InfluxDBHook
+
+
+class InfluxDBOperator(BaseOperator):
+    """
+    Executes sql code in a specific InfluxDB database
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the guide:
+        :ref:`howto/operator:InfluxDBOperator`

Review comment:
       The operator doc that is referenced in the docstring is missing.

##########
File path: airflow/providers/influxdb/operators/influxdb.py
##########
@@ -0,0 +1,60 @@
+#
+# 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 typing import Dict, Iterable, Mapping, Optional, Union
+
+from airflow.models import BaseOperator
+from airflow.providers.influxdb.hooks.influxdb import InfluxDBHook
+
+
+class InfluxDBOperator(BaseOperator):
+    """
+    Executes sql code in a specific InfluxDB database
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the guide:
+        :ref:`howto/operator:InfluxDBOperator`
+
+    :param sql: the sql code to be executed. Can receive a str representing a
+        sql statement
+    :type sql: str
+    :param influxdb_conn_id: Reference to :ref:`Influxdb connection id <howto/connection:influxdb>`.
+    :type influxdb_conn_id: str
+    """
+
+    def __init__(
+        self,
+        *,
+        sql: str,
+        influxdb_conn_id: str = 'influxdb_default',
+        parameters: Optional[Union[Mapping, Iterable]] = None,

Review comment:
       The docstring is missing information on the `parameters` param. 
   
   Although it looks like the `InfluxDBHook.query()` method doesn't take a `parameters` argument so maybe this parameter is not needed.

##########
File path: airflow/providers/influxdb/example_dags/example_influxdb_operator.py
##########
@@ -0,0 +1,39 @@
+# 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 datetime import datetime
+
+from airflow.models.dag import DAG
+from airflow.providers.influxdb.operators.influxdb import InfluxDBOperator
+
+dag = DAG(
+    'example_influxdb_operator',
+    start_date=datetime(2021, 1, 1),
+    default_args={'influxdb_conn_id': 'influxdb_conn_id'},
+    tags=['example'],
+    catchup=False,
+)
+
+# [START howto_operator_influxdb]
+
+query_influxdb_task = InfluxDBOperator(
+    task_id='query_influxdb', sql='from(bucket:"test-influx") |> range(start: -10m)', dag=dag
+)
+
+# [END howto_operator_influxdb]
+
+query_influxdb_task

Review comment:
       This line is not needed. The task node will be created when instantiating the operator above.

##########
File path: airflow/providers/influxdb/operators/influxdb.py
##########
@@ -0,0 +1,60 @@
+#
+# 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 typing import Dict, Iterable, Mapping, Optional, Union
+
+from airflow.models import BaseOperator
+from airflow.providers.influxdb.hooks.influxdb import InfluxDBHook
+
+
+class InfluxDBOperator(BaseOperator):
+    """
+    Executes sql code in a specific InfluxDB database
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the guide:
+        :ref:`howto/operator:InfluxDBOperator`
+
+    :param sql: the sql code to be executed. Can receive a str representing a
+        sql statement
+    :type sql: str
+    :param influxdb_conn_id: Reference to :ref:`Influxdb connection id <howto/connection:influxdb>`.
+    :type influxdb_conn_id: str
+    """
+
+    def __init__(
+        self,
+        *,
+        sql: str,
+        influxdb_conn_id: str = 'influxdb_default',
+        parameters: Optional[Union[Mapping, Iterable]] = None,

Review comment:
       WDYT about adding `template_fields`, `template_fields_renderers`, and `template_ext` for the operator? At the very least adding some templating functionality for `sql` would be useful.

##########
File path: airflow/providers/influxdb/operators/influxdb.py
##########
@@ -0,0 +1,60 @@
+#
+# 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 typing import Dict, Iterable, Mapping, Optional, Union
+
+from airflow.models import BaseOperator
+from airflow.providers.influxdb.hooks.influxdb import InfluxDBHook
+
+
+class InfluxDBOperator(BaseOperator):
+    """
+    Executes sql code in a specific InfluxDB database
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the guide:
+        :ref:`howto/operator:InfluxDBOperator`
+
+    :param sql: the sql code to be executed. Can receive a str representing a
+        sql statement
+    :type sql: str
+    :param influxdb_conn_id: Reference to :ref:`Influxdb connection id <howto/connection:influxdb>`.
+    :type influxdb_conn_id: str
+    """
+
+    def __init__(
+        self,
+        *,
+        sql: str,
+        influxdb_conn_id: str = 'influxdb_default',
+        parameters: Optional[Union[Mapping, Iterable]] = None,
+        **kwargs,
+    ) -> None:
+        super().__init__(**kwargs)
+        self.influxdb_conn_id = influxdb_conn_id
+        self.sql = sql
+        self.parameters = parameters
+        self.hook = None
+
+    def get_hook(self):
+        """Function to retrieve the InfluxDB Hook."""
+        return InfluxDBHook(conn_id=self.influxdb_conn_id)

Review comment:
       This method seems unnecessary. You could instantiate the hook directly in the `execute()` method and the functionality would not change.

##########
File path: tests/providers/influxdb/operators/test_influxdb.py
##########
@@ -0,0 +1,40 @@
+# 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 unittest import mock
+
+from airflow.providers.influxdb.operators.influxdb import InfluxDBOperator
+from airflow.utils import timezone
+
+DEFAULT_DATE = timezone.datetime(2015, 1, 1)
+DEFAULT_DATE_ISO = DEFAULT_DATE.isoformat()
+DEFAULT_DATE_DS = DEFAULT_DATE_ISO[:10]
+TEST_DAG_ID = 'unit_test_dag'

Review comment:
       Are these variables needed for the unit test?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org