1

In addition to the most commonly used relational databases and caches, we have previously introduced how to configure and use MongoDB , LDAP these storage cases in Spring Boot. Next, we continue to introduce another special database: the use of the time series database InfluxDB in Spring Boot.

Introduction to InfluxDB

What is a time series database? The full name is a time series database. Time series database is mainly used to process data with time labels (changes in the order of time, that is, time serialization). Data with time labels is also called time series data.
Time series data is mainly collected and generated by various types of real-time monitoring, inspection and analysis equipment in the power industry, chemical industry, etc. The typical characteristics of these industrial data are: fast generation frequency (each monitoring point can generate more than one second Pieces of data), heavily dependent on the collection time (each piece of data requires a unique time), multiple measurement points and a large amount of information (the conventional real-time monitoring system has thousands of monitoring points, which are generated every second Data, generating dozens of gigabytes of data every day). Although relational databases can also store data based on time series, due to the disadvantages of the storage structure, these data cannot be efficiently stored with high frequency and query statistics. Therefore, a special storage and optimization for time series has been born. Database to meet higher efficiency requirements. - Reference: Baidu Encyclopedia: Time Series Database

InfluxDB is currently the more popular open source time series database (official website address: https://www.influxdata.com/ ). Our more common usage scenarios are some time-related high-frequency data recording and statistical needs, such as: Storage and query of monitoring data.

Before proceeding with the following hands-on links, let's first understand a few important terms in InfluxDB:

  • database: database
  • measurement: similar to a table in a relational database
  • points: similar to row (a row of data) in a relational database

Among them, a Point consists of three parts:

  • time: timestamp
  • fields: recorded values
  • tags: attributes of the index

Try it

After understanding what a time series database is and some basic concepts of InfluxDB, let's use a simple case of regularly reporting monitoring data to further understand the basic configuration, data organization and write operations of InfluxDB!

first step : Create a basic Spring Boot project (if you don’t it yet, you can refer to this article: 1610cacc3620d4 Quick Start 1 )

Step : In pom.xml introduction influx official SDK,

<dependency>
    <groupId>org.influxdb</groupId>
    <artifactId>influxdb-java</artifactId>
</dependency>

Note: Because there is an SDK version that maintains InfluxDB in the parent of Spring Boot 2.x version, there is no need to manually specify the version information. If you are using an older version of Spring Boot, the version information may be missing and you need to write it manually.

third step : Configure the influxdb information to be connected

spring.influx.url=http://localhost:8086
spring.influx.user=admin
spring.influx.password=

The three attributes represent: connection address, user name, and password. At this point, the basic configuration is complete.

Note: Although there is no spring data support, the automatic configuration of InfluxDB is also implemented in the spring boot 2.x version, so you only need to write the configuration information before you can use it. The specific configuration properties can be viewed in the source code: org.springframework.boot.autoconfigure.influx.InfluxDbProperties .

fourth step : Create a timed task, simulate reporting data, and write it to InfluxDB

@Service
@AllArgsConstructor
@Slf4j
public class Monitor {

    private InfluxDB influxDB;

    @Scheduled(fixedRate = 5000)
    public void writeQPS() {
        // 模拟要上报的统计数据
        int count = (int) (Math.random() * 100);

        Point point = Point.measurement("ApiQPS")     // ApiQPS表
                .tag("url", "/hello")  // url字段
                .addField("count", count)        // 统计数据
                .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)  // 时间
                .build();

        // 往test库写数据
        influxDB.write("test", "autogen", point);

        log.info("上报统计数据:" + count);
    }

}

Test verification

first step : Start InfluxDB, and prepare the database to be used through the command line. The main commands involved are as follows;

  • Enter InfluxDB:
$ influx
  • Query the currently existing database:
> show databases
  • Create a database (note that the database name is consistent with the first parameter of write in the above Java code):
> create database "test"

second step : Start the Spring Boot application, under the action of the timed task, we will see a log similar to the following:

2021-08-03 01:52:47.732  INFO 94110 --- [           main] c.d.chapter63.Chapter63Application       : Started Chapter63Application in 2.326 seconds (JVM running for 3.027)
2021-08-03 01:52:47.764  INFO 94110 --- [   scheduling-1] com.didispace.chapter63.Monitor          : 上报统计数据:25
2021-08-03 01:52:52.736  INFO 94110 --- [   scheduling-1] com.didispace.chapter63.Monitor          : 上报统计数据:30
2021-08-03 01:52:57.737  INFO 94110 --- [   scheduling-1] com.didispace.chapter63.Monitor          : 上报统计数据:38
2021-08-03 01:53:02.739  INFO 94110 --- [   scheduling-1] com.didispace.chapter63.Monitor          : 上报统计数据:51
2021-08-03 01:53:07.739  INFO 94110 --- [   scheduling-1] com.didispace.chapter63.Monitor          : 上报统计数据:31

third step : through the command, check whether the data already exists in InfluxDB

> select * from ApiQPS order by time desc;

name: ApiQPS
time                count url
----                ----- ---
1627926787730000000 31    /hello
1627926782730000000 51    /hello
1627926777729000000 38    /hello
1627926772727000000 30    /hello
1627926767728000000 25    /hello

As you can see, the same data as in the log already exists.

Well, today's tutorial is over here, remember to try it yourself! Remember to follow me and learn not to get lost! We will continue to introduce how to display these time series data later! This series of tutorials "Spring Boot 2.x basic tutorial" click directly! , welcome to collect and forward! What if you encounter difficulties in the learning process? You can join our Spring technology exchange group , participate in exchanges and discussions, and learn and progress better!

Code example

The complete project of this article can be viewed in the chapter6-3 directory in the following warehouse:

If you think this article is good, welcome Star support, your attention is my motivation for persistence!

Welcome to pay attention to my public account: Program Ape DD, share knowledge and thoughts that can’t be seen elsewhere

程序猿DD
2.2k 声望2.8k 粉丝

作品:《Spring Cloud微服务实战》、SpringForAll社区、OpenWrite、Youtube中文配音