예제 #1
0
파일: influxdb.py 프로젝트: vipadm/airflow
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
    :param influxdb_conn_id: Reference to :ref:`Influxdb connection id <howto/connection:influxdb>`.
    """

    template_fields: Sequence[str] = ('sql',)

    def __init__(
        self,
        *,
        sql: str,
        influxdb_conn_id: str = 'influxdb_default',
        **kwargs,
    ) -> None:
        super().__init__(**kwargs)
        self.influxdb_conn_id = influxdb_conn_id
        self.sql = sql

    def execute(self, context: 'Context') -> None:
        self.log.info('Executing: %s', self.sql)
        self.hook = InfluxDBHook(conn_id=self.influxdb_conn_id)
        self.hook.query(self.sql)
def test_influxdb_hook():
    bucket_name = 'test-influx'
    influxdb_hook = InfluxDBHook()
    client = influxdb_hook.get_conn()
    print(client)
    print(f"Organization name {influxdb_hook.org_name}")

    # Make sure enough permissions to create bucket.
    influxdb_hook.create_bucket(bucket_name,
                                "Bucket to test influxdb connection",
                                influxdb_hook.org_name)
    influxdb_hook.write(bucket_name, "test_point", "location", "Prague",
                        "temperature", 25.3, True)

    tables = influxdb_hook.query(
        'from(bucket:"test-influx") |> range(start: -10m)')

    for table in tables:
        print(table)
        for record in table.records:
            print(record.values)

    bucket_id = influxdb_hook.find_bucket_id_by_name(bucket_name)
    print(bucket_id)
    # Delete bucket takes bucket id.
    influxdb_hook.delete_bucket(bucket_name)
예제 #3
0
파일: influxdb.py 프로젝트: vipadm/airflow
 def execute(self, context: 'Context') -> None:
     self.log.info('Executing: %s', self.sql)
     self.hook = InfluxDBHook(conn_id=self.influxdb_conn_id)
     self.hook.query(self.sql)