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/08/03 04:37:32 UTC

[GitHub] [airflow] uranusjr commented on a change in pull request #17068: Influxdb Hook

uranusjr commented on a change in pull request #17068:
URL: https://github.com/apache/airflow/pull/17068#discussion_r681427401



##########
File path: airflow/providers/influxdb/hooks/influxdb.py
##########
@@ -0,0 +1,166 @@
+#
+# 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.
+
+"""This module allows to connect to a InfluxDB database."""
+
+from typing import List
+
+import pandas as pd
+from influxdb_client import InfluxDBClient
+from influxdb_client.client.flux_table import FluxTable
+from influxdb_client.client.write.point import Point
+from influxdb_client.client.write_api import SYNCHRONOUS
+
+from airflow.hooks.base import BaseHook
+from airflow.models import Connection
+
+
+class InfluxDBHook(BaseHook):
+    """
+    Interact with InfluxDB.
+
+    Performs a connection to InfluxDB and retrieves client.
+
+    :param influxdb_conn_id: Reference to :ref:`Influxdb connection id <howto/connection:influxdb>`.
+    :type influxdb_conn_id: str
+    """
+
+    conn_name_attr = 'influxdb_conn_id'
+    default_conn_name = 'influxdb_default'
+    conn_type = 'influxdb'
+    hook_name = 'Influxdb'
+
+    def __init__(self, conn_id: str = default_conn_name, *args, **kwargs) -> None:
+        super().__init__(*args, **kwargs)
+        self.influxdb_conn_id = conn_id
+        self.connection = kwargs.pop("connection", None)
+        self.client = None
+        self.extras = None
+        self.uri = None
+        self.org_name = None
+
+    def get_client(self, uri, token, org_name):
+        return InfluxDBClient(url=uri, token=token, org=org_name)
+
+    def get_uri(self, conn: Connection):
+        """
+        Function to add additional parameters to the URI
+        based on SSL or other InfluxDB host requirements
+
+        """
+        return '{scheme}://{host}:{port}'.format(
+            scheme='https' if conn.schema is None else f'{conn.schema}',
+            host=conn.host,
+            port='7687' if conn.port is None else f'{conn.port}',
+        )
+
+    def get_conn(self) -> InfluxDBClient:
+        """
+        Function that initiates a new InfluxDB connection
+        with token and organization name
+        """
+        self.connection = self.get_connection(self.influxdb_conn_id)
+        self.extras = self.connection.extra_dejson.copy()
+
+        self.uri = self.get_uri(self.connection)
+        self.log.info('URI: %s', self.uri)
+
+        if self.client is not None:
+            return self.client
+
+        token = self.connection.extra_dejson.get('token')
+        self.org_name = self.connection.extra_dejson.get('org_name')
+
+        self.log.info('URI: %s', self.uri)
+        self.log.info('Organization: %s', self.org_name)
+
+        self.client = self.get_client(self.uri, token, self.org_name)
+
+        return self.client
+
+    def query(self, query) -> List[FluxTable]:
+        """
+        Function to use the query_api
+        to run the query.
+        Note: The bucket name
+        should be included in the query
+        'from(bucket:"my-bucket") |> range(start: -10m)'
+
+        :param query: InfluxDB query
+        :return: List[FluxTable]
+        """
+        client = self.get_conn()
+
+        query_api = client.query_api()
+        return query_api.query(query)
+
+    def query_with_df(self, query) -> pd.DataFrame:

Review comment:
       ```suggestion
       def query_to_df(self, query) -> pd.DataFrame:
   ```
   
   “With” to me means `query` is a dataframe, not the return value.
   
   (`query_df` or `query_data_frame` like how the Influx DB client calls it is fine as well.)




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