【问题标题】:How do I avoid out of order inserts using Influx line protocol on QuestDB如何在 QuestDB 上使用 Influx 线路协议避免乱序插入
【发布时间】:2021-02-10 09:19:33
【问题描述】:

我正在测试每个向 QuestDB 实例发送温度读数的 IoT 传感器,我使用 Python 中的基本 TCP 套接字连接来发送它,如下所示:

import datetime as dt
import socket
import Adafruit_DHT

HOST = '127.0.0.1'
PORT = 9009

sensor = Adafruit_DHT.DHT22
pin = 23
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

def now():
  return int((dt.datetime.utcnow() - dt.datetime(1970, 1, 1)).total_seconds() * 1000) * 1000000

while True:
  humidity, temp = Adafruit_DHT.read_retry(sensor, pin)
  if temp is not None:
    try:    
      sock.sendto(('readings,sensor=%s temp=%s %s' % (sensor_name,temp,now())).encode(), (HOST, PORT))
    except socket.error as e:
      print("Got error: %s" % (e))

sock.close()

这可以将数据插入到readings 表中,但看起来有些读数丢失了,我明白了

cannot insert records out of order

【问题讨论】:

    标签: questdb


    【解决方案1】:

    您可以省略客户端的时间戳,并允许服务器分配一个作为服务器系统时间的时间戳。这意味着您可以摆脱在示例中调用 now() 函数:

    import socket
    import Adafruit_DHT
    
    # maybe an environment variable can identify the IoT device
    sensor_name="My sensor"
    
    HOST = '127.0.0.1'
    PORT = 9009
    sensor = Adafruit_DHT.DHT22
    pin = 23
    
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    
    while True:
      humidity, temp = Adafruit_DHT.read_retry(sensor, pin)
      if temp is not None:
        try:    
          sock.sendto(('readings,sensor=%s temp=%s' % (sensor_name,temp)).encode(), (HOST, PORT))
        except socket.error as e:
          print("Got error: %s" % (e))
    
    sock.close()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多