当前位置: 首页 > 工具软件 > influxdb-orm > 使用案例 >

Java连接influxDB数据库工具类

卢深
2023-12-01

第一步:导入maven依赖

<!-- influxdb-java -->
<dependency>
    <groupId>org.influxdb</groupId>
    <artifactId>influxdb-java</artifactId>
    <version>2.15</version>
</dependency>

第二步:引入工具类

package com.coalmine.alarm.datasource.util;

import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;

/**
 * @version : 1.0
 * @program : code
 * @description : TODO
 * @date : 2021-10-24 21:10
 **/

public class InfluxDbUtil {

    private  String openurl;
    private  String username;
    private  String password;
    private  String database;

    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(String username, String password, String openurl, String database) {
        //创建 连接
        InfluxDbUtil influxDbUtil = new InfluxDbUtil(username, password, openurl, database);

        influxDbUtil.influxDbBuild();

        return influxDbUtil;
    }

    /**
     * 连接时序数据库;获得InfluxDB
     **/
    public InfluxDB influxDbBuild() {
        if (influxDB == null) {
            influxDB = InfluxDBFactory.connect(openurl, username, password);
            influxDB.createDatabase(database);
        }
        return influxDB;
    }


}

 类似资料: