java使用influxDB數(shù)據(jù)庫(kù)的詳細(xì)代碼
本文實(shí)例為大家分享了java使用influxDB數(shù)據(jù)庫(kù)的具體代碼,供大家參考,具體內(nèi)容如下
1.pom.xml中導(dǎo)入jar包依賴(lài)
<!-- 引入influxdb依賴(lài) --> <dependency> <groupId>org.influxdb</groupId> <artifactId>influxdb-java</artifactId> <version>2.5</version> </dependency>
2.編寫(xiě)influxDB工具類(lèi):
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;
/**
* 時(shí)序數(shù)據(jù)庫(kù) InfluxDB 連接
* @author Dai_LW
*
*/
public class InfluxDbUtil {
private static String openurl = "http://127.0.0.1:8086";//連接地址
private static String username = "root";//用戶(hù)名
private static String password = "root";//密碼
private static String database = "PARAMTER_DB";//數(shù)據(jù)庫(kù)
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(){
//創(chuàng)建 連接
InfluxDbUtil influxDbUtil = new InfluxDbUtil(username, password, openurl, database);
influxDbUtil.influxDbBuild();
influxDbUtil.createRetentionPolicy();
// influxDB.deleteDB(database);
// influxDB.createDB(database);
return influxDbUtil;
}
/**連接時(shí)序數(shù)據(jù)庫(kù);獲得InfluxDB**/
public InfluxDB influxDbBuild(){
if(influxDB == null){
influxDB = InfluxDBFactory.connect(openurl, username, password);
influxDB.createDatabase(database);
}
return influxDB;
}
/**
* 設(shè)置數(shù)據(jù)保存策略
* defalut 策略名 /database 數(shù)據(jù)庫(kù)名/ 30d 數(shù)據(jù)保存時(shí)限30天/ 1 副本個(gè)數(shù)為1/ 結(jié)尾DEFAULT 表示 設(shè)為默認(rèn)的策略
*/
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);
}
/**
* 查詢(xún)
* @param command 查詢(xún)語(yǔ)句
* @return
*/
public QueryResult query(String command){
return influxDB.query(new Query(command, database));
}
/**
* 插入
* @param tags 標(biāo)簽
* @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 刪除語(yǔ)句
* @return 返回錯(cuò)誤信息
*/
public String deleteMeasurementData(String command){
QueryResult result = influxDB.query(new Query(command, database));
return result.getError();
}
/**
* 創(chuàng)建數(shù)據(jù)庫(kù)
* @param dbName
*/
public void createDB(String dbName){
influxDB.createDatabase(dbName);
}
/**
* 刪除數(shù)據(jù)庫(kù)
* @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);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java emoji持久化mysql過(guò)程詳解
- java emoji表情存儲(chǔ)的解決方法
- 讓Java后臺(tái)MySQL數(shù)據(jù)庫(kù)能夠支持emoji表情的方法
- java數(shù)據(jù)庫(kù)唯一id生成工具類(lèi)
- sqlite數(shù)據(jù)庫(kù)的介紹與java操作sqlite的實(shí)例講解
- Java使用JDBC連接postgresql數(shù)據(jù)庫(kù)示例
- Java實(shí)現(xiàn)批量導(dǎo)入excel表格數(shù)據(jù)到數(shù)據(jù)庫(kù)中的方法
- java處理數(shù)據(jù)庫(kù)不支持的emoji表情符問(wèn)題解決
相關(guān)文章
基于SpringBoot實(shí)現(xiàn)圖片上傳及圖片回顯
本篇文章主要介紹了SpringBoot如何實(shí)現(xiàn)圖片上傳及圖片回顯,文中通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2022-08-08
Springboot yml如何獲取系統(tǒng)環(huán)境變量的值
這篇文章主要介紹了Springboot yml如何獲取系統(tǒng)環(huán)境變量的值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
Struts2學(xué)習(xí)筆記(6)-簡(jiǎn)單的數(shù)據(jù)校驗(yàn)
這篇文章主要介紹Struts2中的數(shù)據(jù)校驗(yàn),通過(guò)一個(gè)簡(jiǎn)單的例子來(lái)說(shuō)明,希望能給大家做一個(gè)參考。2016-06-06
java設(shè)計(jì)模式之代理模式(Porxy)詳解
這篇文章主要為大家詳細(xì)介紹了java設(shè)計(jì)模式之代理模式Porxy的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
基于Spring Cloud Zookeeper實(shí)現(xiàn)服務(wù)注冊(cè)與發(fā)現(xiàn)
這篇文章主要介紹了基于Spring Cloud Zookeeper實(shí)現(xiàn)服務(wù)注冊(cè)與發(fā)現(xiàn),幫助大家更好的理解和學(xué)習(xí)spring框架,感興趣的朋友可以了解下2020-11-11
servlet簡(jiǎn)單實(shí)現(xiàn)文件下載的方法
這篇文章主要介紹了servlet簡(jiǎn)單實(shí)現(xiàn)文件下載的方法,涉及基于servlet技術(shù)實(shí)現(xiàn)流形式文件傳輸?shù)南嚓P(guān)操作技巧,需要的朋友可以參考下2016-12-12

