In addition to the most commonly used relational databases and caches, we have shown examples of how to configure and use MongoDB and LDAP storage in Spring Boot. Next, let’s move on to another special database: the use of InfluxDB in Spring Boot.

InfluxDB profile

What is a sequential database? Time series database. Time series database is mainly used to process the data with time label (change in time order, that is, time serialization). The data with time label is also called time series data. Time series data are mainly collected and generated by various types of real-time monitoring, inspection and analysis equipment in electric power industry and chemical industry. The typical characteristics of these industrial data are as follows: Fast frequency (each monitoring point in one second can produce multiple data), heavily dependent on the acquisition time (only each data are required in the time), measuring point more informative (conventional have tens of thousands of monitoring real time monitoring system, monitoring data every second, every day to produce dozens of GB of data volume). Although relational database can also store data based on time series, due to the disadvantage of storage structure, these data can not efficiently realize the high-frequency storage and query statistics, so a kind of database specifically for time series storage and optimization was born to meet the requirements of higher efficiency. — Reference: Baidu Baike: Timing database

InfluxDB is a popular open source timing database (www.influxdata.com/). It is commonly used for time-related high-frequency data recording and statistical needs, such as storage and query of monitoring data.

Before moving on, let’s take a look at some important terms used in the InfluxDB.

  • -Leonard: The database.
  • Measurement: similar to tables in relational databases
  • Points: Similar to a row in a relational database

A Point consists of three parts:

  • Time: indicates the time stamp
  • Fields: Indicates the recorded value
  • Tags: Attributes of an index

Began to try

After learning about what a sequential database is and some of the basic concepts of InfluxDB, let’s take a closer look at the basic configuration, data organization, and write operations of InfluxDB with a simple case study of regularly reporting monitoring data!

Step 1: Create a basic Spring Boot project (see this article: Quick Start 1 if you don’t already)

Step 2: Introduce an official SDK with POM.xml

<dependency>
    <groupId>org.influxdb</groupId>
    <artifactId>influxdb-java</artifactId>
</dependency>
Copy the code

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

Step 3: Configure the influxDB information to be connected

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

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

Note: Although not supported by Spring Data, spring Boot 2.x also implements automatic configuration of InfluxDB, so you only need to write the configuration information to use it. Specific configuration properties can view the source code: org. Springframework. Boot. Autoconfigure. Influx. InfluxDbProperties.

Step 4: Create a scheduled task, simulate the reported data, and write the InfluxDB

@Service
@AllArgsConstructor
@Slf4j
public class Monitor {

    private InfluxDB influxDB;

    @Scheduled(fixedRate = 5000)
    public void writeQPS(a) {
        // Simulate statistics to be reported
        int count = (int) (Math.random() * 100);

        Point point = Point.measurement("ApiQPS")     / / ApiQPS table
                .tag("url"."/hello")  / / url field
                .addField("count", count)        // Statistics
                .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)  / / time
                .build();

        // Write data to test library
        influxDB.write("test"."autogen", point);

        log.info("Report statistics:"+ count); }}Copy the code

The validation test

Step 1: Start the InfluxDB and prepare the database for use using the InfluxDB command line.

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

Step 2: Start the Spring Boot application. Under the effect of scheduled tasks, we will see a log similar to the following:

The 01:52:47 2021-08-03. 94110-732 the INFO [main] C.D.C. hapter63. 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 : Reporting statistics: 25 01:52:52 2021-08-03. 94110-736 the INFO] [the scheduling - 1 com. Didispace. Chapter63. Monitor: Report statistics: 30 2021-08-03 01:52:57. 94110-737 the INFO] [the scheduling - 1 com. Didispace. Chapter63. Monitor: Reporting statistics: 38 01:53:02 2021-08-03. 94110-739 the INFO] [the scheduling - 1 com. Didispace. Chapter63. Monitor: Report statistics: 51 01:53:07 2021-08-03. 94110-739 the INFO] [the scheduling - 1 com. Didispace. Chapter63. Monitor: reporting statistics: 31Copy the code

Step 3: Check whether the data already exists in InfluxDB using the InfluxDB command

> 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 the code

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

Well, that’s the end of today’s tutorial, so try it yourself! Remember to pay attention to me, learn not to get lost! We’ll talk more about how to display this temporal data later! Spring Boot 2.x Basic Tutorial , welcome to collect and forward! If you encounter difficulties in learning? You can join our Spring technology exchange group, participate in exchange and discussion, better learning and progress!

Code sample

The complete project for this article can be viewed in the chapter6-3 directory in the repository below:

  • Github:github.com/dyc87112/Sp…
  • Gitee:gitee.com/didispace/S…

If you found this article good, welcomeStarSupport, your attention is my motivation!

Welcome to pay attention to my public account: program ape DD, share the outside can not see the dry goods and thinking!