Compare commits
12 Commits
d00316c1fd
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 0088ac746d | |||
| 88bf4b215b | |||
| c8b9b6f730 | |||
| f4ff6b2f1c | |||
| 222080344c | |||
|
|
8d12f409bb | ||
| dd053bc5ca | |||
| 742079e5c2 | |||
| 41784494a1 | |||
|
|
9252193a91 | ||
|
|
44347310c1 | ||
|
|
9b91feead6 |
@@ -8,6 +8,7 @@ import org.influxdb.InfluxDB;
|
|||||||
import org.influxdb.InfluxDBFactory;
|
import org.influxdb.InfluxDBFactory;
|
||||||
import org.influxdb.impl.InfluxDBMapper;
|
import org.influxdb.impl.InfluxDBMapper;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
import org.springframework.context.ConfigurableApplicationContext;
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
@@ -20,6 +21,7 @@ import org.springframework.core.io.ResourceLoader;
|
|||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
@Configuration
|
@Configuration
|
||||||
|
@ConditionalOnProperty(name = "spring.influx.database")
|
||||||
public class InfluxDbConfig {
|
public class InfluxDbConfig {
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -75,6 +75,7 @@ public interface InfluxDbSqlConstant {
|
|||||||
String AS_VALUE = StrPool.C_SPACE + "as value" + StrPool.C_SPACE;
|
String AS_VALUE = StrPool.C_SPACE + "as value" + StrPool.C_SPACE;
|
||||||
|
|
||||||
String EQ = "=";
|
String EQ = "=";
|
||||||
|
String DOUBLE_EQ = "==";
|
||||||
String NE = "!=";
|
String NE = "!=";
|
||||||
String QM = "'";
|
String QM = "'";
|
||||||
String DQM = "\"";
|
String DQM = "\"";
|
||||||
@@ -84,6 +85,7 @@ public interface InfluxDbSqlConstant {
|
|||||||
String GE = ">=";
|
String GE = ">=";
|
||||||
String LT = "<";
|
String LT = "<";
|
||||||
String LE = "<=";
|
String LE = "<=";
|
||||||
|
String SLASH = "/";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* influxDB函数拼接.
|
* influxDB函数拼接.
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ public class InfluxExecutor {
|
|||||||
public <E> List<E> select(String sql, Class domainClass) {
|
public <E> List<E> select(String sql, Class domainClass) {
|
||||||
List<E> results;
|
List<E> results;
|
||||||
//如果domainClass没有注解Measurement,则使用influxdb去查询
|
//如果domainClass没有注解Measurement,则使用influxdb去查询
|
||||||
if (domainClass.isAnnotationPresent(Measurement.class)) {
|
if (!domainClass.isAnnotationPresent(Measurement.class)) {
|
||||||
QueryResult queryResult = influxDb.query(new Query(sql, database));
|
QueryResult queryResult = influxDb.query(new Query(sql, database));
|
||||||
results = new ArrayList<>();
|
results = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
@@ -70,7 +70,7 @@ public class InfluxExecutor {
|
|||||||
//处理tag分组数据
|
//处理tag分组数据
|
||||||
for (String columnName : tags.keySet()) {
|
for (String columnName : tags.keySet()) {
|
||||||
for (Field declaredField : fields) {
|
for (Field declaredField : fields) {
|
||||||
if (columnName.equalsIgnoreCase(InfluxQueryWrapper.getColumnName(declaredField))) {
|
if (columnName.replace("_","").equalsIgnoreCase(InfluxQueryWrapper.getColumnName(declaredField).replace("_",""))) {
|
||||||
//获取属性定义的类型
|
//获取属性定义的类型
|
||||||
declaredField.setAccessible(true);
|
declaredField.setAccessible(true);
|
||||||
//判断是否过滤该条记录,当数据返回 null时
|
//判断是否过滤该条记录,当数据返回 null时
|
||||||
@@ -109,7 +109,7 @@ public class InfluxExecutor {
|
|||||||
String columnName = columns.get(i);
|
String columnName = columns.get(i);
|
||||||
//属性名有下划线的替换掉
|
//属性名有下划线的替换掉
|
||||||
for (Field declaredField : fields) {
|
for (Field declaredField : fields) {
|
||||||
if (columnName.equalsIgnoreCase(InfluxQueryWrapper.getColumnName(declaredField))) {
|
if (columnName.replace("_","").equalsIgnoreCase(InfluxQueryWrapper.getColumnName(declaredField).replace("_",""))) {
|
||||||
//获取属性定义的类型
|
//获取属性定义的类型
|
||||||
declaredField.setAccessible(true);
|
declaredField.setAccessible(true);
|
||||||
//判断是否过滤该条记录,当数据返回 null时
|
//判断是否过滤该条记录,当数据返回 null时
|
||||||
@@ -123,16 +123,16 @@ public class InfluxExecutor {
|
|||||||
declaredField.set(object, InstantUtil.stringToInstant(columnValue.get(i).toString().replace("+08:00", "Z")));
|
declaredField.set(object, InstantUtil.stringToInstant(columnValue.get(i).toString().replace("+08:00", "Z")));
|
||||||
//字符串
|
//字符串
|
||||||
} else if (declaredField.getType() == String.class) {
|
} else if (declaredField.getType() == String.class) {
|
||||||
declaredField.set(object, columnValue.get(i).toString());
|
declaredField.set(object,Objects.isNull(columnValue.get(i))?"":columnValue.get(i).toString());
|
||||||
//浮点双精度
|
//浮点双精度
|
||||||
} else if (declaredField.getType() == Double.class) {
|
} else if (declaredField.getType() == Double.class) {
|
||||||
declaredField.set(object, Double.parseDouble(columnValue.get(i).toString()));
|
declaredField.set(object,Objects.isNull(columnValue.get(i))?null: Double.parseDouble(columnValue.get(i).toString()));
|
||||||
//浮点
|
//浮点
|
||||||
} else if (declaredField.getType() == Float.class) {
|
} else if (declaredField.getType() == Float.class) {
|
||||||
declaredField.set(object, Float.parseFloat(columnValue.get(i).toString()));
|
declaredField.set(object, Objects.isNull(columnValue.get(i))?null: Float.parseFloat(columnValue.get(i).toString()));
|
||||||
//整型
|
//整型
|
||||||
} else if (declaredField.getType() == Integer.class) {
|
} else if (declaredField.getType() == Integer.class) {
|
||||||
declaredField.set(object, Integer.parseInt(columnValue.get(i).toString()));
|
declaredField.set(object,Objects.isNull(columnValue.get(i))?null: Integer.parseInt(columnValue.get(i).toString()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -221,5 +221,4 @@ public class InfluxExecutor {
|
|||||||
return originalSql.concat(InfluxDbSqlConstant.TZ);
|
return originalSql.concat(InfluxDbSqlConstant.TZ);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -238,6 +238,20 @@ public class InfluxQueryWrapper {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <T, R> InfluxQueryWrapper count(String columnName,String resultName) {
|
||||||
|
String selectFragment = InfluxDbSqlConstant.COUNT +
|
||||||
|
InfluxDbSqlConstant.LBK +
|
||||||
|
InfluxDbSqlConstant.DQM +
|
||||||
|
columnName +
|
||||||
|
InfluxDbSqlConstant.DQM +
|
||||||
|
InfluxDbSqlConstant.RBK +
|
||||||
|
InfluxDbSqlConstant.AS +
|
||||||
|
resultName +
|
||||||
|
StrPool.C_SPACE;
|
||||||
|
selectColumns.add(selectFragment);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* 统计平均值
|
* 统计平均值
|
||||||
@@ -924,6 +938,22 @@ public class InfluxQueryWrapper {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <T, R> InfluxQueryWrapper percentile(String columnName,String resultName, int percent) {
|
||||||
|
String selectFragment = InfluxDbSqlConstant.PERCENTILE +
|
||||||
|
InfluxDbSqlConstant.LBK +
|
||||||
|
InfluxDbSqlConstant.DQM +
|
||||||
|
columnName +
|
||||||
|
InfluxDbSqlConstant.DQM +
|
||||||
|
StrPool.COMMA +
|
||||||
|
percent +
|
||||||
|
InfluxDbSqlConstant.RBK +
|
||||||
|
InfluxDbSqlConstant.AS +
|
||||||
|
resultName +
|
||||||
|
StrPool.C_SPACE;
|
||||||
|
selectColumns.add(selectFragment);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* 批量返回field key较大的百分之N的值。
|
* 批量返回field key较大的百分之N的值。
|
||||||
* @author xuyang
|
* @author xuyang
|
||||||
@@ -943,6 +973,26 @@ public class InfluxQueryWrapper {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* 批量返回包含谐波次数的数据。
|
||||||
|
* @author xuyang
|
||||||
|
* @param prefix 表字段名
|
||||||
|
* @param suffix 映射名称
|
||||||
|
* @return InfluxQueryWrapper
|
||||||
|
* 输出 prefix+diffContent+suffix as prefix+diffContent+suffix
|
||||||
|
*/
|
||||||
|
public InfluxQueryWrapper samePrefixAndSuffix(String prefix, String suffix, List<Object> diffContent) {
|
||||||
|
if (CollectionUtil.isEmpty(diffContent)) {
|
||||||
|
throw new RuntimeException("查询数值集合为空,请校验!");
|
||||||
|
}
|
||||||
|
for (Object obj : diffContent) {
|
||||||
|
String fieldName = prefix + obj;
|
||||||
|
String resultName = suffix + obj;
|
||||||
|
this.select(fieldName,resultName);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**************************自定义查询条件,比如between、>、<、=、>=、<=等等**************************/
|
/**************************自定义查询条件,比如between、>、<、=、>=、<=等等**************************/
|
||||||
|
|
||||||
@@ -1278,6 +1328,7 @@ public class InfluxQueryWrapper {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public <T, R, O> InfluxQueryWrapper or(String columnName, List<O> columnValues) {
|
public <T, R, O> InfluxQueryWrapper or(String columnName, List<O> columnValues) {
|
||||||
if (CollectionUtil.isEmpty(columnValues)) {
|
if (CollectionUtil.isEmpty(columnValues)) {
|
||||||
throw new RuntimeException("查询数值集合为空,请校验!");
|
throw new RuntimeException("查询数值集合为空,请校验!");
|
||||||
@@ -1346,6 +1397,20 @@ public class InfluxQueryWrapper {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <T, R> InfluxQueryWrapper regular(String fieldName, List<String> columnValues) {
|
||||||
|
if (CollectionUtil.isEmpty(columnValues)) {
|
||||||
|
throw new RuntimeException("查询数值集合为空,请校验!");
|
||||||
|
}
|
||||||
|
String columnName = this.getColumnName(measurement, fieldName);
|
||||||
|
String conditionSql = columnName + InfluxDbSqlConstant.EQ;
|
||||||
|
String middleSql = String.join(InfluxDbSqlConstant.REGULAR_MIDDLE, columnValues);
|
||||||
|
conditionSql = conditionSql + InfluxDbSqlConstant.REGULAR_PREFIX
|
||||||
|
+ middleSql
|
||||||
|
+ InfluxDbSqlConstant.REGULAR_SUFFIX;
|
||||||
|
conditions.add(conditionSql);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/********************* [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] [LIMIT_clause] [OFFSET_clause] [SLIMIT_clause] [SOFFSET_clause]*********************/
|
/********************* [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] [LIMIT_clause] [OFFSET_clause] [SLIMIT_clause] [SOFFSET_clause]*********************/
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user