简体   繁体   English

在使用替换字段值查询后,将系列转储回InfluxDB

[英]Dump series back into InfluxDB after querying with replaced field value

Scenario 脚本

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 . 它可以是10 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中的标签无法实现这一点)

Usage 用法

  1. Query some information using the client with "status"=0 . 使用"status"=0的客户端查询一些信息。
  2. Restructure JSON to be sent to the cloud 重构要发送到云的JSON
  3. Send the information to cloud 将信息发送到云端
  4. If successful then write the output from Step 1. back into the DB but with status=1 . 如果成功,则将步骤1的输出写回DB,但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的度量列表的长度。

Steps 脚步

  1. 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') 
  2. Make the ResultSet into a list: ResultSet放入列表中:

      batch = list(op.get_points('gps')) 
  3. create an empty list for update 创建一个空列表进行更新

      updated_batch = [] 
  4. 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) 
  5. 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.

相关问题 使用 DateTime 查询 InfluxDB - Querying InfluxDB with DateTime Django QuerySet查询或过滤特定字段中的“奇数”和/或“偶数”值 - Django QuerySet querying or filtering “Odd” and/or “Even” value in a particular field Django ManyToMany字段查询 - Django ManyToMany Field Querying InfluxDB 字段值混合来自另一个测量 - InfluxDB field values mixed from another measurement 在namedtuple的namedtuple列表中返回一个替换字段 - returning a replaced field in a list of namedtuples in a namedtuple 熊猫系列中的替换值,其中要替换的元素包含要替换的元素的一部分 - Replace values in pandas series where the element to be replaced contains a part of the element by which it is to be replaced 在一组数据点上应用 fft 后如何获得傅立叶级数系数​​? - How to get the Fourier series coefficients back after applying fft on a set of data point? 新迭代后行值被替换 - row values get replaced after new iteration 提交后Django表单字段未设置值 - Django Form Field Not Setting Value After Submitting Facebook Messenger bot的日期选择器Webview-无法将字段值带回bot的输入字段 - Date picker webview for Facebook Messenger bot - cannot bring field value back to bot's input field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM