【问题标题】:How do I write influx line protocol records to QuestDB using Python?如何使用 Python 将流入线路协议记录写入 QuestDB?
【发布时间】:2021-02-22 16:10:00
【问题描述】:

如何从 python 向 QuestDB 发送流入线协议消息?我应该使用库还是应该写入套接字? node example 看起来像

const net = require("net")

const client = new net.Socket()

const HOST = "localhost"
const PORT = 9009

function run() {
  client.connect(PORT, HOST, () => {
    const rows = [
      `trades,name=test_ilp1 value=12.4 ${Date.now() * 1e6}`,
      `trades,name=test_ilp2 value=11.4 ${Date.now() * 1e6}`,
    ]

    rows.forEach((row) => {
      client.write(`${row}\n`)
    })

    client.destroy()
  })

  client.on("data", function (data) {
    console.log("Received: " + data)
  })

  client.on("close", function () {
    console.log("Connection closed")
  })
}

run()

【问题讨论】:

    标签: questdb


    【解决方案1】:

    该示例的 Python 等效项如下所示:

    import time
    import socket
    
    HOST = 'localhost'
    PORT = 9009
    
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    
    try:
      sock.sendto(('trades,name=test_ilp1 value=12.4 %d' % (time.time_ns())).encode(), (HOST, PORT))
      sock.sendto(('trades,name=test_ilp2 value=11.4 %d' % (time.time_ns())).encode(), (HOST, PORT))
    except socket.error as e:
      print("Got error: %s" % (e))
    sock.close()
    

    请注意,在流入行消息中调用time.time_ns() 是可选的,因此您可以省略它,服务器会将系统时间分配为该行的时间戳。传入消息的格式可以参考questdb influx docs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多