脚本
我想通过查询来自InfluxDB的测量结果将数据发送到MQTT Broker(Cloud)。
我在架构中有一个名为status的字段。它可以是1或0. status = 0表示……
该 batch_size 实际上是需要传递给的测量列表的长度 write_points 。
batch_size
write_points
从测量创建客户端和查询(这里,我们查询gps信息)
client = InfluxDBClient(database='dummy') op = client.query('SELECT * FROM gps WHERE "status"=0', epoch='ns')
制作 ResultSet 列表:
ResultSet
batch = list(op.get_points('gps'))
创建一个空列表进行更新
updated_batch = []
解析每个测量并更改 status 国旗到 1 。注意,InfluxDB中的默认值是float
status
1
for each in batch: new_mes = { 'measurement': 'gps', 'tags': { 'type': 'gps' }, 'time': each['time'], 'fields': { 'lat': float(each['lat']), 'lon': float(each['lon']), 'alt': float(each['alt']), 'status': float(1) } } updated_batch.append(new_mes)
最后通过客户端将积分转回 batch_size 作为长度 updated_batch
updated_batch
client.write_points(updated_batch, batch_size=len(updated_batch))
这会覆盖系列,因为它包含相同的时间戳 status 字段设置为 1