今天就跟大家聊聊有關influxDB數(shù)據(jù)庫怎么在java中使用,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據(jù)這篇文章可以有所收獲。
成都網(wǎng)站建設哪家好,找創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設計、成都網(wǎng)站建設、微信開發(fā)、成都小程序開發(fā)、集團企業(yè)網(wǎng)站制作等服務項目。核心團隊均擁有互聯(lián)網(wǎng)行業(yè)多年經(jīng)驗,服務眾多知名企業(yè)客戶;涵蓋的客戶類型包括:成都資質代辦等眾多領域,積累了大量豐富的經(jīng)驗,同時也獲得了客戶的一致贊揚!
1.pom.xml中導入jar包依賴
org.influxdb influxdb-java 2.5
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; /** * 時序數(shù)據(jù)庫 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";//數(shù)據(jù)庫 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ù)據(jù)庫;獲得InfluxDB**/ public InfluxDB influxDbBuild(){ if(influxDB == null){ influxDB = InfluxDBFactory.connect(openurl, username, password); influxDB.createDatabase(database); } return influxDB; } /** * 設置數(shù)據(jù)保存策略 * defalut 策略名 /database 數(shù)據(jù)庫名/ 30d 數(shù)據(jù)保存時限30天/ 1 副本個數(shù)為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(Maptags, Map 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(); } /** * 創(chuàng)建數(shù)據(jù)庫 * @param dbName */ public void createDB(String dbName){ influxDB.createDatabase(dbName); } /** * 刪除數(shù)據(jù)庫 * @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(); Maptags = new HashMap<>(); Map 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); } }
看完上述內容,你們對influxDB數(shù)據(jù)庫怎么在java中使用有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。