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.
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
<dependency> <groupId>org.influxdb</groupId> <artifactId>influxdb-java</artifactId> </dependency>
spring.influx.url=http://localhost:8086 spring.influx.user=admin spring.influx.password=
@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); } }
$ influx
> show databases
> create database "test"
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
> 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
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!