Add query script
This commit is contained in:
33
influxdb_query_SCADA_data.py
Normal file
33
influxdb_query_SCADA_data.py
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
from influxdb_client import InfluxDBClient, Point, WriteOptions
|
||||||
|
from influxdb_client.client.query_api import QueryApi
|
||||||
|
import influxdb_info
|
||||||
|
|
||||||
|
# 配置 InfluxDB 连接
|
||||||
|
url = influxdb_info.url
|
||||||
|
token = influxdb_info.token
|
||||||
|
org = influxdb_info.org
|
||||||
|
bucket = "SCADA_data"
|
||||||
|
|
||||||
|
# 创建 InfluxDB 客户端
|
||||||
|
client = InfluxDBClient(url=url, token=token, org=org)
|
||||||
|
|
||||||
|
# 创建查询 API 对象
|
||||||
|
query_api = client.query_api()
|
||||||
|
|
||||||
|
# 构建查询语句
|
||||||
|
query = f'''
|
||||||
|
from(bucket: "{bucket}")
|
||||||
|
|> range(start: -1h)
|
||||||
|
|> filter(fn: (r) => r._measurement == "your-measurement" and r._field == "your-field")
|
||||||
|
'''
|
||||||
|
|
||||||
|
# 执行查询
|
||||||
|
result = query_api.query(query)
|
||||||
|
|
||||||
|
# 处理查询结果
|
||||||
|
for table in result:
|
||||||
|
for record in table.records:
|
||||||
|
print(f"Time: {record.get_time()}, Value: {record.get_value()}, Measurement: {record.get_measurement()}, Field: {record.get_field()}")
|
||||||
|
|
||||||
|
# 关闭客户端连接
|
||||||
|
client.close()
|
||||||
Reference in New Issue
Block a user