influx db的基本操作
[root@test_cast ~]# influx
Connected to http://localhost:8086 version 1.5.2
InfluxDB shell version: 1.5.2
>
>
> show databases;
name: databases
name
----
_internal
bdcloud
> CREATE DATABASE bdcloud //创建数据库语句
> //以下语句为创建存储策略DURATION 30h:代表存储30个小时的数据,REPLICATION 2:备份数为2个,SHARD DURATION 30m :代表每存储30m的数据保存为一个文件
> CREATE RETENTION POLICY logmonitor ON bdcloud DURATION 30h REPLICATION 2 SHARD DURATION 30m DEFAULT
> //插入一条数据
> INSERT cpu,host=serverA,region=us_west idle=0.64,happydevop=false,uptimesecs=123456789i
>
> select * from cpu
name: cpu
time happydevop host idle region uptimesecs
---- ---------- ---- ---- ------ ----------
2017-06-20T15:32:46.202829088Z false serverA 0.64 us_west 123456789
先引入pom依赖
<!-- influxdb-->
<dependency>
<groupId>org.influxdb</groupId>
<artifactId>influxdb-java</artifactId>
<version>2.15</version>
</dependency>
在application.xml中配置influxdb 的连接参数
spring:
influx:
url: http://10.212.130.46:8086
database: bdcloud
配置influxdb的configuretion
package com.bdcloud.config;
import com.bdcloud.utils.InfluxDbUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class InfluxDbConfig {
@Value("${spring.influx.url:''}")
private String influxDBUrl;
@Value("${spring.influx.user:''}")
private String userName;
@Value("${spring.influx.password:''}")
private String password;
@Value("${spring.influx.database:''}")
private String database;
@Bean
public InfluxDbUtils influxDbUtils() {
return new InfluxDbUtils(userName, password, influxDBUrl, database, "logmonitor");
}
}
配置influxdb的工具类
package com.bdcloud.utils;
import lombok.Data;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Data
public class InfluxDbUtils {
private String userName;
private String password;
private String url;
public String database;
private String retentionPolicy;
// InfluxDB实例
private InfluxDB influxDB;
Logger logger = LoggerFactory.getLogger(InfluxDbUtils.class);
// 数据保存策略
public static String policyNamePix = "logmonitor";
public InfluxDbUtils(String userName, String password, String url, String database,
String retentionPolicy) {
this.userName = userName;
this.password = password;
this.url = url;
this.database = database;
this.retentionPolicy = retentionPolicy == null || "".equals(retentionPolicy) ? "autogen" : retentionPolicy;
this.influxDB = influxDbBuild();
}
/**
* 连接数据库 ,若不存在则创建
*
* @return influxDb实例
*/
private InfluxDB influxDbBuild() {
if (influxDB == null) {
influxDB = InfluxDBFactory.connect(url, userName, password);
}
try {
createDB(database);
influxDB.setDatabase(database);
} catch (Exception e) {
logger.error("create influx db failed, error: {}", e.getMessage());
} finally {
influxDB.setRetentionPolicy(retentionPolicy);
}
influxDB.setLogLevel(InfluxDB.LogLevel.BASIC);
return influxDB;
}
/****
* 创建数据库
* @param database
*/
private void createDB(String database) {
influxDB.query(new Query("CREATE DATABASE " + database));
}
}
调用接口存入数据
@KafkaListener(topics = {"IM_MONITOR"})
private void storeMonitorCast(ConsumerRecord<String, String> record) throws Exception{
String value = record.value();
Map<String,Object> map = (Map<String, Object>) JSONObject.parse(value);
InfluxDB influxDB = influxDbUtils.getInfluxDB();
//IM_MONITOR为表名
influxDB.write(Point.measurement("IM_MONITOR")
.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
.addField("proc", (String) map.get("proc"))
.addField("hostName", (String) map.get("hostName"))
.addField("content", String.valueOf(map.get("content")))
.addField("utime",(String) map.get("time"))
.addField("type",(String) map.get("type"))
.build());
}
其中measurement是配置表名的参数
InfluxDB实战
本文详细介绍InfluxDB的基本操作,包括数据库创建、存储策略设定、数据插入及查询等关键步骤,并提供Spring Boot集成InfluxDB的配置示例,以及通过Kafka监听器存储监控数据的方法。
2302





