0

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

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()
4

1 回答 1

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 文档中找到参考

于 2021-02-22T16:15:34.350 回答