|
|
package org.jeecg.modules.jmreport.utils.influx;
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.influxdb.InfluxDB;
|
|
|
import org.influxdb.dto.Point;
|
|
|
import org.influxdb.dto.Query;
|
|
|
import org.influxdb.dto.QueryResult;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.LinkedList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
public class InfluxDbUtils {
|
|
|
|
|
|
@Autowired
|
|
|
InfluxDBConfig influxDBConfig;
|
|
|
|
|
|
@Autowired
|
|
|
private InfluxDB influxDB;
|
|
|
|
|
|
/**
|
|
|
* 创建数据库
|
|
|
*
|
|
|
* @param dbName
|
|
|
*/
|
|
|
public void createDB(String dbName) {
|
|
|
influxDB.createDatabase(dbName);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 删除数据库
|
|
|
*
|
|
|
* @param dbName
|
|
|
*/
|
|
|
public void deleteDB(String dbName) {
|
|
|
influxDB.deleteDatabase(dbName);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 创建策略
|
|
|
*
|
|
|
* @param command
|
|
|
* @param database
|
|
|
*/
|
|
|
public void createRetentionPolicy(String command, String defalut, String database, String policy, int fb) {
|
|
|
command = String.format("CREATE RETENTION POLICY \"%s\" ON \"%s\" DURATION %s REPLICATION %s DEFAULT", defalut, database, policy, fb);
|
|
|
query(command, database);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 删除策略
|
|
|
*
|
|
|
* @param command
|
|
|
* @param database
|
|
|
*/
|
|
|
public void removeRetentionPolicy(String command, String defalut, String database, String policy, int fb) {
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 创建表
|
|
|
*
|
|
|
* @param command 删除语句
|
|
|
* @return 返回错误信息
|
|
|
*/
|
|
|
public void createMeasurementData(String command, String database) {
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 删除表
|
|
|
*
|
|
|
* @param command 删除语句
|
|
|
* @return 返回错误信息
|
|
|
*/
|
|
|
public String deleteMeasurementData(String command, String database) {
|
|
|
QueryResult result = influxDB.query(new Query(command, database));
|
|
|
return result.getError();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 查询
|
|
|
*
|
|
|
* @param command 查询语句
|
|
|
* @return
|
|
|
*/
|
|
|
public QueryResult query(String command, String database) {
|
|
|
return influxDB.query(new Query(command, database));
|
|
|
}
|
|
|
|
|
|
|
|
|
// public void insert(String measurements, Map<String, String> tagsMap, Map<String, Object> fieldsMap) {
|
|
|
// influxDBConfig.getInfluxDB();
|
|
|
// influxDB.insert(measurements, tagsMap, fieldsMap);
|
|
|
//package org.jeecg.modules.jmreport.utils.influx;
|
|
|
//
|
|
|
//import lombok.extern.slf4j.Slf4j;
|
|
|
//import org.influxdb.InfluxDB;
|
|
|
//import org.influxdb.dto.Point;
|
|
|
//import org.influxdb.dto.Query;
|
|
|
//import org.influxdb.dto.QueryResult;
|
|
|
//import org.springframework.beans.factory.annotation.Autowired;
|
|
|
//import org.springframework.stereotype.Service;
|
|
|
//
|
|
|
//import java.text.SimpleDateFormat;
|
|
|
//import java.util.HashMap;
|
|
|
//import java.util.LinkedList;
|
|
|
//import java.util.List;
|
|
|
//import java.util.Map;
|
|
|
//
|
|
|
//@Slf4j
|
|
|
//@Service
|
|
|
//public class InfluxDbUtils {
|
|
|
//
|
|
|
// @Autowired
|
|
|
// InfluxDBConfig influxDBConfig;
|
|
|
//
|
|
|
// @Autowired
|
|
|
// private InfluxDB influxDB;
|
|
|
//
|
|
|
// /**
|
|
|
// * 创建数据库
|
|
|
// *
|
|
|
// * @param dbName
|
|
|
// */
|
|
|
// public void createDB(String dbName) {
|
|
|
// influxDB.createDatabase(dbName);
|
|
|
// }
|
|
|
|
|
|
/**
|
|
|
* 新增
|
|
|
*
|
|
|
* @param dateabse
|
|
|
* @param measurements
|
|
|
* @param tagsMap
|
|
|
* @param fieldsMap
|
|
|
*/
|
|
|
public void insert(String dateabse, String measurements, Map<String, String> tagsMap, Map<String, Double> fieldsMap) {
|
|
|
Point.Builder builder = Point.measurement(measurements);
|
|
|
//builder.time(System.currentTimeMillis(),TimeUnit.MICROSECONDS);
|
|
|
builder.addField("kpiValue", fieldsMap.get("kpiValue"));
|
|
|
//builder.tag("resType", tagsMap.get("resType"));
|
|
|
builder.tag("resId", tagsMap.get("resId"));
|
|
|
builder.tag("kpiId", tagsMap.get("kpiId"));
|
|
|
builder.tag("flag", tagsMap.get("flag"));
|
|
|
//builder.tag("ipAdde", tagsMap.get("ipAdde"));
|
|
|
Point point = builder.build();
|
|
|
influxDB.setDatabase(dateabse).write(point);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 将queryResult 查询结果转换为 map
|
|
|
*
|
|
|
* @param queryResult 查询结果
|
|
|
* @return List<Map < String , Object>>
|
|
|
* @author 惠佳旭
|
|
|
* @date 2020/12/17
|
|
|
*/
|
|
|
public List<Map<String, Object>> queryResultToMapList(QueryResult queryResult) {
|
|
|
List<Map<String, Object>> resultList = new LinkedList<>();
|
|
|
for (QueryResult.Result result : queryResult.getResults()) {
|
|
|
if (result == null || result.getSeries() == null) {
|
|
|
break;
|
|
|
}
|
|
|
for (QueryResult.Series series : result.getSeries()) {
|
|
|
if (series == null || series.getValues() == null) {
|
|
|
break;
|
|
|
}
|
|
|
List<String> columns = series.getColumns();
|
|
|
for (List<Object> value : series.getValues()) {
|
|
|
if (value == null) {
|
|
|
break;
|
|
|
}
|
|
|
Map<String, Object> map = new HashMap<>(columns.size());
|
|
|
for (int i = 0; i < value.size(); i++) {
|
|
|
String columnName = columns.get(i);
|
|
|
if ("time".equals(columnName)) {
|
|
|
try {
|
|
|
String timeStr = value.get(i).toString();
|
|
|
map.put(columnName, timeStr.substring(0, 10) + " " + timeStr.substring(11, 19));
|
|
|
} catch (Exception e) {
|
|
|
log.error("转换失败:{}转换为日期失败", value.get(i));
|
|
|
map.put(columnName, value.get(i));
|
|
|
}
|
|
|
} else {
|
|
|
map.put(columnName, value.get(i));
|
|
|
}
|
|
|
}
|
|
|
resultList.add(map);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return resultList;
|
|
|
}
|
|
|
|
|
|
|
|
|
public List<Map<String, Object>> queryResultToMap(QueryResult queryResult, String dateFormat) {
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
|
|
|
List<Map<String, Object>> resultList = new LinkedList<>();
|
|
|
for (QueryResult.Result result : queryResult.getResults()) {
|
|
|
if (result == null || result.getSeries() == null) {
|
|
|
break;
|
|
|
}
|
|
|
for (QueryResult.Series series : result.getSeries()) {
|
|
|
if (series == null || series.getValues() == null) {
|
|
|
break;
|
|
|
}
|
|
|
List<String> columns = series.getColumns();
|
|
|
for (List<Object> value : series.getValues()) {
|
|
|
if (value == null) {
|
|
|
break;
|
|
|
}
|
|
|
Map<String, Object> map = new HashMap<>(columns.size());
|
|
|
for (int i = 0; i < value.size(); i++) {
|
|
|
String columnName = columns.get(i);
|
|
|
if ("time".equals(columnName)) {
|
|
|
try {
|
|
|
map.put(columnName, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(sdf.parse(value.get(i).toString())));
|
|
|
} catch (Exception e) {
|
|
|
log.error("转换失败:{}转换为日期失败", value.get(i));
|
|
|
map.put(columnName, value.get(i));
|
|
|
}
|
|
|
} else {
|
|
|
map.put(columnName, value.get(i));
|
|
|
}
|
|
|
}
|
|
|
resultList.add(map);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return resultList;
|
|
|
}
|
|
|
|
|
|
|
|
|
} |
|
|
//
|
|
|
// /**
|
|
|
// * 删除数据库
|
|
|
// *
|
|
|
// * @param dbName
|
|
|
// */
|
|
|
// public void deleteDB(String dbName) {
|
|
|
// influxDB.deleteDatabase(dbName);
|
|
|
// }
|
|
|
//
|
|
|
// /**
|
|
|
// * 创建策略
|
|
|
// *
|
|
|
// * @param command
|
|
|
// * @param database
|
|
|
// */
|
|
|
// public void createRetentionPolicy(String command, String defalut, String database, String policy, int fb) {
|
|
|
// command = String.format("CREATE RETENTION POLICY \"%s\" ON \"%s\" DURATION %s REPLICATION %s DEFAULT", defalut, database, policy, fb);
|
|
|
// query(command, database);
|
|
|
// }
|
|
|
//
|
|
|
// /**
|
|
|
// * 删除策略
|
|
|
// *
|
|
|
// * @param command
|
|
|
// * @param database
|
|
|
// */
|
|
|
// public void removeRetentionPolicy(String command, String defalut, String database, String policy, int fb) {
|
|
|
//
|
|
|
// }
|
|
|
//
|
|
|
// /**
|
|
|
// * 创建表
|
|
|
// *
|
|
|
// * @param command 删除语句
|
|
|
// * @return 返回错误信息
|
|
|
// */
|
|
|
// public void createMeasurementData(String command, String database) {
|
|
|
//
|
|
|
// }
|
|
|
//
|
|
|
// /**
|
|
|
// * 删除表
|
|
|
// *
|
|
|
// * @param command 删除语句
|
|
|
// * @return 返回错误信息
|
|
|
// */
|
|
|
// public String deleteMeasurementData(String command, String database) {
|
|
|
// QueryResult result = influxDB.query(new Query(command, database));
|
|
|
// return result.getError();
|
|
|
// }
|
|
|
//
|
|
|
// /**
|
|
|
// * 查询
|
|
|
// *
|
|
|
// * @param command 查询语句
|
|
|
// * @return
|
|
|
// */
|
|
|
// public QueryResult query(String command, String database) {
|
|
|
// return influxDB.query(new Query(command, database));
|
|
|
// }
|
|
|
//
|
|
|
//
|
|
|
//// public void insert(String measurements, Map<String, String> tagsMap, Map<String, Object> fieldsMap) {
|
|
|
//// influxDBConfig.getInfluxDB();
|
|
|
//// influxDB.insert(measurements, tagsMap, fieldsMap);
|
|
|
//// }
|
|
|
//
|
|
|
// /**
|
|
|
// * 新增
|
|
|
// *
|
|
|
// * @param dateabse
|
|
|
// * @param measurements
|
|
|
// * @param tagsMap
|
|
|
// * @param fieldsMap
|
|
|
// */
|
|
|
// public void insert(String dateabse, String measurements, Map<String, String> tagsMap, Map<String, Double> fieldsMap) {
|
|
|
// Point.Builder builder = Point.measurement(measurements);
|
|
|
// //builder.time(System.currentTimeMillis(),TimeUnit.MICROSECONDS);
|
|
|
// builder.addField("kpiValue", fieldsMap.get("kpiValue"));
|
|
|
// //builder.tag("resType", tagsMap.get("resType"));
|
|
|
// builder.tag("resId", tagsMap.get("resId"));
|
|
|
// builder.tag("kpiId", tagsMap.get("kpiId"));
|
|
|
// builder.tag("flag", tagsMap.get("flag"));
|
|
|
// //builder.tag("ipAdde", tagsMap.get("ipAdde"));
|
|
|
// Point point = builder.build();
|
|
|
// influxDB.setDatabase(dateabse).write(point);
|
|
|
// }
|
|
|
//
|
|
|
// /**
|
|
|
// * 将queryResult 查询结果转换为 map
|
|
|
// *
|
|
|
// * @param queryResult 查询结果
|
|
|
// * @return List<Map < String , Object>>
|
|
|
// * @author 惠佳旭
|
|
|
// * @date 2020/12/17
|
|
|
// */
|
|
|
// public List<Map<String, Object>> queryResultToMapList(QueryResult queryResult) {
|
|
|
// List<Map<String, Object>> resultList = new LinkedList<>();
|
|
|
// for (QueryResult.Result result : queryResult.getResults()) {
|
|
|
// if (result == null || result.getSeries() == null) {
|
|
|
// break;
|
|
|
// }
|
|
|
// for (QueryResult.Series series : result.getSeries()) {
|
|
|
// if (series == null || series.getValues() == null) {
|
|
|
// break;
|
|
|
// }
|
|
|
// List<String> columns = series.getColumns();
|
|
|
// for (List<Object> value : series.getValues()) {
|
|
|
// if (value == null) {
|
|
|
// break;
|
|
|
// }
|
|
|
// Map<String, Object> map = new HashMap<>(columns.size());
|
|
|
// for (int i = 0; i < value.size(); i++) {
|
|
|
// String columnName = columns.get(i);
|
|
|
// if ("time".equals(columnName)) {
|
|
|
// try {
|
|
|
// String timeStr = value.get(i).toString();
|
|
|
// map.put(columnName, timeStr.substring(0, 10) + " " + timeStr.substring(11, 19));
|
|
|
// } catch (Exception e) {
|
|
|
// log.error("转换失败:{}转换为日期失败", value.get(i));
|
|
|
// map.put(columnName, value.get(i));
|
|
|
// }
|
|
|
// } else {
|
|
|
// map.put(columnName, value.get(i));
|
|
|
// }
|
|
|
// }
|
|
|
// resultList.add(map);
|
|
|
// }
|
|
|
// }
|
|
|
// }
|
|
|
// return resultList;
|
|
|
// }
|
|
|
//
|
|
|
//
|
|
|
// public List<Map<String, Object>> queryResultToMap(QueryResult queryResult, String dateFormat) {
|
|
|
// SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
|
|
|
// List<Map<String, Object>> resultList = new LinkedList<>();
|
|
|
// for (QueryResult.Result result : queryResult.getResults()) {
|
|
|
// if (result == null || result.getSeries() == null) {
|
|
|
// break;
|
|
|
// }
|
|
|
// for (QueryResult.Series series : result.getSeries()) {
|
|
|
// if (series == null || series.getValues() == null) {
|
|
|
// break;
|
|
|
// }
|
|
|
// List<String> columns = series.getColumns();
|
|
|
// for (List<Object> value : series.getValues()) {
|
|
|
// if (value == null) {
|
|
|
// break;
|
|
|
// }
|
|
|
// Map<String, Object> map = new HashMap<>(columns.size());
|
|
|
// for (int i = 0; i < value.size(); i++) {
|
|
|
// String columnName = columns.get(i);
|
|
|
// if ("time".equals(columnName)) {
|
|
|
// try {
|
|
|
// map.put(columnName, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(sdf.parse(value.get(i).toString())));
|
|
|
// } catch (Exception e) {
|
|
|
// log.error("转换失败:{}转换为日期失败", value.get(i));
|
|
|
// map.put(columnName, value.get(i));
|
|
|
// }
|
|
|
// } else {
|
|
|
// map.put(columnName, value.get(i));
|
|
|
// }
|
|
|
// }
|
|
|
// resultList.add(map);
|
|
|
// }
|
|
|
// }
|
|
|
// }
|
|
|
// return resultList;
|
|
|
// }
|
|
|
//
|
|
|
//
|
|
|
//} |
...
|
...
|
|