InfluxDB Java入门

Posted 木易森林

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了InfluxDB Java入门相关的知识,希望对你有一定的参考价值。

添加依赖

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

Java实例

package com.ysl.influx;

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

public class InfluxDBUtils {

    private String userName;
    private String password;
    private String url;

    private InfluxDB influxDB;

    public InfluxDBUtils(String url,String userName, String password) {
        this.userName = userName;
        this.password = password;
        this.url = url;
    }

    public InfluxDB builder(){
        if(influxDB == null){
            synchronized (this){
                if(influxDB == null){
                    influxDB = InfluxDBFactory.connect(url,userName,password);
                }
            }
        }
        return influxDB;
    }

    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 getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public InfluxDB getInfluxDB() {
        return influxDB;
    }

    public void setInfluxDB(InfluxDB influxDB) {
        this.influxDB = influxDB;
    }
}
package com.ysl.influx;

import org.influxdb.InfluxDB;
import org.influxdb.dto.Point;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;

import java.util.Map;
public class InfluxdbService {

    private String database;
    private String policyName;
    private InfluxDB influxDB;

    public InfluxdbService(String database, String policyName, InfluxDB influxDB) {
        this.database = database;
        this.policyName = policyName;
        this.influxDB = influxDB;
    }

    public void createDatabase(){
        influxDB.createDatabase(database);
    }

    /**
     *  建保存策略
     * @param duration 存放时间
     * @param replicationNum 备份数量
     */
    public void createRetentionPolicy(String duration, Integer replicationNum){
        String cmd = String.format("CREATE RETENTION POLICY %s ON %s DURATION %s REPLICATION %s DEFAULT",policyName,database,duration,replicationNum);
        influxDB.query(new Query(cmd,database));
    }


    public QueryResult query(String cmd){
        return influxDB.query(new Query(cmd,database));
    }

    /**
     * 插入数据
     * @param measurement 表名
     * @param tags
     * @param fields
     */
    public void insert(String measurement,Map<String,String> tags,Map<String,Object> fields){
        Point.Builder builder = Point.measurement(measurement).tag(tags);
        if(fields != null && !fields.isEmpty()){
            builder.fields(fields);
        }
        influxDB.write(database,policyName,builder.build());
    }

    public String getDatabase() {
        return database;
    }

    public String getPolicyName() {
        return policyName;
    }

    public InfluxDB getInfluxDB() {
        return influxDB;
    }
}
package com.ysl.influx;

import org.influxdb.InfluxDB;
import org.influxdb.dto.QueryResult;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InfluxdbTest {

    /**
     * 数据库名
     */
    private static final String DB_NAME = "influx_test";

    /**
     * 数据保存策略
     */
    private static final String  retentionPolicy = "influx_name";

    public static void main(String[] args){

        InfluxDB influxDB = new InfluxDBUtils("http://localhost:8086/", "user", "pass").builder();
        InfluxdbService service = new InfluxdbService(DB_NAME,retentionPolicy,influxDB);

        service.createDatabase();

        service.createRetentionPolicy("30d", 1);

        Map<String, String> tags = new HashMap<>();
        tags.put("methodName", "getName");
        Map<String, Object> fields = new HashMap<>();
        fields.put("rt", 200);
        fields.put("tps", 300);
        service.insert("measurementKey", tags, fields);

        // 查询数据
        QueryResult queryResult = service.query("select * from \"measurementKey\"");
        List<QueryResult.Result> results = queryResult.getResults();
        if (results != null && results.size() > 0) {
            for (QueryResult.Result result : results) {
                System.out.println(result.toString());
            }
        }
    }
}

 

以上是关于InfluxDB Java入门的主要内容,如果未能解决你的问题,请参考以下文章

javaweb入门-----jsp概念

推荐net开发cad入门阅读代码片段

Java版InfluxDB工具类

超过每个标签的最大值限制 InfluxDB

三天入门 Docker - 第三天:实现 Docker 容器监控

influxdb-java