[英]Dump series back into InfluxDB after querying with replaced field value
I want to send data to an MQTT Broker (Cloud) by querying measurements from InfluxDB. 我想通过查询来自InfluxDB的测量结果将数据发送到MQTT Broker(Cloud)。
I have a field in the schema which is called status
. 我在架构中有一个名为
status
的字段 。 It can either be 1
or 0
. 它可以是
1
或0
。 status=0
indicated that series has not been sent to the cloud. status=0
表示该系列尚未发送到云端。 If I get an acknowlegdment from the MQTT Broker then I wish to rewrite the query back into the database with status=1
. 如果我从MQTT Broker获得确认,那么我希望将查询重写回
status=1
的数据库。
As mentioned in FAQs for InfluxDB regarding Duplicate data If the information has the same timestamp as the previous query but with a different field value => then the update field will be shown. 如关于重复数据的InfluxDB常见问题解答中所述如果信息与前一个查询具有相同的时间戳但具有不同的字段值=>则将显示更新字段。
In order to test this I created the following: 为了测试这个,我创建了以下内容:
CREATE DATABASE dummy
USE dummy
INSERT meas_1, type=t1, status=0,value=123 1536157064275338300
query: 查询:
SELECT * FROM meas_1
provides 提供
time status type value
1536157064275338300 0 t1 234
now if I want to overwrite the series I do the following: 现在,如果我要覆盖该系列,我会执行以下操作:
INSERT meas_1, type=t1, status=1,value=123 1536157064275338300
which will overwrite the series 这将覆盖该系列
time status type value
1536157064275338300 1 t1 234
(Note: this is not possible via Tags currently in InfluxDB) (注意:目前在InfluxDB中的标签无法实现这一点)
"status"=0
. "status"=0
的客户端查询一些信息。 status=1
. status=1
。 I am using the InfluxDBClient Python3
to create the Application (MQTT + InfluxDB) 我使用
InfluxDBClient Python3
来创建应用程序(MQTT + InfluxDB)
Within the write_points
API there is a parameter which mentions batch_size
which require int
as input. 在
write_points
API中有一个参数,它提到了需要int
作为输入的batch_size
。
I am not sure how can I use this with the Application that I want. 我不知道如何在我想要的应用程序中使用它。 Can someone guide me with this or with the Schema of the DB so that I can upload actual and non-redundant information to the cloud ?
有人可以指导我或使用数据库的架构,以便我可以将实际和非冗余信息上传到云端吗?
The batch_size
is actually the length of the list of the measurements that needs to passed to write_points
. batch_size
实际上是需要传递给write_points
的度量列表的长度。
Create client and query from measurement (here, we query gps information) 从测量创建客户端和查询(这里,我们查询gps信息)
client = InfluxDBClient(database='dummy') op = client.query('SELECT * FROM gps WHERE "status"=0', epoch='ns')
Make the ResultSet
into a list: 将
ResultSet
放入列表中:
batch = list(op.get_points('gps'))
create an empty list for update 创建一个空列表进行更新
updated_batch = []
parse through each measurement and change the status
flag to 1
. 解析每个度量并将
status
标志更改为1
。 Note, default values in InfluxDB are float 注意,InfluxDB中的默认值是float
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)
Finally dump the points back via the client with batch_size
as the length of the updated_batch
最后通过客户端使用
batch_size
作为updated_batch
的长度来回转点
client.write_points(updated_batch, batch_size=len(updated_batch))
This overwrites the series because it contains the same timestamps with status
field set to 1
这会覆盖系列,因为它包含
status
字段设置为1
的相同时间戳
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.