Package: influxdb3-python
InfluxDB 3.0 Python ClientIntroductioninfluxdb_client_3is a Python module that provides a simple and convenient way to interact with InfluxDB 3.0. This module supports both writing data to InfluxDB and querying data using the Flight client, which allows you to execute SQL and InfluxQL queries on InfluxDB 3.0.Dependenciespyarrow(automatically installed)pandas(optional)InstallationYou can install 'influxdb3-python' usingpip:pipinstallinfluxdb3-pythonNote: This does not include Pandas support. If you would like to use key features such asto_pandas()andwrite_file()you will need to installpandasseparately.Note: Please make sure you are using 3.6 or above. For the best performance use 3.11+UsageOne of the easiest ways to get started is to checkout the"Pokemon Trainer Cookbook". This scenario takes you through the basics of both the client library and Pyarrow.Importing the Modulefrominfluxdb_client_3importInfluxDBClient3,PointInitializationIf you are using InfluxDB Cloud, then you should note that:You will need to supply your org id, this is not necessary for InfluxDB Dedicated.Use a bucketname for the database argument.client=InfluxDBClient3(token="your-token",host="your-host",org="your-org",database="your-database")Writing DataYou can write data using the Point class, or supplying line protocol.Using Pointspoint=Point("measurement").tag("location","london").field("temperature",42)client.write(point)Using Line Protocolpoint="measurement fieldname=0"client.write(point)Write from fileUsers can import data from CSV, JSON, Feather, ORC, Parquetimportinfluxdb_client_3asInfluxDBClient3importpandasaspdimportnumpyasnpfrominfluxdb_client_3importwrite_client_options,WritePrecision,WriteOptions,InfluxDBErrorclassBatchingCallback(object):defsuccess(self,conf,data:str):print(f"Written batch:{conf}, data:{data}")deferror(self,conf,data:str,exception:InfluxDBError):print(f"Cannot write batch:{conf}, data:{data}due:{exception}")defretry(self,conf,data:str,exception:InfluxDBError):print(f"Retryable error occurs for batch:{conf}, data:{data}retry:{exception}")callback=BatchingCallback()write_options=WriteOptions(batch_size=500,flush_interval=10_000,jitter_interval=2_000,retry_interval=5_000,max_retries=5,max_retry_delay=30_000,exponential_base=2)wco=write_client_options(success_callback=callback.success,error_callback=callback.error,retry_callback=callback.retry,WriteOptions=write_options)withInfluxDBClient3.InfluxDBClient3(token="INSERT_TOKEN",host="eu-central-1-1.aws.cloud2.influxdata.com",org="6a841c0c08328fb1",database="python",write_client_options=wco)asclient:client.write_file(file='./out.csv',timestamp_column='time',tag_columns=["provider","machineID"])client.write_file(file='./out.json',timestamp_column='time',tag_columns=["provider","machineID"],date_unit='ns')Pandas DFclient._write_api.write(bucket="pokemon-codex",record=pd_df,data_frame_measurement_name='caught',data_frame_tag_columns=['trainer','id','num'],data_frame_timestamp_column='timestamp')Polars DFclient._write_api.write(bucket="pokemon-codex",record=pl_df,data_frame_measurement_name='caught',data_frame_tag_columns=['trainer','id','num'],data_frame_timestamp_column='timestamp')QueryingQuerying with SQLquery="select * from measurement"reader=client.query(query=query,language="sql")table=reader.read_all()print(table.to_pandas().to_markdown())Querying with influxqlquery="select * from measurement"reader=client.query(query=query,language="influxql")table=reader.read_all()print(table.to_pandas().to_markdown())Windows UsersCurrently, Windows users require an extra installation when querying via Flight natively. This is due to the fact gRPC cannot locate Windows root certificates. To work around this please follow these steps:
InstallcertifipipinstallcertifiNext include certifi within the flight client options:importinfluxdb_client_3asInfluxDBClient3importpandasaspdimportnumpyasnpfrominfluxdb_client_3importflight_client_optionsimportcertififh=open(certifi.where(),"r")cert=fh.read()fh.close()client=InfluxDBClient3.InfluxDBClient3(token="",host="b0c7cce5-8dbc-428e-98c6-7f996fb96467.a.influxdb.io",org="6a841c0c08328fb1",database="flightdemo",flight_client_options=flight_client_options(tls_root_certs=cert))table=client.query(query="SELECT * FROM flight WHERE time > now() - 4h",language="influxql")print(table.to_pandas())You may also include your own root certificate via this manor aswell.