1 安装
cat <<EOF | sudo tee /etc/yum.repos.d/influxdb.repo
[influxdb]
name = InfluxDB Repository - RHEL \$releasever
baseurl = https://repos.influxdata.com/rhel/\$releasever/\$basearch/stable
enabled = 1
gpgcheck = 1
gpgkey = https://repos.influxdata.com/influxdb.key
EOF
yum install influxdb
sudo systemctl start influxd
firewall-cmd --zone=public --add-port=8086/tcp --permanent
systemctl restart firewalld
2 使用
influx
CREATE USER root WITH PASSWORD '123456' WITH ALL PRIVILEGES
SHOW USERS
CREATE DATABASE test
SHOW DATABASES
USE test
INSERT cpu,host=192.168.1.1 load=0.1,usage=0.2
SELECT * FROM "cpu"
SELECT "host","load","usage" FROM "cpu"
SELECT "host","load","usage" FROM "cpu" WHERE "host" = '192.168.1.1'
SELECT "host","load","usage" FROM "cpu" WHERE "usage" > 0.1
CREATE DATABASE "db_name"
DROP DATABASE "db_name"
SHOW MEASUREMENTS
DROP MEASUREMENT "t_name"
3 在springboot3中的使用
3.1 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gzz</groupId>
<artifactId>67-boot-hello</artifactId>
<version>1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.0</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>17</java.version>
<skipTests>true</skipTests>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.influxdb</groupId>
<artifactId>influxdb-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.2 application.yml
spring:
influx:
url: http://192.168.217.128:8086
user: root
password: 123456
3.3 Config.java
package com.gzz.config;
import org.influxdb.InfluxDB;
import org.influxdb.impl.InfluxDBMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Config {
@Bean
public InfluxDBMapper influxDBMapper(InfluxDB influxDB) {
return new InfluxDBMapper(influxDB);
}
}
3.4 User .java
package com.gzz.user;
import org.influxdb.annotation.Column;
import org.influxdb.annotation.Measurement;
import lombok.Data;
@Data
@Measurement(name = "t_user", database = "test")
public class User {
@Column(name = "time")
private String time;
@Column(name = "user_id",tag = true)
private Integer userId;
@Column(name = "status1")
private Integer status1;
@Column(name = "status2")
private Integer status2;
@Column(name = "status3")
private Integer status3;
@Column(name = "status4")
private Integer status4;
@Column(name = "status5")
private Integer status5;
@Column(name = "status6")
private Integer status6;
}
3.5 UserVo.java 有点奇怪 但这样好使
package com.gzz.user;
import org.influxdb.annotation.Column;
import org.influxdb.annotation.Measurement;
import lombok.Data;
@Data
@Measurement(name = "t_user", database = "test")
public class UserVo {
@Column(name = "time")
private String time;
@Column(name = "user_id",tag = true)
private String userId;
@Column(name = "status1")
private String status1;
@Column(name = "status2")
private String status2;
@Column(name = "status3")
private String status3;
@Column(name = "status4")
private String status4;
@Column(name = "status5")
private String status5;
@Column(name = "status6")
private String status6;
}
3.6 UserDao .java
package com.gzz.user;
import java.util.List;
import org.influxdb.annotation.Measurement;
import org.influxdb.dto.Query;
import org.influxdb.impl.InfluxDBMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Repository
public class UserDao {
@Autowired
private InfluxDBMapper influxDBMapper;
private static final String TABLE = User.class.getAnnotation(Measurement.class).name();
private static final String DATABASE = User.class.getAnnotation(Measurement.class).database();
public void selectAll() {
StringBuilder sql = new StringBuilder("select * from " + TABLE);
List<UserVo> records = influxDBMapper.query(new Query(sql.toString(), DATABASE), UserVo.class);
records.forEach(i -> log.info("record={}", i));
}
public void save(User user) {
influxDBMapper.save(user);
}
}
3.7 Application .java
package com.gzz;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.gzz.user.User;
import com.gzz.user.UserDao;
import jakarta.annotation.PostConstruct;
/**
* @author 高振中
* @date 2023-06-18 00:16:57
**/
@SpringBootApplication
public class Application {
public static void main(String[] args) throws IOException {
SpringApplication.run(Application.class, args);
}
@Autowired
private UserDao userDao;
@PostConstruct
public void test() {
User user = new User();
user.setUserId(2);
user.setStatus1(3);
user.setStatus2(5);
user.setStatus3(3);
user.setStatus4(4);
user.setStatus5(7);
user.setStatus6(6);
userDao.save(user);
userDao.selectAll();
}
}