Telegraf 订阅mqtt 转发数据到QuestDB
·
1.QuestDB
时序数据库是专门用于存储和处理时间序列数据的数据库,支持时序数据高效读写、高压缩存储、插值和聚合等功能
项目中需要从物联设备中定时采集实时数据并入库,数据库选择使用时序数据库 QuestDB
QuestDB 官方文档:https://questdb.io/docs/
1.安装成功后,控制台默认9000端口,正常访问

2.QuestDB相关协议
设想是从mq中获取实时数据,直接插入数据库中,(不通过代码) ,所以选择 InfluxDB Line Protocol 插入数据
查询数据使用 PostgreSQL wire protocol

3.Telegraf
Telegraf是一个基于服务器的代理,用于从数据库、系统和物联网传感器收集和发送所有指标和事件,在数据采集和平台监控系统中,Telegraf 可以采集多种组件的运行信息
最关键的是,Telegraf支持订阅mqtt,并通过 InfluxDB Line Protocol 入库
值得注意的是,Telegraf默认接受 InfluxDB Line Protocol 格式的数据,项目中约定的 mqtt topic 中 payload 数据格式为 json
{
"data": [
{
"create_time": 1465839830100400000,
"name": "faseeee, gs",
"value": 5,
"id":1
},
{
"create_time": 1465839830100400000,
"name": "eeeda:ee",
"value": 6,
"id":2
},
{
"create_time": 1465839830100400000,
"name": "fffff",
"value": 7,
"id": 2
},
{
"create_time": 1465839830100400000,
"name": "ssdds",
"value": 8,
"id" : 5
}
]
}
所以要更改配置文件 具体参考官方文档
https://docs.influxdata.com/telegraf/v1.24/data_formats/input/json/
4.docker 安装 telegraf
docker pull Telegraf:1.24.2- 创建/root/docker-compose/telegraf/conf 以及telegraf.conf,telegraf.conf内容:
# Configuration for Telegraf agent
[agent]
omit_hostname = true #默认会自动添加 hostname字段,在此省略
# logtarget = "file"
# logfile = ""
[[inputs.mqtt_consumer]]
## The MQTT broker
servers = ["tcp://39.97.199.131:1883"] # 连接mqtt服务器
## Topics that will be subscribed to.
topics = [
"topic/insert2qdb", # 订阅的主题
]
data_format = "json" # 接受的数据格式 json
name_override = "trades" # 表名
json_string_fields = ["id","name","value","create_time"] # 解析的字段
json_query = "data" # 解析的数组名 data
topic_tag="" # 默认会自动添加 topic 字段,在此省略
[[outputs.socket_writer]]
# Write metrics to a local QuestDB instance over TCP
address = "tcp://39.97.199.131:9009" # 写入数据库
- 创建docker-compose.yml
version: "3"
services:
telegraf:
image: telegraf:1.24.2
container_name: telegraf
restart: always
volumes:
- /root/docker-compose/telegraf/conf/telegraf.conf:/etc/telegraf/telegraf.conf
networks:
jm:
external: true
cd /root/docker-compose/telegrafdocker-compose up - d启动telegraf
5.测试
发布json格式数据到topic/insert2qdb

通过telegraf解析转发,以influx db 行协议,写入questdb中

更多推荐



所有评论(0)