国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > Java > 正文

java使用influxDB數(shù)據(jù)庫的詳細代碼

2024-07-14 08:41:51
字體:
來源:轉載
供稿:網(wǎng)友

本文實例為大家分享了java使用influxDB數(shù)據(jù)庫的具體代碼,供大家參考,具體內容如下

1.pom.xml中導入jar包依賴

<!-- 引入influxdb依賴  --> <dependency>  <groupId>org.influxdb</groupId>  <artifactId>influxdb-java</artifactId>  <version>2.5</version> </dependency>

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(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 刪除語句   * @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();  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);  }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 镇赉县| 应城市| 保康县| 吐鲁番市| 错那县| 莆田市| 青神县| 禹州市| 英吉沙县| 石河子市| 湘潭市| 湖口县| 上思县| 万安县| 阿拉善右旗| 黔南| 高清| 鹤庆县| 息烽县| 桑植县| 馆陶县| 和田市| 若尔盖县| 慈溪市| 静海县| 廉江市| 娄烦县| 竹山县| 黄大仙区| 东明县| 旬阳县| 台东市| 七台河市| 衡阳县| 西昌市| 洛扎县| 永仁县| 龙江县| 靖远县| 泸州市| 鹤壁市|