Ingest data into QuestDB using Influx Line Protocol
# For First batch, run the script "as is" | |
# For Second batch, uncomment line 26 | |
# For Third batch, uncomment line 29 | |
from influx_line_protocol import Metric | |
import datetime | |
import socket | |
import random | |
# QuestDB installed on local listening for TCP/UDP packets on port 9009 | |
host = "localhost" | |
port = 9009 | |
# Current time in nanoseconds | |
def current_timestamp(): | |
return int((datetime.datetime.utcnow() - datetime.datetime(1970, 1, 1)).total_seconds() * 1000) * 1000000 | |
# Create a batch of 10000 random metrics to ingest. | |
metric = Metric("engine_diagnostics") | |
str_metric = "" | |
metrics = "" | |
for i in range(10000): | |
metric.with_timestamp(current_timestamp()) | |
# metric.add_tag('Make', 'Volkswagen') | |
metric.add_tag('Model', 'Polo') | |
metric.add_value('engine_temperature', random.uniform(120.0, 150.0)) | |
# metric.add_value('oil_gauge', random.uniform(91.0,99.5)) | |
str_metric = str(metric) | |
str_metric += "\n" | |
metrics += str_metric | |
# Convert string to bytes | |
bytes_metric = bytes(metrics, "utf-8") | |
# Open a socket & connect to 9009 | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.connect((host, port)) | |
s.sendall(bytes_metric) | |
s.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment