java使用influxDB数据库的详细代码
作者:java开发鼻祖 时间:2023-03-04 05:22:27
本文实例为大家分享了java使用influxDB数据库的具体代码,供大家参考,具体内容如下
1.pom.xml中导入jar包依赖
<!-- 引入influxdb依赖 -->
<dependency>
<groupId>org.influxdb</groupId>
<artifactId>influxdb-java</artifactId>
<version>2.5</version>
</dependency>
2.编写influxDB工具类:
package com.hontye.parameter.util;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.Point;
import org.influxdb.dto.Point.Builder;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;
import java.util.Map;
/**
* 时序数据库 InfluxDB 连接
* @author Dai_LW
*
*/
public class InfluxDbUtil {
private static String openurl = "http://127.0.0.1:8086";//连接地址
private static String username = "root";//用户名
private static String password = "root";//密码
private static String database = "PARAMTER_DB";//数据库
private static String measurement = "tw_parameter_tb";//表名
private InfluxDB influxDB;
public InfluxDbUtil(String username, String password, String openurl, String database){
this.username = username;
this.password = password;
this.openurl = openurl;
this.database = database;
}
public static InfluxDbUtil setUp(){
//创建 连接
InfluxDbUtil influxDbUtil = new InfluxDbUtil(username, password, openurl, database);
influxDbUtil.influxDbBuild();
influxDbUtil.createRetentionPolicy();
// influxDB.deleteDB(database);
// influxDB.createDB(database);
return influxDbUtil;
}
/**连接时序数据库;获得InfluxDB**/
public InfluxDB influxDbBuild(){
if(influxDB == null){
influxDB = InfluxDBFactory.connect(openurl, username, password);
influxDB.createDatabase(database);
}
return influxDB;
}
/**
* 设置数据保存策略
* defalut 策略名 /database 数据库名/ 30d 数据保存时限30天/ 1 副本个数为1/ 结尾DEFAULT 表示 设为默认的策略
*/
public void createRetentionPolicy(){
String command = String.format("CREATE RETENTION POLICY \"%s\" ON \"%s\" DURATION %s REPLICATION %s DEFAULT",
"defalut", database, "30d", 1);
this.query(command);
}
/**
* 查询
* @param command 查询语句
* @return
*/
public QueryResult query(String command){
return influxDB.query(new Query(command, database));
}
/**
* 插入
* @param tags 标签
* @param fields 字段
*/
public void insert(Map<String, String> tags, Map<String, Object> fields){
Builder builder = Point.measurement(measurement);
builder.tag(tags);
builder.fields(fields);
influxDB.write(database, "", builder.build());
}
/**
* 删除
* @param command 删除语句
* @return 返回错误信息
*/
public String deleteMeasurementData(String command){
QueryResult result = influxDB.query(new Query(command, database));
return result.getError();
}
/**
* 创建数据库
* @param dbName
*/
public void createDB(String dbName){
influxDB.createDatabase(dbName);
}
/**
* 删除数据库
* @param dbName
*/
public void deleteDB(String dbName){
influxDB.deleteDatabase(dbName);
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getOpenurl() {
return openurl;
}
public void setOpenurl(String openurl) {
this.openurl = openurl;
}
public void setDatabase(String database) {
this.database = database;
}
}
3.存值
public class QuatyServiceImpl{
private InfluxDbUtil influxDB;
public void intoDb() {
influxDB = InfluxDbUtil.setUp();
Map<String, String> tags = new HashMap<>();
Map<String, Object> fields = new HashMap<>();
tags.put("TAG_NAME",info.getKey());
fields.put("TAG_VALUE",code);
fields.put("TIMAMPEST", df.format(new Date()));
influxDB.insert(tags, fields);
}
}
来源:https://blog.csdn.net/qq_36004521/article/details/80101608
标签:java,influxDB,数据库
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java8时间接口LocalDateTime详细用法
2023-11-25 09:56:49
jdbc+jsp实现简单员工管理系统
2021-08-31 23:08:15
OpenCV实现直线检测并消除
2023-07-12 20:44:36
![](https://img.aspxhome.com/file/2023/3/92493_0s.jpg)
解决Mybatis中foreach嵌套使用if标签对象取值的问题
2023-11-23 06:02:02
重温C# clr 笔记总结
2023-06-22 02:24:30
java快速生成数据库文档详情
2023-11-10 05:25:20
![](https://img.aspxhome.com/file/2023/3/59363_0s.png)
java实现分页显示效果
2021-12-29 20:17:43
![](https://img.aspxhome.com/file/2023/8/61378_0s.jpg)
Android解决所有双击优化的问题
2023-07-27 09:07:03
![](https://img.aspxhome.com/file/2023/0/86010_0s.png)
java实现随机数生成器
2023-06-15 04:26:53
![](https://img.aspxhome.com/file/2023/6/61776_0s.png)
RestTemplate自定义请求失败异常处理示例解析
2021-12-03 22:13:17
![](https://img.aspxhome.com/file/2023/0/60700_0s.png)
Java后台线程操作示例【守护线程】
2023-11-25 01:35:44
完整的医院就诊挂号系统基于Spring MVC + Spring + MyBatis实现
2022-06-03 18:07:05
![](https://img.aspxhome.com/file/2023/0/61400_0s.png)
JPA Specification常用查询+排序实例
2023-11-23 04:56:32
![](https://img.aspxhome.com/file/2023/3/59423_0s.png)
SpringBoot Profile多环境配置方式
2023-12-14 01:44:24
![](https://img.aspxhome.com/file/2023/8/60898_0s.png)
Android 动画之TranslateAnimation应用详解
2023-06-27 06:17:59
![](https://img.aspxhome.com/file/2023/1/82411_0s.png)
SpringBoot详细讲解yaml配置文件的用法
2022-04-21 16:29:33
![](https://img.aspxhome.com/file/2023/0/61030_0s.png)
Java看完秒懂版熔断和降级的关系
2023-11-06 01:12:13
![](https://img.aspxhome.com/file/2023/9/58919_0s.jpg)
c# 在windows中操作IIS设置FTP服务器的示例
2023-07-18 06:13:01
![](https://img.aspxhome.com/file/2023/6/66116_0s.jpg)
maven工程中jar包瘦身的五种方法
2023-11-04 05:01:54
go打包aar及flutter调用aar流程详解
2023-06-24 17:39:21
![](https://img.aspxhome.com/file/2023/4/87634_0s.png)