Connecting to an InfluxDB instance using Python
To connect to you InfluxDB instance using Python v3, we recommend you using the Python client library for InfluxDB 2.0, that is compatible with InfluxDB 1.8+.
The documentation includes an example for querying 1.8 instances.
You will have to query your Influx instance using the Flux query language. Also, you will have to install first the influxdb-client.
Code example using the recommended Python client library
Python 3.6.8 (default, Nov 16 2020, 16:55:22)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from influxdb_client import InfluxDBClient
>>> url = 'https://dbod-xxxxxxx.cern.ch:<port_number>'
>>> database = "my_database"
>>> retention_policy = 'autogen'
>>> bucket = f'{database}/{retention_policy}'
>>> username="*******"
>>> password="***********"
>>> client = InfluxDBClient(url=url, token=f'{username}:{password}', org='-', verify_ssl=True, ssl_ca_cert='/etc/ssl/certs/CERN-bundle.pem')
>>> client.ping()
True
>>> query_api = client.query_api()
>>> tables = query_api.query('from(bucket:"my_database/autogen") |> range(start: -10m)')
>>> for table in tables:
... print(table)
... for row in table.records:
... print (row.values)
...
FluxTable() columns: 9, records: 10
{'result': '_result', 'table': 0, '_start': datetime.datetime(2023, 4, 5, 9, 55, 30, 93084, tzinfo=UTC), '_stop': datetime.datetime(2023, 4, 5, 10, 5, 30, 93084, tzinfo=UTC), '_time': datetime.datetime(2023, 4, 5, 9, 56, 0, 444292, tzinfo=UTC), '_value': 1680688560.0, '_field': 'value', '_measurement': 'collectd_ping', 'measurement': 'ping'}
{'result': '_result', 'table': 0, '_start': datetime.datetime(2023, 4, 5, 9, 55, 30, 93084, tzinfo=UTC), '_stop': datetime.datetime(2023, 4, 5, 10, 5, 30, 93084, tzinfo=UTC), '_time': datetime.datetime(2023, 4, 5, 9, 57, 0, 444980, tzinfo=UTC), '_value': 1680688620.0, '_field': 'value', '_measurement': 'collectd_ping', 'measurement': 'ping'}
{'result': '_result', 'table': 0, '_start': datetime.datetime(2023, 4, 5, 9, 55, 30, 93084, tzinfo=UTC), '_stop': datetime.datetime(2023, 4, 5, 10, 5, 30, 93084, tzinfo=UTC), '_time': datetime.datetime(2023, 4, 5, 9, 58, 0, 443614, tzinfo=UTC), '_value': 1680688680.0, '_field': 'value', '_measurement': 'collectd_ping', 'measurement': 'ping'}
{'result': '_result', 'table': 0, '_start': datetime.datetime(2023, 4, 5, 9, 55, 30, 93084, tzinfo=UTC), '_stop': datetime.datetime(2023, 4, 5, 10, 5, 30, 93084, tzinfo=UTC), '_time': datetime.datetime(2023, 4, 5, 9, 59, 0, 446910, tzinfo=UTC), '_value': 1680688740.0, '_field': 'value', '_measurement': 'collectd_ping', 'measurement': 'ping'}
{'result': '_result', 'table': 0, '_start': datetime.datetime(2023, 4, 5, 9, 55, 30, 93084, tzinfo=UTC), '_stop': datetime.datetime(2023, 4, 5, 10, 5, 30, 93084, tzinfo=UTC), '_time': datetime.datetime(2023, 4, 5, 10, 0, 0, 448202, tzinfo=UTC), '_value': 1680688800.0, '_field': 'value', '_measurement': 'collectd_ping', 'measurement': 'ping'}
{'result': '_result', 'table': 0, '_start': datetime.datetime(2023, 4, 5, 9, 55, 30, 93084, tzinfo=UTC), '_stop': datetime.datetime(2023, 4, 5, 10, 5, 30, 93084, tzinfo=UTC), '_time': datetime.datetime(2023, 4, 5, 10, 1, 0, 444059, tzinfo=UTC), '_value': 1680688860.0, '_field': 'value', '_measurement': 'collectd_ping', 'measurement': 'ping'}
{'result': '_result', 'table': 0, '_start': datetime.datetime(2023, 4, 5, 9, 55, 30, 93084, tzinfo=UTC), '_stop': datetime.datetime(2023, 4, 5, 10, 5, 30, 93084, tzinfo=UTC), '_time': datetime.datetime(2023, 4, 5, 10, 2, 0, 446218, tzinfo=UTC), '_value': 1680688920.0, '_field': 'value', '_measurement': 'collectd_ping', 'measurement': 'ping'}
{'result': '_result', 'table': 0, '_start': datetime.datetime(2023, 4, 5, 9, 55, 30, 93084, tzinfo=UTC), '_stop': datetime.datetime(2023, 4, 5, 10, 5, 30, 93084, tzinfo=UTC), '_time': datetime.datetime(2023, 4, 5, 10, 3, 0, 480692, tzinfo=UTC), '_value': 1680688980.0, '_field': 'value', '_measurement': 'collectd_ping', 'measurement': 'ping'}
{'result': '_result', 'table': 0, '_start': datetime.datetime(2023, 4, 5, 9, 55, 30, 93084, tzinfo=UTC), '_stop': datetime.datetime(2023, 4, 5, 10, 5, 30, 93084, tzinfo=UTC), '_time': datetime.datetime(2023, 4, 5, 10, 4, 0, 448445, tzinfo=UTC), '_value': 1680689040.0, '_field': 'value', '_measurement': 'collectd_ping', 'measurement': 'ping'}
{'result': '_result', 'table': 0, '_start': datetime.datetime(2023, 4, 5, 9, 55, 30, 93084, tzinfo=UTC), '_stop': datetime.datetime(2023, 4, 5, 10, 5, 30, 93084, tzinfo=UTC), '_time': datetime.datetime(2023, 4, 5, 10, 5, 0, 447472, tzinfo=UTC), '_value': 1680689100.0, '_field': 'value', '_measurement': 'collectd_ping', 'measurement': 'ping'}
Using the deprecated influxdb-python library for 1.x instances supporting InfluxQL
The influxdb-python v1 client libraries for InfluxDB were typically developed and maintained by community members. They have all now been succeeded by v2 client libraries. They are being archived it in favor of the v2 client library mentioned above.
You can read the documentation to understand how to interact with the instance.
Code example using the deprecated Python v1 client libraries
Python 3.6.8 (default, Nov 16 2020, 16:55:22)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from influxdb import InfluxDBClient
>>> host = "dbod-*****.cern.ch"
>>> port = <port_number>
>>> dbname = "<my_database>"
>>> username = "******"
>>> password = "************"
>>> client = InfluxDBClient(host, port, username, password, dbname, ssl=True)
>>> client.ping()
/usr/lib/python3.6/site-packages/urllib3/connectionpool.py:1004: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
InsecureRequestWarning,
'1.8.3'
>>> dbs = client.get_list_database()
/usr/lib/python3.6/site-packages/urllib3/connectionpool.py:1004: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
InsecureRequestWarning,
>>> dbs
[{'name': '_internal'}, {'name': 'AMS_ISS'}, {'name': 'AMS_FS'}, {'name': 'UTTPS_QM'}, {'name': 'UTTPS_FM'}, {'name': 'UTTPS_FS'}, {'name': 'Pump_Life_Test'}, {'name': 'UTTPE_EM'}, {'name': 'Pump_Test'}, {'name': 'TrackerDB'}, {'name': 'TRD_Expert'}, {'name': 'L0_PG_RAW_P'}, {'name': 'L0_TW_RAW_P'}, {'name': 'L0_IHEP_RAW_P'}]