InfluxDB 3 Python Client
The influxdb3-python library is the community Python client for InfluxDB 3.0 (also known as InfluxDB IOx or InfluxDB Cloud Serverless). It provides a Pythonic interface for writing and querying time-series data using Apache Arrow Flight (gRPC) and InfluxQL/SQL. The current version is 0.18.0, with new releases typically happening monthly or bi-monthly, incorporating new features and bug fixes for integration with InfluxDB 3.0.
Warnings
- gotcha The `org` parameter, which was mandatory in InfluxDB 2.x and earlier `influxdb-client` versions, is no longer required or supported by `influxdb3-python`. Including it can lead to unexpected behavior or errors.
- gotcha Using `write_dataframe()` or `query_dataframe()` methods requires either `pandas` or `polars` to be installed as optional dependencies. If these libraries are not installed, attempting to use these methods will raise an `ImportError`.
- gotcha The `query_async()` method (introduced in v0.12.0) is an `awaitable` coroutine. It must be called with `await` within an `async` function. Calling it directly without `await` will not execute the query and might lead to unexpected behavior or a `RuntimeWarning` if not awaited.
- gotcha The `WriteOptions.no_sync` flag (introduced in v0.14.0) allows for faster writes by not waiting for Write-Ahead Log (WAL) persistence confirmation. While faster, data is not guaranteed to be durable if the server crashes immediately after the write. Default is `False`.
- gotcha Prior to version 0.18.0, `InfluxDBClient3.write_file()` and `InfluxDBClient3.write_dataframe()` could fail when batching mode was enabled due to a bug. This was fixed in v0.18.0.
Install
-
pip install influxdb3-python
Imports
- InfluxDBClient3
from influxdb_client_3 import InfluxDBClient3
- Point
from influxdb_client_3 import Point
Quickstart
import os
from influxdb_client_3 import InfluxDBClient3, Point
# Ensure these environment variables are set or replace with actual values
# Example: export INFLUXDB_V3_HOST="us-east-1-1.aws.cloud2.influxdata.com"
HOST = os.environ.get("INFLUXDB_V3_HOST", "us-east-1-1.aws.cloud2.influxdata.com")
TOKEN = os.environ.get("INFLUXDB_V3_TOKEN", "YOUR_API_TOKEN")
DATABASE = os.environ.get("INFLUXDB_V3_DATABASE", "YOUR_DATABASE")
client = InfluxDBClient3(host=HOST, token=TOKEN, database=DATABASE)
try:
# Write data
point = Point("measurement1").tag("tag1", "value1").field("field1", 1.0)
client.write(point)
print("Data written successfully.")
# Query data
query = f"SELECT * FROM measurement1 WHERE time > now() - interval '1 hour'"
table = client.query(query=query, database=DATABASE)
print("\nQuery results:")
for row in table:
print(row)
except Exception as e:
print(f"An error occurred: {e}")
finally:
client.close()