InfluxDB数据库读写

1、运行环境:

操作系统:Centos7.2
Java Version1.8.0_131
InfluxDB Version1.2.2

2、代码

import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.Point;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;
import java.util.HashMap;
import java.util.Map;

public class InfluxDBUtil {
    private String url = "";
    private String username = "";
    private String password = "";
    private String database = "";
    private String policyName = "";
    private String duration = "";
    private Integer replicationNum = 1;
    private InfluxDB influxDB = null;

    public InfluxDBUtil(String url) {
        this.url = url;
    }

    /**
     * 设置数据库
     * */
    public InfluxDBUtil setDatabase(String database, String policyName) {
        this.database = database;
        this.policyName = policyName;
        return this;
    }

    public InfluxDBUtil setAuthentication(String username, String password) {
        this.username = username;
        this.password = password;
        return this;
    }

    /**
     * 创建保存策略
    **/
    public InfluxDBUtil setRetentionPolicy(String duration, Integer replicationNum) {
        this.duration = duration;
        this.replicationNum = replicationNum;
        return this;
    }

    public InfluxDBUtil build() {
        if (influxDB == null) {
            synchronized (this) {
                if (influxDB == null) {
                    influxDB = InfluxDBFactory.connect(url, username, password);
                }
            }
        }
        influxDB.createDatabase(database);
        String command = String.format("CREATE RETENTION POLICY \"%s\" ON \"%s\" DURATION %s REPLICATION %s DEFAULT",
                policyName, database, duration, replicationNum);
        this.query(command);
        return this;
    }

    /**
     * 插入数据
     */
    public void insert(String measurement, Map tagsToAdd, Map fields) {

        Point.Builder builder = Point.measurement(measurement).tag(tagsToAdd);
        if (fields != null && !fields.isEmpty()) {
            builder.fields(fields);
        }
        influxDB.write(database, policyName, builder.build());
    }

    /**
     * 查询数据
     */
    public QueryResult query(String command) {
        return influxDB.query(new Query(command, database));
    }

}

3、POM依赖

<dependency>
    <groupId>org.influxdbgroupId>
    <artifactId>influxdb-javaartifactId>
    <version>2.5version>
dependency>

4、测试

public static void main(String[] args) throws Exception {
    InfluxDBUtil svr = new InfluxDBUtil("http://127.0.0.1:8086")
        .setDatabase("mytest", "autogen")
        .setAuthentication("admin", "admin")
        .setRetentionPolicy("30m", 1)
        .build();

    for (int i = 0; i < 30; i++) {
        Map tags = new HashMap<>();
        tags.put("host", "127.0.0.1");

        Map fields = new HashMap<>();
        fields.put("height", "30m");
        fields.put("width", "10m");

        svr.insert("measure", tags, fields);
    }
}

你可能感兴趣的:(数据库)