新增计算量相关td表结构修改
This commit is contained in:
parent
73e409e656
commit
9896224fb1
@ -175,10 +175,9 @@ public class SysIotModelServiceImpl implements SysIotModelService {
|
|||||||
if (sysIotModelFieldDto.getAttributeType() == 140) {
|
if (sysIotModelFieldDto.getAttributeType() == 140) {
|
||||||
sysIotModelField.setDataType("tinyint");
|
sysIotModelField.setDataType("tinyint");
|
||||||
}
|
}
|
||||||
|
sysIotModelFieldMapper.insert(sysIotModelField);
|
||||||
//动态执行创建td超级表,字段
|
//动态执行创建td超级表,字段
|
||||||
createTdStableOrColumn(sysIotModelField);
|
createTdStableOrColumn(sysIotModelField);
|
||||||
sysIotModelFieldMapper.insert(sysIotModelField);
|
|
||||||
//新增物模型属性缓存
|
//新增物模型属性缓存
|
||||||
addModelFieldCache(sysIotModelField);
|
addModelFieldCache(sysIotModelField);
|
||||||
SysIotModelFieldVo sysIotModelFieldVo = new SysIotModelFieldVo();
|
SysIotModelFieldVo sysIotModelFieldVo = new SysIotModelFieldVo();
|
||||||
@ -223,8 +222,9 @@ public class SysIotModelServiceImpl implements SysIotModelService {
|
|||||||
@Override
|
@Override
|
||||||
public void deleteSysIotModelField(SysIotModelFieldDto sysIotModelFieldDto) {
|
public void deleteSysIotModelField(SysIotModelFieldDto sysIotModelFieldDto) {
|
||||||
SysIotModelField sysIotModelField = sysIotModelFieldMapper.selectById(sysIotModelFieldDto.getId());
|
SysIotModelField sysIotModelField = sysIotModelFieldMapper.selectById(sysIotModelFieldDto.getId());
|
||||||
deleteTDStableOrColumn(sysIotModelField);
|
|
||||||
sysIotModelFieldMapper.deleteById(sysIotModelFieldDto.getId());
|
sysIotModelFieldMapper.deleteById(sysIotModelFieldDto.getId());
|
||||||
|
//删除td表结构字段
|
||||||
|
deleteTDStableOrColumn(sysIotModelField);
|
||||||
//删除物模型属性缓存
|
//删除物模型属性缓存
|
||||||
deleteModelFieldCache(sysIotModelField);
|
deleteModelFieldCache(sysIotModelField);
|
||||||
}
|
}
|
||||||
@ -437,9 +437,10 @@ public class SysIotModelServiceImpl implements SysIotModelService {
|
|||||||
}
|
}
|
||||||
if (CollectionUtils.isNotEmpty(sysIotModelFieldList)) {
|
if (CollectionUtils.isNotEmpty(sysIotModelFieldList)) {
|
||||||
//创建td表结构
|
//创建td表结构
|
||||||
//分为高频低频两个list创建
|
//分为高频,低频,计算量三个list创建
|
||||||
List<SysIotModelField> lowCreateList = sysIotModelFieldList.stream().filter(item -> item.getHighSpeed() == 0).collect(Collectors.toList());
|
List<SysIotModelField> lowCreateList = sysIotModelFieldList.stream().filter(item -> item.getHighSpeed() == 0 && item.getAttributeType() != 199).collect(Collectors.toList());
|
||||||
List<SysIotModelField> highCreateList = sysIotModelFieldList.stream().filter(item -> item.getHighSpeed() == 1).collect(Collectors.toList());
|
List<SysIotModelField> highCreateList = sysIotModelFieldList.stream().filter(item -> item.getHighSpeed() == 1 && item.getAttributeType() != 199).collect(Collectors.toList());
|
||||||
|
List<SysIotModelField> calCreateList = sysIotModelFieldList.stream().filter(item -> item.getAttributeType() == 199).collect(Collectors.toList());
|
||||||
for (int i = 0; i < lowCreateList.size(); i++) {
|
for (int i = 0; i < lowCreateList.size(); i++) {
|
||||||
if (i == 0) {
|
if (i == 0) {
|
||||||
createTdStableOrColumn(lowCreateList.get(i));
|
createTdStableOrColumn(lowCreateList.get(i));
|
||||||
@ -452,7 +453,7 @@ public class SysIotModelServiceImpl implements SysIotModelService {
|
|||||||
map.put(sysIotModelField.getAttributeCode(), sysIotModelField.getDataType());
|
map.put(sysIotModelField.getAttributeCode(), sysIotModelField.getDataType());
|
||||||
//创建低频超级表
|
//创建低频超级表
|
||||||
tdEngineService.addStableColumn(sysIotModel.getIotModelCode(), "l_", map);
|
tdEngineService.addStableColumn(sysIotModel.getIotModelCode(), "l_", map);
|
||||||
|
addModelFieldCache(lowCreateList.get(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int i = 0; i < highCreateList.size(); i++) {
|
for (int i = 0; i < highCreateList.size(); i++) {
|
||||||
@ -467,8 +468,12 @@ public class SysIotModelServiceImpl implements SysIotModelService {
|
|||||||
map.put(sysIotModelField.getAttributeCode(), sysIotModelField.getDataType());
|
map.put(sysIotModelField.getAttributeCode(), sysIotModelField.getDataType());
|
||||||
//创建高频超级表
|
//创建高频超级表
|
||||||
tdEngineService.addStableColumn(sysIotModel.getIotModelCode(), "h_", map);
|
tdEngineService.addStableColumn(sysIotModel.getIotModelCode(), "h_", map);
|
||||||
|
addModelFieldCache(highCreateList.get(i));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
for (SysIotModelField item : calCreateList){
|
||||||
|
createTdStableOrColumn(item);
|
||||||
|
addModelFieldCache(item);
|
||||||
}
|
}
|
||||||
//新增pg数据库
|
//新增pg数据库
|
||||||
sysIotModelFieldMapper.insertBatch(sysIotModelFieldList);
|
sysIotModelFieldMapper.insertBatch(sysIotModelFieldList);
|
||||||
@ -480,7 +485,7 @@ public class SysIotModelServiceImpl implements SysIotModelService {
|
|||||||
if (!oldSysIotField.getAttributeCode().equals(item.getAttributeCode()) && oldSysIotField.getDataType().equals(item.getDataType()) && Objects.equals(oldSysIotField.getHighSpeed(), item.getHighSpeed())) {
|
if (!oldSysIotField.getAttributeCode().equals(item.getAttributeCode()) && oldSysIotField.getDataType().equals(item.getDataType()) && Objects.equals(oldSysIotField.getHighSpeed(), item.getHighSpeed())) {
|
||||||
//更新td表结构
|
//更新td表结构
|
||||||
updateTDStableOrColumn(item, oldSysIotField);
|
updateTDStableOrColumn(item, oldSysIotField);
|
||||||
updateModelFieldCache(item,oldSysIotField);
|
updateModelFieldCache(item, oldSysIotField);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -524,41 +529,48 @@ public class SysIotModelServiceImpl implements SysIotModelService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void createTdStableOrColumn(SysIotModelField sysIotModelField) {
|
public void createTdStableOrColumn(SysIotModelField sysIotModelField) {
|
||||||
//如果新增的是第一条记录 创建tdengine超级表 分为高频和低频
|
//如果type是计算量199,创建单独计算量超级表
|
||||||
QueryWrapper<SysIotModelField> queryWrapper = new QueryWrapper<>();
|
if (sysIotModelField.getAttributeType() == 199) {
|
||||||
queryWrapper.eq("iot_model_id", sysIotModelField.getIotModelId());
|
String modelCode = dataService.iotModelMap.get(sysIotModelField.getIotModelId().toString());
|
||||||
queryWrapper.eq("highspeed", sysIotModelField.getHighSpeed());
|
tdEngineService.createCalStable(modelCode, sysIotModelField.getAttributeCode(), sysIotModelField.getDataType());
|
||||||
List<SysIotModelField> sysIotModelFields = sysIotModelFieldMapper.selectList(queryWrapper);
|
|
||||||
if (CollectionUtils.isEmpty(sysIotModelFields)) {
|
|
||||||
if (sysIotModelField.getHighSpeed() == 0) {
|
|
||||||
SysIotModel sysIotModel = sysIotModelMapper.selectById(sysIotModelField.getIotModelId());
|
|
||||||
Map<String, String> map = new HashMap<>();
|
|
||||||
map.put(sysIotModelField.getAttributeCode(), sysIotModelField.getDataType());
|
|
||||||
//创建低频超级表
|
|
||||||
tdEngineService.createStable(sysIotModel.getIotModelCode(), "l_", map);
|
|
||||||
}
|
|
||||||
if (sysIotModelField.getHighSpeed() == 1) {
|
|
||||||
SysIotModel sysIotModel = sysIotModelMapper.selectById(sysIotModelField.getIotModelId());
|
|
||||||
Map<String, String> map = new HashMap<>();
|
|
||||||
map.put(sysIotModelField.getAttributeCode(), sysIotModelField.getDataType());
|
|
||||||
//创建高频超级表
|
|
||||||
tdEngineService.createStable(sysIotModel.getIotModelCode(), "h_", map);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
//stable已经存在,新增stable列
|
//创建type为138 139 140的超级表
|
||||||
if (sysIotModelField.getHighSpeed() == 0) {
|
//如果新增的是第一条记录 创建tdengine超级表 分为高频和低频
|
||||||
SysIotModel sysIotModel = sysIotModelMapper.selectById(sysIotModelField.getIotModelId());
|
QueryWrapper<SysIotModelField> queryWrapper = new QueryWrapper<>();
|
||||||
Map<String, String> map = new HashMap<>();
|
queryWrapper.eq("iot_model_id", sysIotModelField.getIotModelId());
|
||||||
map.put(sysIotModelField.getAttributeCode(), sysIotModelField.getDataType());
|
queryWrapper.eq("highspeed", sysIotModelField.getHighSpeed());
|
||||||
//创建低频超级表
|
List<SysIotModelField> sysIotModelFields = sysIotModelFieldMapper.selectList(queryWrapper);
|
||||||
tdEngineService.addStableColumn(sysIotModel.getIotModelCode(), "l_", map);
|
if (CollectionUtils.isEmpty(sysIotModelFields)) {
|
||||||
}
|
if (sysIotModelField.getHighSpeed() == 0) {
|
||||||
if (sysIotModelField.getHighSpeed() == 1) {
|
SysIotModel sysIotModel = sysIotModelMapper.selectById(sysIotModelField.getIotModelId());
|
||||||
SysIotModel sysIotModel = sysIotModelMapper.selectById(sysIotModelField.getIotModelId());
|
Map<String, String> map = new HashMap<>();
|
||||||
Map<String, String> map = new HashMap<>();
|
map.put(sysIotModelField.getAttributeCode(), sysIotModelField.getDataType());
|
||||||
map.put(sysIotModelField.getAttributeCode(), sysIotModelField.getDataType());
|
//创建低频超级表
|
||||||
//创建高频超级表
|
tdEngineService.createStable(sysIotModel.getIotModelCode(), "l_", map);
|
||||||
tdEngineService.addStableColumn(sysIotModel.getIotModelCode(), "h_", map);
|
}
|
||||||
|
if (sysIotModelField.getHighSpeed() == 1) {
|
||||||
|
SysIotModel sysIotModel = sysIotModelMapper.selectById(sysIotModelField.getIotModelId());
|
||||||
|
Map<String, String> map = new HashMap<>();
|
||||||
|
map.put(sysIotModelField.getAttributeCode(), sysIotModelField.getDataType());
|
||||||
|
//创建高频超级表
|
||||||
|
tdEngineService.createStable(sysIotModel.getIotModelCode(), "h_", map);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//stable已经存在,新增stable列
|
||||||
|
if (sysIotModelField.getHighSpeed() == 0) {
|
||||||
|
SysIotModel sysIotModel = sysIotModelMapper.selectById(sysIotModelField.getIotModelId());
|
||||||
|
Map<String, String> map = new HashMap<>();
|
||||||
|
map.put(sysIotModelField.getAttributeCode(), sysIotModelField.getDataType());
|
||||||
|
//创建低频超级表
|
||||||
|
tdEngineService.addStableColumn(sysIotModel.getIotModelCode(), "l_", map);
|
||||||
|
}
|
||||||
|
if (sysIotModelField.getHighSpeed() == 1) {
|
||||||
|
SysIotModel sysIotModel = sysIotModelMapper.selectById(sysIotModelField.getIotModelId());
|
||||||
|
Map<String, String> map = new HashMap<>();
|
||||||
|
map.put(sysIotModelField.getAttributeCode(), sysIotModelField.getDataType());
|
||||||
|
//创建高频超级表
|
||||||
|
tdEngineService.addStableColumn(sysIotModel.getIotModelCode(), "h_", map);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -569,52 +581,70 @@ public class SysIotModelServiceImpl implements SysIotModelService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void deleteTDStableOrColumn(SysIotModelField sysIotModelField) {
|
public void deleteTDStableOrColumn(SysIotModelField sysIotModelField) {
|
||||||
String stableName = null;
|
if (sysIotModelField.getAttributeType() == 199) {
|
||||||
SysIotModel sysIotModel = sysIotModelMapper.selectById(sysIotModelField.getIotModelId());
|
Long iotModelId = sysIotModelField.getIotModelId();
|
||||||
if (sysIotModelField.getHighSpeed() == 0) {
|
String modelCode = dataService.iotModelMap.get(iotModelId.toString());
|
||||||
stableName = "l_" + sysIotModel.getIotModelCode();
|
tdEngineService.deleteStable("c_" + modelCode +"_"+ sysIotModelField.getAttributeCode());
|
||||||
}
|
} else {
|
||||||
if (sysIotModelField.getHighSpeed() == 1) {
|
String stableName = null;
|
||||||
stableName = "h_" + sysIotModel.getIotModelCode();
|
SysIotModel sysIotModel = sysIotModelMapper.selectById(sysIotModelField.getIotModelId());
|
||||||
}
|
if (sysIotModelField.getHighSpeed() == 0) {
|
||||||
QueryWrapper<SysIotModelField> queryWrapper = new QueryWrapper<>();
|
stableName = "l_" + sysIotModel.getIotModelCode();
|
||||||
queryWrapper.eq("iot_model_id", sysIotModelField.getIotModelId());
|
}
|
||||||
queryWrapper.eq("highspeed", sysIotModelField.getHighSpeed());
|
if (sysIotModelField.getHighSpeed() == 1) {
|
||||||
List<SysIotModelField> sysIotModelFields = sysIotModelFieldMapper.selectList(queryWrapper);
|
stableName = "h_" + sysIotModel.getIotModelCode();
|
||||||
if (CollectionUtils.isNotEmpty(sysIotModelFields) && sysIotModelFields.size() > 1) {
|
}
|
||||||
tdEngineService.deleteColumn(stableName, sysIotModelField.getAttributeCode());
|
QueryWrapper<SysIotModelField> queryWrapper = new QueryWrapper<>();
|
||||||
}
|
queryWrapper.eq("iot_model_id", sysIotModelField.getIotModelId());
|
||||||
//物模型属性只剩下最后一个删除表
|
queryWrapper.eq("highspeed", sysIotModelField.getHighSpeed());
|
||||||
if (CollectionUtils.isNotEmpty(sysIotModelFields) && sysIotModelFields.size() == 1) {
|
List<SysIotModelField> sysIotModelFields = sysIotModelFieldMapper.selectList(queryWrapper);
|
||||||
tdEngineService.deleteStable(stableName);
|
if (CollectionUtils.isNotEmpty(sysIotModelFields) && sysIotModelFields.size() > 1) {
|
||||||
|
tdEngineService.deleteColumn(stableName, sysIotModelField.getAttributeCode());
|
||||||
|
}
|
||||||
|
//物模型属性只剩下最后一个删除表
|
||||||
|
if (CollectionUtils.isNotEmpty(sysIotModelFields) && sysIotModelFields.size() == 1) {
|
||||||
|
tdEngineService.deleteStable(stableName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//新增物模型属性,属性dataType缓存
|
//新增物模型属性,属性dataType缓存
|
||||||
private void addModelFieldCache(SysIotModelField sysIotModelField) {
|
private void addModelFieldCache(SysIotModelField sysIotModelField) {
|
||||||
//获取物模型编码
|
//获取物模型编码
|
||||||
String modelCode = dataService.iotModelMap.get(sysIotModelField.getIotModelId().toString());
|
String modelCode = dataService.iotModelMap.get(sysIotModelField.getIotModelId().toString());
|
||||||
if (sysIotModelField.getHighSpeed() == 0) {
|
if (sysIotModelField.getAttributeType() == 199) {
|
||||||
Map<String, Object> map = dataService.lowIotFieldMap.get(modelCode);
|
Map<String, String> map = dataService.calculateIotFieldMap.get(modelCode);
|
||||||
if (map == null){
|
if (map == null) {
|
||||||
Map<String, Object> cacheFieldMap = new HashMap<>();
|
Map<String, String> calMap = new HashMap<>();
|
||||||
cacheFieldMap.put(sysIotModelField.getAttributeCode(), sysIotModelField.getDataType());
|
calMap.put(sysIotModelField.getAttributeCode(), sysIotModelField.getDataType());
|
||||||
dataService.lowIotFieldMap.put(modelCode,cacheFieldMap);
|
dataService.calculateIotFieldMap.put(modelCode, calMap);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
map.put(sysIotModelField.getAttributeCode(), sysIotModelField.getDataType());
|
map.put(sysIotModelField.getAttributeCode(), sysIotModelField.getDataType());
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
if (sysIotModelField.getHighSpeed() == 1) {
|
if (sysIotModelField.getHighSpeed() == 0) {
|
||||||
Map<String, Object> map = dataService.highIotFieldMap.get(modelCode);
|
Map<String, Object> map = dataService.lowIotFieldMap.get(modelCode);
|
||||||
if (map == null){
|
if (map == null) {
|
||||||
Map<String, Object> cacheFieldMap = new HashMap<>();
|
Map<String, Object> cacheFieldMap = new HashMap<>();
|
||||||
cacheFieldMap.put(sysIotModelField.getAttributeCode(), sysIotModelField.getDataType());
|
cacheFieldMap.put(sysIotModelField.getAttributeCode(), sysIotModelField.getDataType());
|
||||||
dataService.highIotFieldMap.put(modelCode,cacheFieldMap);
|
dataService.lowIotFieldMap.put(modelCode, cacheFieldMap);
|
||||||
}else {
|
} else {
|
||||||
map.put(sysIotModelField.getAttributeCode(), sysIotModelField.getDataType());
|
map.put(sysIotModelField.getAttributeCode(), sysIotModelField.getDataType());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (sysIotModelField.getHighSpeed() == 1) {
|
||||||
|
Map<String, Object> map = dataService.highIotFieldMap.get(modelCode);
|
||||||
|
if (map == null) {
|
||||||
|
Map<String, Object> cacheFieldMap = new HashMap<>();
|
||||||
|
cacheFieldMap.put(sysIotModelField.getAttributeCode(), sysIotModelField.getDataType());
|
||||||
|
dataService.highIotFieldMap.put(modelCode, cacheFieldMap);
|
||||||
|
} else {
|
||||||
|
map.put(sysIotModelField.getAttributeCode(), sysIotModelField.getDataType());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//更新物模型属性,属性dataType缓存
|
//更新物模型属性,属性dataType缓存
|
||||||
@ -625,25 +655,30 @@ public class SysIotModelServiceImpl implements SysIotModelService {
|
|||||||
addModelFieldCache(newSysIotModelField);
|
addModelFieldCache(newSysIotModelField);
|
||||||
}
|
}
|
||||||
|
|
||||||
//新增物模型属性,属性dataType缓存
|
//删除物模型属性,删除属性缓存
|
||||||
private void deleteModelFieldCache(SysIotModelField sysIotModelField) {
|
private void deleteModelFieldCache(SysIotModelField sysIotModelField) {
|
||||||
//获取物模型编码
|
//获取物模型编码
|
||||||
String modelCode = dataService.iotModelMap.get(sysIotModelField.getIotModelId().toString());
|
String modelCode = dataService.iotModelMap.get(sysIotModelField.getIotModelId().toString());
|
||||||
if (sysIotModelField.getHighSpeed() == 0) {
|
if (sysIotModelField.getAttributeType() == 199){
|
||||||
Map<String, Object> map = dataService.lowIotFieldMap.get(modelCode);
|
Map<String, String> map = dataService.calculateIotFieldMap.get(modelCode);
|
||||||
map.remove(sysIotModelField.getAttributeCode());
|
|
||||||
}
|
|
||||||
if (sysIotModelField.getHighSpeed() == 1) {
|
|
||||||
Map<String, Object> map = dataService.highIotFieldMap.get(modelCode);
|
|
||||||
map.remove(sysIotModelField.getAttributeCode());
|
map.remove(sysIotModelField.getAttributeCode());
|
||||||
|
}else {
|
||||||
|
if (sysIotModelField.getHighSpeed() == 0) {
|
||||||
|
Map<String, Object> map = dataService.lowIotFieldMap.get(modelCode);
|
||||||
|
map.remove(sysIotModelField.getAttributeCode());
|
||||||
|
}
|
||||||
|
if (sysIotModelField.getHighSpeed() == 1) {
|
||||||
|
Map<String, Object> map = dataService.highIotFieldMap.get(modelCode);
|
||||||
|
map.remove(sysIotModelField.getAttributeCode());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addModelCache(SysIotModel sysIotModel){
|
private void addModelCache(SysIotModel sysIotModel) {
|
||||||
dataService.iotModelMap.put(sysIotModel.getId().toString(), sysIotModel.getIotModelCode());
|
dataService.iotModelMap.put(sysIotModel.getId().toString(), sysIotModel.getIotModelCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void deleteModelCache(Long irn){
|
private void deleteModelCache(Long irn) {
|
||||||
dataService.iotModelMap.remove(irn.toString());
|
dataService.iotModelMap.remove(irn.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -91,6 +91,27 @@ public class TDEngineService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void createCalStable(String iotmodel, String iotModelField,String dataType) {
|
||||||
|
try (Connection conn = hikariDataSource.getConnection();
|
||||||
|
Statement pstmt = conn.createStatement()) {
|
||||||
|
StringBuilder sb = new StringBuilder(1024 * 1024);
|
||||||
|
sb.setLength(0);
|
||||||
|
sb.append("CREATE STABLE IF NOT EXISTS ");
|
||||||
|
sb.append("c_").append(iotmodel).append("_").append(iotModelField);
|
||||||
|
sb.append(" (`updatetime` TIMESTAMP, datavalue ");
|
||||||
|
sb.append(dataType);
|
||||||
|
sb.append(") TAGS (`deviceid` BIGINT);");
|
||||||
|
try {
|
||||||
|
log.info(sb.toString());
|
||||||
|
pstmt.executeUpdate(sb.toString());
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("创建超级表失败,失败原因{}", e);
|
||||||
|
|
||||||
|
}
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 新增超级表列
|
* 新增超级表列
|
||||||
*/
|
*/
|
||||||
@ -170,14 +191,11 @@ public class TDEngineService {
|
|||||||
|
|
||||||
|
|
||||||
// 遍历所有的物模型存入内存中
|
// 遍历所有的物模型存入内存中
|
||||||
public void initIotModel(List<IotModelFieldVo> allIotModel, ConcurrentHashMap<String, Map<String, Object>> highIotFieldMap, ConcurrentHashMap<String, Map<String, Object>> lowIotFieldMap) {
|
public void initIotModel(List<IotModelFieldVo> allIotModel, ConcurrentHashMap<String, Map<String, Object>> highIotFieldMap, ConcurrentHashMap<String, Map<String, Object>> lowIotFieldMap, ConcurrentHashMap<String, Map<String, String>> calculateIotFieldMap) {
|
||||||
// 创建物模型超级表
|
// 创建物模型超级表
|
||||||
|
|
||||||
try (Connection conn = hikariDataSource.getConnection();
|
try (Connection conn = hikariDataSource.getConnection();
|
||||||
Statement pstmt = conn.createStatement()) {
|
Statement pstmt = conn.createStatement()) {
|
||||||
|
|
||||||
ListUtil.page(allIotModel, batchSize, list -> {
|
ListUtil.page(allIotModel, batchSize, list -> {
|
||||||
|
|
||||||
for (IotModelFieldVo info : list) {
|
for (IotModelFieldVo info : list) {
|
||||||
StringBuilder sb = new StringBuilder(1024 * 1024);
|
StringBuilder sb = new StringBuilder(1024 * 1024);
|
||||||
sb.setLength(0);
|
sb.setLength(0);
|
||||||
@ -187,7 +205,6 @@ public class TDEngineService {
|
|||||||
sb.append("CREATE STABLE IF NOT EXISTS ");
|
sb.append("CREATE STABLE IF NOT EXISTS ");
|
||||||
sb.append("h_").append(info.getIotModelCode());
|
sb.append("h_").append(info.getIotModelCode());
|
||||||
sb.append(" (`updatetime` TIMESTAMP");
|
sb.append(" (`updatetime` TIMESTAMP");
|
||||||
|
|
||||||
// 使用增强的 for 循环遍历键
|
// 使用增强的 for 循环遍历键
|
||||||
for (String key : map.keySet()) {
|
for (String key : map.keySet()) {
|
||||||
sb.append(", ");
|
sb.append(", ");
|
||||||
@ -202,7 +219,6 @@ public class TDEngineService {
|
|||||||
log.error("save yx error", ex);
|
log.error("save yx error", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
for (IotModelFieldVo info : list) {
|
for (IotModelFieldVo info : list) {
|
||||||
StringBuilder sb = new StringBuilder(1024 * 1024);
|
StringBuilder sb = new StringBuilder(1024 * 1024);
|
||||||
@ -227,6 +243,16 @@ public class TDEngineService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//初始化创建计算量td超级表
|
||||||
|
for (IotModelFieldVo info : list) {
|
||||||
|
String iotModelCode = info.getIotModelCode();
|
||||||
|
Map<String, String> calFieldMap = calculateIotFieldMap.get(iotModelCode);
|
||||||
|
if (calFieldMap.keySet().size() != 0){
|
||||||
|
for (String key : calFieldMap.keySet()){
|
||||||
|
createCalStable(iotModelCode,key,calFieldMap.get(key));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
log.error(ex.getMessage());
|
log.error(ex.getMessage());
|
||||||
@ -353,8 +379,8 @@ public class TDEngineService {
|
|||||||
}
|
}
|
||||||
Map<String, Map<String, Map<String, Object>>> result = new HashMap<>();
|
Map<String, Map<String, Map<String, Object>>> result = new HashMap<>();
|
||||||
Map<String, Map<String, Object>> valueMap = new HashMap<>();
|
Map<String, Map<String, Object>> valueMap = new HashMap<>();
|
||||||
for (String item : fieldList){
|
for (String item : fieldList) {
|
||||||
valueMap.put(item,null);
|
valueMap.put(item, null);
|
||||||
}
|
}
|
||||||
StringBuffer sb = new StringBuffer(2048);
|
StringBuffer sb = new StringBuffer(2048);
|
||||||
if (StrUtil.isNotBlank(interval)) {
|
if (StrUtil.isNotBlank(interval)) {
|
||||||
@ -389,17 +415,17 @@ public class TDEngineService {
|
|||||||
Statement smt = conn.createStatement();
|
Statement smt = conn.createStatement();
|
||||||
ResultSet rs = smt.executeQuery(sb.toString())) {
|
ResultSet rs = smt.executeQuery(sb.toString())) {
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
for (int i = 0; i < fieldList.size(); i++){
|
for (int i = 0; i < fieldList.size(); i++) {
|
||||||
if (valueMap.get(fieldList.get(i)) == null){
|
if (valueMap.get(fieldList.get(i)) == null) {
|
||||||
Map<String,Object> map = new HashMap<>();
|
Map<String, Object> map = new HashMap<>();
|
||||||
List<Long> timeList = new ArrayList<>();
|
List<Long> timeList = new ArrayList<>();
|
||||||
timeList.add(rs.getTimestamp(1).getTime());
|
timeList.add(rs.getTimestamp(1).getTime());
|
||||||
List<Object> valueList = new ArrayList<>();
|
List<Object> valueList = new ArrayList<>();
|
||||||
valueList.add(rs.getObject(fieldList.get(i).toLowerCase()));
|
valueList.add(rs.getObject(fieldList.get(i).toLowerCase()));
|
||||||
map.put("times",timeList);
|
map.put("times", timeList);
|
||||||
map.put("values",valueList);
|
map.put("values", valueList);
|
||||||
valueMap.put(fieldList.get(i),map);
|
valueMap.put(fieldList.get(i), map);
|
||||||
}else {
|
} else {
|
||||||
Map<String, Object> map = valueMap.get(fieldList.get(i));
|
Map<String, Object> map = valueMap.get(fieldList.get(i));
|
||||||
List<Long> times = (List<Long>) map.get("times");
|
List<Long> times = (List<Long>) map.get("times");
|
||||||
List<Object> values = (List<Object>) map.get("values");
|
List<Object> values = (List<Object>) map.get("values");
|
||||||
@ -408,7 +434,7 @@ public class TDEngineService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result.put(irn.toString(),valueMap);
|
result.put(irn.toString(), valueMap);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("获取数据异常", e);
|
log.error("获取数据异常", e);
|
||||||
return result;
|
return result;
|
||||||
@ -426,8 +452,8 @@ public class TDEngineService {
|
|||||||
}
|
}
|
||||||
Map<String, Map<String, Map<String, Object>>> result = new HashMap<>();
|
Map<String, Map<String, Map<String, Object>>> result = new HashMap<>();
|
||||||
Map<String, Map<String, Object>> valueMap = new HashMap<>();
|
Map<String, Map<String, Object>> valueMap = new HashMap<>();
|
||||||
for (String item : fieldList){
|
for (String item : fieldList) {
|
||||||
valueMap.put(item,null);
|
valueMap.put(item, null);
|
||||||
}
|
}
|
||||||
StringBuffer sb = new StringBuffer(2048);
|
StringBuffer sb = new StringBuffer(2048);
|
||||||
if (StrUtil.isNotBlank(interval)) {
|
if (StrUtil.isNotBlank(interval)) {
|
||||||
@ -462,17 +488,17 @@ public class TDEngineService {
|
|||||||
Statement smt = conn.createStatement();
|
Statement smt = conn.createStatement();
|
||||||
ResultSet rs = smt.executeQuery(sb.toString())) {
|
ResultSet rs = smt.executeQuery(sb.toString())) {
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
for (int i = 0; i < fieldList.size(); i++){
|
for (int i = 0; i < fieldList.size(); i++) {
|
||||||
if (valueMap.get(fieldList.get(i)) == null){
|
if (valueMap.get(fieldList.get(i)) == null) {
|
||||||
Map<String,Object> map = new HashMap<>();
|
Map<String, Object> map = new HashMap<>();
|
||||||
List<Long> timeList = new ArrayList<>();
|
List<Long> timeList = new ArrayList<>();
|
||||||
timeList.add(rs.getTimestamp(1).getTime());
|
timeList.add(rs.getTimestamp(1).getTime());
|
||||||
List<Object> valueList = new ArrayList<>();
|
List<Object> valueList = new ArrayList<>();
|
||||||
valueList.add(rs.getObject(fieldList.get(i).toLowerCase()));
|
valueList.add(rs.getObject(fieldList.get(i).toLowerCase()));
|
||||||
map.put("times",timeList);
|
map.put("times", timeList);
|
||||||
map.put("values",valueList);
|
map.put("values", valueList);
|
||||||
valueMap.put(fieldList.get(i),map);
|
valueMap.put(fieldList.get(i), map);
|
||||||
}else {
|
} else {
|
||||||
Map<String, Object> map = valueMap.get(fieldList.get(i));
|
Map<String, Object> map = valueMap.get(fieldList.get(i));
|
||||||
List<Long> times = (List<Long>) map.get("times");
|
List<Long> times = (List<Long>) map.get("times");
|
||||||
List<Object> values = (List<Object>) map.get("values");
|
List<Object> values = (List<Object>) map.get("values");
|
||||||
@ -481,7 +507,7 @@ public class TDEngineService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result.put(irn.toString(),valueMap);
|
result.put(irn.toString(), valueMap);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("获取数据异常", e);
|
log.error("获取数据异常", e);
|
||||||
return result;
|
return result;
|
||||||
|
@ -71,6 +71,8 @@ public class DataServiceImpl implements DataService {
|
|||||||
|
|
||||||
public ConcurrentHashMap<String, Map<String, Object>> lowIotFieldMap = new ConcurrentHashMap<>(10000);
|
public ConcurrentHashMap<String, Map<String, Object>> lowIotFieldMap = new ConcurrentHashMap<>(10000);
|
||||||
|
|
||||||
|
public ConcurrentHashMap<String, Map<String, String>> calculateIotFieldMap = new ConcurrentHashMap<>(10000);
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void init() {
|
public void init() {
|
||||||
//初始化高性能队列
|
//初始化高性能队列
|
||||||
@ -218,8 +220,9 @@ public class DataServiceImpl implements DataService {
|
|||||||
String key = String.valueOf(item.getId());
|
String key = String.valueOf(item.getId());
|
||||||
iotModelMap.put(key, item.getIotModelCode());
|
iotModelMap.put(key, item.getIotModelCode());
|
||||||
List<SysIotModelField> allIotModelField = sysIotModelMapper.getAllIotModelField(item.getId());
|
List<SysIotModelField> allIotModelField = sysIotModelMapper.getAllIotModelField(item.getId());
|
||||||
Map<String,String> LowModelFieldList = allIotModelField.stream().filter(field -> field.getHighSpeed() == 0).collect(Collectors.toMap(SysIotModelField::getAttributeCode, SysIotModelField::getDataType, (value1, value2) -> value1));
|
Map<String,String> LowModelFieldList = allIotModelField.stream().filter(field -> field.getHighSpeed() == 0 && field.getAttributeType() != 199).collect(Collectors.toMap(SysIotModelField::getAttributeCode, SysIotModelField::getDataType, (value1, value2) -> value1));
|
||||||
Map<String,String> HighModelFieldList = allIotModelField.stream().filter(field -> field.getHighSpeed() == 1).collect(Collectors.toMap(SysIotModelField::getAttributeCode, SysIotModelField::getDataType, (value1, value2) -> value1));
|
Map<String,String> HighModelFieldList = allIotModelField.stream().filter(field -> field.getHighSpeed() == 1 && field.getAttributeType() != 199).collect(Collectors.toMap(SysIotModelField::getAttributeCode, SysIotModelField::getDataType, (value1, value2) -> value1));
|
||||||
|
Map<String, String> calculateFieldList = allIotModelField.stream().filter(field -> field.getAttributeType() == 199).collect(Collectors.toMap(SysIotModelField::getAttributeCode, SysIotModelField::getDataType, (value1, value2) -> value1));
|
||||||
Map<String, Object> map = new HashMap<>();
|
Map<String, Object> map = new HashMap<>();
|
||||||
for (String field : HighModelFieldList.keySet()) {
|
for (String field : HighModelFieldList.keySet()) {
|
||||||
map.put(field, HighModelFieldList.get(field));
|
map.put(field, HighModelFieldList.get(field));
|
||||||
@ -230,8 +233,9 @@ public class DataServiceImpl implements DataService {
|
|||||||
lowMap.put(field, LowModelFieldList.get(field));
|
lowMap.put(field, LowModelFieldList.get(field));
|
||||||
}
|
}
|
||||||
lowIotFieldMap.put(item.getIotModelCode(), lowMap);
|
lowIotFieldMap.put(item.getIotModelCode(), lowMap);
|
||||||
|
calculateIotFieldMap.put(item.getIotModelCode(),calculateFieldList);
|
||||||
}
|
}
|
||||||
tdEngineService.initIotModel(allIotModel, highIotFieldMap, lowIotFieldMap);
|
tdEngineService.initIotModel(allIotModel, highIotFieldMap, lowIotFieldMap,calculateIotFieldMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -78,7 +78,7 @@
|
|||||||
where se.id = #{id}
|
where se.id = #{id}
|
||||||
</select>
|
</select>
|
||||||
<select id="getAllIotModelField" resultType="com.das.modules.equipment.entity.SysIotModelField">
|
<select id="getAllIotModelField" resultType="com.das.modules.equipment.entity.SysIotModelField">
|
||||||
select simf.attribute_code as attributeCode,simf.highspeed as highSpeed,simf.datatype as dataType from sys_iot_model_field simf where simf.iot_model_id = #{id} order by simf.attribute_code
|
select simf.attribute_code as attributeCode,simf.highspeed as highSpeed,simf.datatype as dataType,simf.attribute_type as attributeType from sys_iot_model_field simf where simf.iot_model_id = #{id} order by simf.attribute_code
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user