两者区别:
1.x 版本使用 influxQL 查询语言,2.x 和 1.8+(beta) 使用 flux 查询语法;相比V1 移除了database 和 RP,增加了bucket,
V2具有以下几个概念:
timestamp、field key、field value、field set、tag key、tag value、tag set、measurement、series、point、bucket、bucket schema、organization
新增的概念:
bucket:所有 InfluxDB 资料都存盘在一个存盘桶中,一个桶结合了数据库的概念和存盘周期(时间每个资料点仍然存在持续时间),一个桶属于一个组织
bucket schema:具有明确的schema-type的存盘桶需要为每个度量指定显式架构,测量包含标签、栏位和时间戳,显式模式限制了可以写入该度量的资料的形状,
organization:InfluxDB组织是一组用户的作业区,所有仪表板、任务、存盘桶和用户都属于一个组织,
本文所有代码:https://github.com/Tom-shushu/InfluxDB1.xAnd2.x-SpringBoot
新得阅读地址:http://www.zhouhong.icu/post/161
一、InfluxDB1.x Docker安装以及与Boot整合
A、docker安装InfluxDB1.x (influxdb1.8.4)
1、安装:
docker run -d --name influxdb -p 8086:8086 influxdb:1.8.4
2、查看
docker ps -a
3、进入docker的influx中
docker exec -it daf88772adc9 /bin/bash
4、直接输入influx启动
influx
5、修改账户密码
# 显示用户
SHOW USERS
# 创建用户
CREATE USER "username" WITH PASSWORD 'password'
# 赋予用户管理员权限
GRANT ALL PRIVILEGES TO username
# 创建管理员权限的用户
CREATE USER <username> WITH PASSWORD '<password>' WITH ALL PRIVILEGES
# 修改用户密码
SET PASSWORD FOR username = 'password'
# 撤消权限
REVOKE ALL ON mydb FROM username
# 查看权限
SHOW GRANTS FOR username
# 洗掉用户
DROP USER "username"
6、在组态档启用认证
默认情况下,influxdb的组态档是禁用认证策略的,所以需要修改设定一下,
编辑组态档vim /etc/influxdb/influxdb.conf,把 [http] 下的 auth-enabled 选项设定为 true
7、设定保存策略(多长时间之前的资料需要洗掉)---默认为 autogen 永久不洗掉
a、查看数据库的保存策略
show retention policies on 数据库名
例子:
# 选择使用telegraf数据库
> use influx_test;
Using database influx_test
# 查询资料保存策略
> show retention policies on influx_test
name duration shardGroupDuration replicaN default
---- -------- ------------------ -------- -------
autogen 0s 168h0m0s 1 true
name 策略名称:默认autogen
duration 持续时间: 0s 代表无限制
shardGroupDuration shardGroup资料存盘时间:shardGroup是InfluxDB的一个基本存盘结构, 应该大于这个时间的资料在查询效率上应该有所降低,
replicaN 副本个数:1 代表只有一个副本
default 是否默认策略:true 代表设定为该数据库的默认策略
b、设定保存策略
# 新建一个策略
CREATE RETENTION POLICY "策略名称" ON 数据库名 DURATION 时长 REPLICATION 副本个数;
# 新建一个策略并且直接设定为默认策略
CREATE RETENTION POLICY "策略名称" ON 数据库名 DURATION 时长 REPLICATION 副本个数 DEFAULT;
例子:
# 创建新的默认策略role_01保留资料时长1小时
> CREATE RETENTION POLICY "1hour" ON influx_test DURATION 1h REPLICATION 1 DEFAULT;
c、修改保存策略
ALTER RETENTION POLICY "策略名称" ON "数据库名" DURATION 时长
ALTER RETENTION POLICY "策略名称" ON "数据库名" DURATION 时长 DEFAULT
d、洗掉保存策略
drop retention POLICY "策略名" ON "数据库名"
8、使用桌面可视化工具连接数据库
工具链接:https://github.com/CymaticLabs/InfluxDBStudio/releases/download/v0.2.0-beta.1/InfluxDBStudio-0.2.0.zip
如果刚才没有设定密码,这里可以不需要填写密码,如果有账号密码则需要勾上下面的Use SSL
连接成功后如下:
B、InfluxDB1.x与Spring整合(只列举部分代码,后面会放上整个项目的GitHub地址)
整个项目结构如下:
1、引入依赖 (其他依赖未显示全,后面会放上整个项目的GitHub地址)
<dependency>
<groupId>com.influxdb</groupId>
<artifactId>influxdb-client-java</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.influxdb</groupId>
<artifactId>influxdb-java</artifactId>
<version>2.20</version>
</dependency>
2、新建yml档案
influx:
url: 'http://xxx.xx.xxx.xx:8086'
password: 'password'
username: 'username'
3、连接配置 InfluxDBConfig
@Data
@Configuration
@ConfigurationProperties(prefix = "influx")
public class InfluxDBConfig {
private String url;
private String username;
private String password;
/**
* description: 用于查询
* date: 2022/1/20 23:11
* author: zhouhong
* @param * @param null
* @return
*/
@Bean(destroyMethod = "close")
public InfluxDB influxDBClient(){
return InfluxDBFactory.connect(this.url, this.username, this.password);
}
/**
* description: 用于写入
* date: 2022/1/20 23:12
* author: zhouhong
* @param * @param null
* @return
*/
@Bean(name = "influxDbWriteApi",destroyMethod = "close")
public WriteApi influxDbWriteApi(){
InfluxDBClient influxDBClient = InfluxDBClientFactory.createV1(this.url, this.username,
this.password.toCharArray(), "influx_test", "autogen");
return influxDBClient.getWriteApi();
}
}
4、封装用于查询的方法
@Component
public class InfluxUtil {
/**
* description: 通用查询
* date: 2022/1/20 23:13
* author: zhouhong
* @param * @param null
* @return
*/
public QueryResult query(String command, String database, InfluxDB influxDB) {
Query query = new Query(command, database);
return influxDB.query(query);
}
}
5、新建需要写入的资料的物体类、需要回传的类(省略,具体参考github示例)InsertParams.java InfluxResult.java
6、新建server层和impl实作类
InfluxServiceImpl.java 如下:
/**
* description: 时序数据库Impl
* date: 2022/1/16 20:47
* author: zhouhong
*/
@Service
@Slf4j
public class InfluxServiceImpl implements InfluxService {
@Resource(name = "influxDbWriteApi")
private WriteApi influxDbWriteApi;
@Resource(name = "influxDBClient")
private InfluxDB influxDBClient;
@Autowired
private InfluxUtil influxUtil;
@Override
public void insert(InsertParams insertParams) {
influxDbWriteApi.writeMeasurement(WritePrecision.MS, insertParams);
}
@Override
public Object queryAll(InsertParams insertParams) {
List<InfluxResult> list = new ArrayList<>();
InfluxResult influxResult = new InfluxResult();
String sql = "SELECT * FROM \"influx_test\" WHERE time > '2022-01-16' tz('Asia/Shanghai')";
QueryResult queryResult = influxUtil.query(sql, "influx_test", influxDBClient);
queryResult.getResults().get(0).getSeries().get(0).getValues().forEach(item -> {
influxResult.setTime(item.get(0).toString());
influxResult.setCurrent(item.get(1).toString());
influxResult.setEnergyUsed(item.get(2).toString());
influxResult.setPower(item.get(3).toString());
influxResult.setVoltage(item.get(4).toString());
list.add(influxResult);
});
return list;
}
@Override
public Object querySumByOneDay(InsertParams insertParams) {
String sql = "SELECT SUM(voltage) FROM \"influx_test\" WHERE time > '2022-01-18' GROUP BY time(1d) tz('Asia/Shanghai')";
QueryResult queryResult = influxUtil.query(sql, "influx_test", influxDBClient);
return queryResult.getResults().get(0).getSeries().get(0);
}
}
7、controller层 InfluxDbController.java(回传结果是封装过后的,详情见github示例)
@RestController
public class InfluxDbController {
@Autowired
private InfluxService influxService;
/**
* description: 时序数据库插入测验
* date: 2022/1/16 23:00
* author: zhouhong
* @param * @param null
* @return
*/
@PostMapping("/influxdb/insert")
public ResponseData insert(@RequestBody InsertParams insertParams) {
influxService.insert(insertParams);
return new SuccessResponseData();
}
/**
* description: 时序数据库查询全部资料测验
* date: 2022/1/16 23:00
* author: zhouhong
* @param * @param null
* @return
*/
@PostMapping("/influxdb/queryAll")
public ResponseData query(@RequestBody InsertParams insertParams) {
return new SuccessResponseData(influxService.queryAll(insertParams));
}
/**
* description: 时序数据库按天查询当前电压总和测验
* date: 2022/1/16 23:00
* author: zhouhong
* @param * @param null
* @return
*/
@PostMapping("/influxdb/queryByOneDay")
public ResponseData queryByOneDay(@RequestBody InsertParams insertParams) {
return new SuccessResponseData(influxService.querySumByOneDay(insertParams));
}
}
8、PostMan测验(注意需要先新建一个 数据库---influx_test)
8.1 插入测验 localhost:9998/influxdb/insert
入参:
{
"energyUsed":243.78,
"power":54.50,
"current":783.34,
"voltage":44.09
}
回传:
{
"success": true,
"code": 200,
"message": "请求成功",
"localizedMsg": "请求成功",
"data": null
}
8.2、查询全部(注意,这里回传结果我封装了一下)localhost:9998/influxdb/queryAll
入参:
{
}
回传:
{
"success": true,
"code": 200,
"message": "请求成功",
"localizedMsg": "请求成功",
"data": [
{
"energyUsed": "243.78",
"power": "54.5",
"current": "783.34",
"voltage": "44.09",
"time": "2022-01-20T23:44:00.626+08:00"
},
{
"energyUsed": "243.78",
"power": "54.5",
"current": "783.34",
"voltage": "44.09",
"time": "
0 评论