Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to store relevant information through InfluxDB

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to store relevant information through InfluxDB". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to store relevant information through InfluxDB.

InfluxDB is an open source sequential database developed by InfluxData. It is written by Go and focuses on querying and storing sequential data with high performance. compared with the OpenTSDB database InfluxDB introduced in the previous issue, it is lighter than InfluxDB, and InfluxDB is stronger than OpenTSDB in the benchmark test of all indicators given by InfluxData.

In the face of large-scale and rapid growth of Internet of things sensor collection, transaction records and other data, the accumulation speed of time series data is very fast. Time series database processes this large-scale data by improving efficiency, and brings performance improvement, including higher Ingest Rates, faster large-scale queries (although some databases support more queries than other databases) and better data compression.

Install and verify InfluxDB server

Download and install the InfluxDB server. This article uses InfluxDB 1.7.

Configure the EMQ X server

The configuration files related to EMQ XMagi InfluxDB installed through RPM are located in / etc/emqx/plugins/emqx_backend_influxdb.conf, and the InfluxDB plug-in only supports message storage for functional positioning.

Configure the connection address and connection pool size:

# # InfluxDB UDP Server## only uses UDP to access backend.influxdb.pool1.server = 127.0.0.1 backend.influxdb.pool1.server 8089 cards # InfluxDB Pool Sizebackend.influxdb.pool1.pool_size = 5 cycles # Whether or not set timestamp when encoding InfluxDB linebackend.influxdb.pool1.set_timestamp = trues

* * parameters of InfluxDB Backend message storage rules: * *

Through the topic filter, you can set the topic in which messages need to be stored. The pool parameter distinguishes multiple data sources:

# # Store Publish Messagebackend.influxdb.hook.message.publish.1 = {"topic": "#", "action": {"function": "on_message_publish"}, "pool": "pool1"}

Start the plug-in, the way to start the plug-in is command line and console, users can choose one of them.

Message template

Because MQTT Message cannot write directly to InfluxDB, InfluxDB Backend provides an emqx_backend_influxdb.tmpl template file to convert MQTT Message to DataPoint that can be written to InfluxDB.

The message template feature requires a restart of EMQ X to apply the changes.

The tmpl file is located in data/templates/emqx_backend_influxdb_example.tmpl. Using json format, you can define different Template for different Topic, similar to:

{"timestamp": "measurement":, "tags": {:}, "fields": {:}}

Among them, measurement and fields are required, tags and timestamp are optional. You can extract a variable named key through placeholders such as $key. The supported variables are as follows:

Qos: message QoS

Form: publisher information

Topic: publish topic

Timestamp: timestamp

Any variable in the body of a payload.*: JSON message, such as {"data": [{"temp": 1}]} can be extracted using ["$payload", "data", "temp"].

The template for this example is as follows:

{"sample": {"measurement": "$topic", "tags": {"host": ["$payload", "data", "$0", "host"], "region": ["$payload", "data", "$0", "region"], "qos": "$qos", "from": "$from"} "fields": {"temperature": ["$payload", "data", "$0", "temp"]}, "timestamp": "$timestamp"}}

When a MQTT Message with Topic of "sample" has the following Payload:

{"data": [{"temp": 1, "host": "serverA", "region": "hangzhou"}, {"temp": 2, "host": "serverB", "region": "ningbo"}]}

Backend converts MQTT Message to:

[{"measurement": "sample", "tags": {"from": "mqttjs_ebcc36079a", "host": "serverA", "qos": "0", "region": "hangzhou"}, "fields": {"temperature": "1"}, "timestamp": "1560743513626681000"}, {"measurement": "sample" "tags": {"from": "mqttjs_ebcc36079a", "host": "serverB", "qos": "0", "region": "ningbo"}, "fields": {"temperature": "2"}, "timestamp": "1560743513626681000"}] use examples

In the WebSocket page of the EMQ X Management console, publish the message in the above format to the sample topic, and the message will be parsed and stored in the measurement corresponding to the InfluxDB udp database.

At this point, I believe you have a deeper understanding of "how to store relevant information through InfluxDB". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report