1、pom依赖
<dependency>
<groupId>org.influxdb</groupId>
<artifactId>influxdb-java</artifactId>
<version>2.21</version>
</dependency>
2、连接配置
influxdb.cached-items=false
influxdb.batch-count=2000
influxdb.datasource.enable=true
influxdb.conn[0]={"url":"http://127.0.0.1:8086", "user":"name", "password":"password", "dbName":"second_supply"}
influxdb.conn[1]={"url":"http://127.0.0.1:8086", "user":"name", "password":"password", "dbName":"pos"}
influxdb.database=second_supply
influxdb.database=pos
influxdb.max-idle=10
influxdb.min-idle=5
influxdb.max-total=20
influxdb.init-pool=5
influxdb.bathSize=100
3、读取配置
package cn.jinka.gcdp.securecenter.influxDB;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j2;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Configuration
@ConditionalOnProperty("influxdb.datasource.enable")
@ConfigurationProperties(prefix = "influxdb")
@Log4j2
public class InfluxDbConfig implements InitializingBean {
private Integer batchCount;
private List<String> conn = new ArrayList<>();
private Map<String, String> database = new HashMap<String, String>();
private Map<String, String> alarm = new HashMap<String, String>();
@Resource
private InfluxDbFactory influxDbFactory;
@Override
public void afterPropertiesSet() throws Exception {
for (String connect : conn) {
InfluxDbConnect influxDbConnect = JSONObject.parseObject(connect, InfluxDbConnect.class);
InfluxDB influxDB = InfluxDBFactory.connect(influxDbConnect.getUrl(), influxDbConnect.getDbName(), influxDbConnect.getPassword());
influxDB.setDatabase(influxDbConnect.getDbName());
influxDB.enableBatch(batchCount, 100, TimeUnit.MILLISECONDS);
influxDbFactory.getInfluxDbMap().put(influxDbConnect.getDbName(), influxDB);
}
influxDbFactory.getTopicMap().putAll(database);
influxDbFactory.getAlarmTopicMap().putAll(alarm);
}
public void setConn(List<String> conn) {
this.conn = conn;
}
public void setDatabase(Map<String, String> database) {
this.database = database;
}
public void setAlarm(Map<String, String> alarm) {
this.alarm = alarm;
}
public void setBatchCount(Integer batchCount) {
this.batchCount = batchCount;
}
}
package cn.jinka.gcdp.securecenter.influxDB;
import lombok.extern.log4j.Log4j2;
import org.influxdb.InfluxDB;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Service
@Log4j2
public class InfluxDbFactory {
private final Map<String, InfluxDB> influxDbMap = new ConcurrentHashMap<>();
private final Map<String, String> topicMap = new HashMap<>();
private final Map<String, String> alarmTopicMap = new HashMap<>();
public Map<String, String> getTopicMap() {
return topicMap;
}
public Map<String, String> getAlarmTopicMap() {
return alarmTopicMap;
}
public String getDbNameByTopic(String topic) {
return topicMap.get(topic);
}
public InfluxDB getInfluxDB(String dbName) throws Exception {
return influxDbMap.get(dbName);
}
public String getAlarmTopic(String systemCode) {
return alarmTopicMap.get(systemCode);
}
public Map<String, InfluxDB> getInfluxDbMap() {
return influxDbMap;
}
}
4、代码调用
package cn.jinka.gcdp.securecenter.influxDB;
import org.influxdb.InfluxDB;
import org.influxdb.dto.QueryResult;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
public class QueryFactory {
@Resource
private InfluxDbFactory influxDbFactory;
public List<QueryResult.Result> baseQuery(String sql, String db) {
try {
InfluxDB influxDB = influxDbFactory.getInfluxDB(db);
QueryResult queryResult = influxDB.query(new org.influxdb.dto.Query(sql, db));
return queryResult.getResults();
} catch (Exception e) {
return new ArrayList<>();
}
}
}
String sql = "SELECT LAST(\"NH_BPG1\") - FIRST(\"NH_BPG1\") AS NH_BPG1,LAST(\"NH_PDG\") - FIRST(\"NH_PDG\") AS NH_PDG,LAST(\"NH_DMTG\") - FIRST(\"NH_DMTG\") AS NH_DMTG FROM \"m_2W04_CN23006001_1\",\"m_2W04_CN23006001_2\",\"m_2W04_CN23006001_0\" WHERE time >= '2023-06-18 00:00:00' AND time < '2023-06-19 13:52:10' GROUP BY time(1h,0m) tz('Asia/Shanghai')";
String db = "second_supply";
List<QueryResult.Result> itemValues = queryFactory.baseQuery(sql, db);