版权声明:欢迎大家转载,转载请注明原作者和原地址链接,谢谢 https://blog.csdn.net/tiandixuanwuliang/article/details/82888132
influxDB学习
InfluxDB工具类
package com.wllfengshu.utils;
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 tiandixuanwu
*
*/
public class InfluxdbUtil {
//以下配置信息可以从配置文件或者环境变量中读
private static String openurl = "http://192.168.40.254:8086";//连接地址
private static String username = "user";//用户名(默认是user/admin)
private static String password = "admin";//密码
private static String database = "PARAMTER_DB";//数据库
private static InfluxdbUtil influxdbUtil = null;
private static InfluxDB influxDB = null;
private InfluxdbUtil(){}
static{
try {
//创建连接
influxDB = InfluxDBFactory.connect(openurl, username, password);
//设置数据保存策略 defalut 策略名 /database 数据库名/ 30d 数据保存时限30天/ 1 副本个数为1/ 结尾DEFAULT表示设为默认的策略
String command = String.format("CREATE RETENTION POLICY \"%s\" ON \"%s\" DURATION %s REPLICATION %s DEFAULT","defalut", database, "30d", 1);
influxDB.query(new Query(command, database));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 或者工具类的实例
* @return
* @throws
*/
public static InfluxdbUtil getInfluxdbUtil() {
if (influxdbUtil == null) {
influxdbUtil=new InfluxdbUtil();
}
return influxdbUtil;
}
/**
* 查询
* @param command 查询语句
* @return
*/
public QueryResult query(String command) {
return influxDB.query(new Query(command, database));
}
/**
* 插入(influxdb不需要创建表,直接在插入时指定表,无则创建,有则插入)
* @param tags 标签
* @param fields 字段
* @param measurement 表名
*/
public void insert(Map<String, String> tags, Map<String, Object> fields,String measurement) {
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);
}
}
测试类
package com.wllfengshu.influx;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.influxdb.dto.QueryResult;
import org.influxdb.dto.QueryResult.Result;
import org.influxdb.dto.QueryResult.Series;
import org.junit.Before;
import org.junit.Test;
import com.wllfengshu.utils.InfluxdbUtil;
/**
* @author tiandixuanwu
*/
public class TestMain {
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private InfluxdbUtil influxdbUtil = null;
@Before
public void init(){
influxdbUtil = InfluxdbUtil.getInfluxdbUtil();
}
@Test
public void createDB(){
//创建数据库
influxdbUtil.createDB("PARAMTER_DB");
}
@Test
public void testInsert(){
//测试插入数据
Map<String, String> tags = new HashMap<>();
Map<String, Object> fields = new HashMap<>();
tags.put("TAG_NAME","name");
fields.put("TAG_VALUE","张三");//注意,这里其实应该是FIELD_VALUE
fields.put("TIMAMPEST", sdf.format(new Date()));
influxdbUtil.insert(tags, fields, "test");
}
@Test
public void testQuery(){
//读取数据
QueryResult queryResult = influxdbUtil.query("select * from test");
for (Result result:queryResult.getResults()) {
List<Series> series = result.getSeries();
for (Series serie : series) {
System.out.println(serie.getName());
System.out.println(serie.getColumns());
System.out.println(serie.getValues());
}
}
}
}
pox.xml文件配置
<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.wllfengshu</groupId>
<artifactId>testInfluxdb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.influxdb</groupId>
<artifactId>influxdb-java</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
效果图
(1)以下为程序输出的结果:
(2)上面的数据在命令行中看到的结果:
注:本人使用的是maven,大家也可以不使用,自己引入jar,下载地址:https://download.csdn.net/download/tiandixuanwuliang/10693992
注:influxdb安装参考教程:https://www.cnblogs.com/mafeng/p/6848166.html
注:参考网址:
https://blog.csdn.net/dailuwen/article/details/73741297
https://www.linuxdaxue.com/influxdb-basic-operation.html