|
@@ -1,13 +1,15 @@
|
|
package com.shkpr.iot.common.core.util;
|
|
package com.shkpr.iot.common.core.util;
|
|
|
|
|
|
|
|
+import com.shkpr.iot.common.core.constants.InfluxdbMetadata;
|
|
|
|
+import com.shkpr.iot.common.core.properties.InfluxDbProperties;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.influxdb.InfluxDB;
|
|
import org.influxdb.InfluxDB;
|
|
import org.influxdb.dto.BatchPoints;
|
|
import org.influxdb.dto.BatchPoints;
|
|
import org.influxdb.dto.Point;
|
|
import org.influxdb.dto.Point;
|
|
|
|
+import org.influxdb.dto.Query;
|
|
|
|
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-import javax.annotation.PreDestroy;
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* influxdb工具类
|
|
* influxdb工具类
|
|
*
|
|
*
|
|
@@ -16,12 +18,18 @@ import javax.annotation.PreDestroy;
|
|
**/
|
|
**/
|
|
@Component
|
|
@Component
|
|
@Slf4j
|
|
@Slf4j
|
|
-public class InfluxDBUtil {
|
|
|
|
|
|
+@EnableConfigurationProperties(InfluxDbProperties.class)
|
|
|
|
+public class InfluxDbUtil {
|
|
final
|
|
final
|
|
- InfluxDB influxDB;
|
|
|
|
|
|
+ InfluxDB influxDb;
|
|
|
|
+
|
|
|
|
+ public InfluxDbUtil(InfluxDbProperties properties, InfluxDB influxDb) {
|
|
|
|
+ //创建数据库
|
|
|
|
+ influxDb.query(new Query(InfluxdbMetadata.CREATE_DATABASE + properties.getDatabase()));
|
|
|
|
+ //设置要使用的数据库
|
|
|
|
+ influxDb.setDatabase(properties.getDatabase());
|
|
|
|
|
|
- public InfluxDBUtil(InfluxDB influxDB) {
|
|
|
|
- this.influxDB = influxDB;
|
|
|
|
|
|
+ this.influxDb = influxDb;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -32,7 +40,7 @@ public class InfluxDBUtil {
|
|
*/
|
|
*/
|
|
public Boolean insert(Point point) {
|
|
public Boolean insert(Point point) {
|
|
try {
|
|
try {
|
|
- influxDB.write(point);
|
|
|
|
|
|
+ influxDb.write(point);
|
|
return true;
|
|
return true;
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.error("插入InfluxDB失败", e);
|
|
log.error("插入InfluxDB失败", e);
|
|
@@ -48,20 +56,11 @@ public class InfluxDBUtil {
|
|
*/
|
|
*/
|
|
public Boolean insertBatch(BatchPoints points) {
|
|
public Boolean insertBatch(BatchPoints points) {
|
|
try {
|
|
try {
|
|
- influxDB.write(points);
|
|
|
|
|
|
+ influxDb.write(points);
|
|
return true;
|
|
return true;
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.error("插入InfluxDB失败", e);
|
|
log.error("插入InfluxDB失败", e);
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 关闭
|
|
|
|
- */
|
|
|
|
- @PreDestroy
|
|
|
|
- private void shutdown() {
|
|
|
|
- influxDB.close();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
}
|
|
}
|