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()); } } } }
每天进步一点点