您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)influxDB數(shù)據(jù)庫(kù)怎么在java中使用,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
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); } }
看完上述內(nèi)容,你們對(duì)influxDB數(shù)據(jù)庫(kù)怎么在java中使用有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。