Python InfluxDB_Client: 写入和读取 GPS 数据
以下是一个简单的示例,展示如何使用 Python 的 influxdb_client 库来写入和读取 GPS 数据。
写入数据:
from influxdb_client import InfluxDBClient, Point
from influxdb_client.client.write_api import SYNCHRONOUS
# 创建 InfluxDBClient 对象
client = InfluxDBClient(url='http://localhost:8086', token='my-token', org='my-org')
# 创建写入 API 对象
write_api = client.write_api(write_options=SYNCHRONOUS)
# 创建 Point 对象,设置 GPS 数据
point = Point('gps_data').tag('location', 'my_location').field('latitude', 40.7128).field('longitude', -74.0060)
# 写入数据
write_api.write(bucket='my-bucket', record=point)
读取数据:
from influxdb_client import InfluxDBClient
# 创建 InfluxDBClient 对象
client = InfluxDBClient(url='http://localhost:8086', token='my-token', org='my-org')
# 查询 GPS 数据
query = f'from(bucket:"my-bucket") |> range(start: -1h) |> filter(fn: (r) => r._measurement == "gps_data" and r.location == "my_location")'
tables = client.query_api().query(query)
# 打印 GPS 数据
for table in tables:
for record in table.records:
print(f'Time: {record.time}, Latitude: {record.latitude}, Longitude: {record.longitude}')
在这个例子中,我们使用 InfluxDBClient 创建了一个 InfluxDB 客户端对象。然后,我们使用写入 API 写入一个 GPS 数据点。最后,我们使用查询 API 从 InfluxDB 中查询 GPS 数据,并打印出结果。
原文地址: https://www.cveoy.top/t/topic/ngiP 著作权归作者所有。请勿转载和采集!