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