Home > Java > javaTutorial > Teach you one trick to use time series database in Spring Boot

Teach you one trick to use time series database in Spring Boot

醉折花枝作酒筹
Release: 2021-08-03 17:47:32
forward
2956 people have browsed it

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

InfluxDB Introduction

What is a time series database? The full name is time series database. Time series database is mainly used to process data with time tags (changing in the order of time, that is, time serialization). Data with time tags 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 (one for each monitoring point) Multiple pieces of data can be generated within seconds), it is heavily dependent on the collection time (each piece of data is required to correspond to a unique time), and there are many measuring points and a large amount of information (conventional real-time monitoring systems have thousands of monitoring points. Data is generated every second, and tens of gigabytes of data are generated every day). Although relational databases can also store data based on time series, due to the disadvantages of the storage structure, these data cannot efficiently achieve high-frequency storage and query statistics. Therefore, a new method was born specifically for time series storage and optimization. database to meet higher efficiency requirements.

InfluxDB is currently a popular open source time series database (official website address: https://www.influxdata.com/). Our more common usage scenarios are some high-frequency data records and statistics related to time. Need, for example: monitoring data storage and query.

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

  • database: database

  • measurement: similar to a table (table) in a relational database

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

Among them, a Point consists of three parts:

  • time: timestamp

  • ##fields: recorded value

  • tags: Index attributes

Try it yourself

After understanding what a time series database is and some basic concepts of InfluxDB, let’s go through a A simple small case of regularly reporting monitoring data to further understand the basic configuration, data organization and writing operations of InfluxDB!

Step One: Create a basic Spring Boot project (if you don’t know how yet, you can refer to this article: Quick Start 1)

Step Two: Introduce it in pom.xml influx's official SDK

<dependency>
    <groupId>org.influxdb</groupId>
    <artifactId>influxdb-java</artifactId>
</dependency>
Copy after login

Note: Because the Spring Boot 2.x version's parent maintains the SDK version of InfluxDB, there is no need to manually specify the version information. If the Spring Boot version used is older, the version information may be missing and needs to be written manually.

Step 3: Configure the influxdb information to be connected

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

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

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

Step 4: Create a scheduled 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);
    }

}
Copy after login

Test verification

Step 1: Start InfluxDB and prepare it through the command line The database to be used, the main commands involved are as follows;

Enter InfluxDB:

$ influx
Copy after login

Query the currently existing database:

> show databases
Copy after login

Create a database (note the database name and the above Java code The first parameter of write is the same):

> create database "test"
Copy after login

Step 2: Start the Spring Boot application. Under the action of the scheduled 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
Copy after login

Step 3: Use the command to 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
Copy after login

You can see that the same data as in the log already exists.

Okay, today’s tutorial ends here, remember to try it yourself!

Recommended related video tutorials:

Java video tutorial

The above is the detailed content of Teach you one trick to use time series database in Spring Boot. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template