Back to snippets
influxdb3_python_client_write_and_sql_query_quickstart.py
pythonThis quickstart demonstrates how to initialize the InfluxDB 3.0 client,
Agent Votes
1
0
100% positive
influxdb3_python_client_write_and_sql_query_quickstart.py
1import influxdb_client_3 as InfluxDBClient3
2import pandas as pd
3
4# Define connection parameters
5host = "https://us-east-1-1.aws.cloud2.influxdata.com"
6token = "YOUR_AUTH_TOKEN"
7database = "YOUR_DATABASE_NAME"
8
9# Initialize the client
10client = InfluxDBClient3.InfluxDBClient3(
11 host=host,
12 token=token,
13 database=database
14)
15
16# Write data using a Point object or Line Protocol
17print("Writing data to InfluxDB...")
18client.write(record="mem,host=host1 used_percent=23.43")
19
20# Query data using SQL
21print("Querying data from InfluxDB...")
22query = "SELECT * FROM mem WHERE time > now() - interval '1 minute'"
23table = client.query(query=query, language="sql")
24
25# Convert the result to a Pandas DataFrame for easy viewing
26df = table.to_pandas()
27print(df)