|
@@ -1,10 +1,15 @@
|
|
|
package com.gyee.redis2taos.service;
|
|
|
|
|
|
+import cn.hutool.core.collection.ListUtil;
|
|
|
import com.gyee.redis2taos.config.TaosConfig;
|
|
|
+import com.gyee.redis2taos.entity.TaosEntity;
|
|
|
+import com.gyee.redis2taos.mapper.PointAiMapper;
|
|
|
import com.gyee.redis2taos.timeseries.BasicTsPoint;
|
|
|
import com.gyee.redis2taos.timeseries.TsDataType;
|
|
|
import com.gyee.redis2taos.timeseries.TsPoint;
|
|
|
+import com.gyee.redis2taos.util.TaosDatabaseInfo;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.poi.util.StringUtil;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
@@ -26,35 +31,65 @@ public class TaosService {
|
|
|
private FileService fileService;
|
|
|
@Resource
|
|
|
private RedisService redisService;
|
|
|
+ @Resource
|
|
|
+ private PointAiMapper pointAiMapper;
|
|
|
|
|
|
- public boolean createDataBase(int keepday) throws Exception {
|
|
|
+ public void createDataBase(String dbname, int keepday) throws Exception {
|
|
|
log.info("创建taos库!");
|
|
|
Statement st = taosConfig.getCreateCon().createStatement();
|
|
|
- String createSTableSql = "create database if not exists " + taosConfig.getDbName() + " cachemodel 'last_row' wal_retention_period 1800 keep " + keepday;
|
|
|
- int i = st.executeUpdate(createSTableSql);
|
|
|
- //taosConfig.closeCon();
|
|
|
- if (i > 0) {
|
|
|
- return true;
|
|
|
- } else {
|
|
|
- return false;
|
|
|
+ String testsql = "CREATE DATABASE IF NOT EXISTS mydbinfo";
|
|
|
+ st.executeUpdate(testsql);
|
|
|
+ testsql = "CREATE STABLE IF NOT EXISTS mydbinfo.kvstb (ts timestamp,name binary(30),val binary(30)) TAGS (remark binary(30))";
|
|
|
+ st.executeUpdate(testsql);
|
|
|
+ testsql = "CREATE TABLE IF NOT EXISTS mydbinfo.dbinfos USING mydbinfo.kvstb TAGS('')";
|
|
|
+ st.executeUpdate(testsql);
|
|
|
+ testsql = "SELECT * FROM mydbinfo.dbinfos WHERE name='mydbname'";
|
|
|
+ ResultSet resultSet = st.executeQuery(testsql);
|
|
|
+ if (!resultSet.next()) {
|
|
|
+ testsql = "INSERT INTO mydbinfo.dbinfos VALUES (NOW, 'mydbname', '" + dbname + "');";
|
|
|
+ st.executeUpdate(testsql);
|
|
|
+ String mydbname = TaosDatabaseInfo.map.get("mydbname");
|
|
|
+ if (mydbname == null) TaosDatabaseInfo.map.put("mydbname", dbname);
|
|
|
}
|
|
|
+ String createSTableSql = "CREATE DATABASE IF NOT EXISTS " + dbname + " cachemodel 'last_row' wal_retention_period 1800 keep " + keepday;
|
|
|
+ st.executeUpdate(createSTableSql);
|
|
|
}
|
|
|
|
|
|
|
|
|
- public boolean createStables() throws Exception {
|
|
|
- log.info("创建taos超级表!");
|
|
|
+ public boolean createStables(String aiSTablename, String diSTablename) throws Exception {
|
|
|
+
|
|
|
Statement st = taosConfig.getInstance().createStatement();
|
|
|
- String sqlai = "create stable if not exists " + taosConfig.getDbName() + "." + taosConfig.getAIStableName() + " (ts timestamp,val double) " +
|
|
|
- "tags (description nchar(100),station binary(20),category binary(20),facility binary(20),uniformcode binary(20),rate float,remark binary(30));";
|
|
|
- int a = st.executeUpdate(sqlai);
|
|
|
- String sqladi = "create stable if not exists " + taosConfig.getDbName() + "." + taosConfig.getDIStableName() + " (ts timestamp,val bool) " +
|
|
|
- "tags (description nchar(100),station binary(20),category binary(20),facility binary(20),uniformcode binary(20),rate float,remark binary(30));";
|
|
|
- int d = st.executeUpdate(sqladi);
|
|
|
- if ((a + d) > 0) {
|
|
|
- return true;
|
|
|
- } else {
|
|
|
- return false;
|
|
|
+ if (StringUtil.isBlank(aiSTablename) || StringUtil.isBlank(diSTablename)) {
|
|
|
+ aiSTablename = taosConfig.getAIStableName();
|
|
|
+ diSTablename = taosConfig.getDIStableName();
|
|
|
+ }
|
|
|
+
|
|
|
+ String mdnm = TaosDatabaseInfo.map.get("mydbname");
|
|
|
+ if (mdnm == null) return false;
|
|
|
+
|
|
|
+ String testsql = "SELECT * FROM mydbinfo.dbinfos WHERE name='aistablename'";
|
|
|
+ ResultSet resultSet = st.executeQuery(testsql);
|
|
|
+ if (!resultSet.next()) {
|
|
|
+ testsql = "INSERT INTO mydbinfo.dbinfos VALUES (NOW, 'aistablename', '" + aiSTablename + "');";
|
|
|
+ st.executeUpdate(testsql);
|
|
|
+ testsql = "INSERT INTO mydbinfo.dbinfos VALUES (NOW, 'distablename', '" + diSTablename + "');";
|
|
|
+ st.executeUpdate(testsql);
|
|
|
+
|
|
|
+ String astn = TaosDatabaseInfo.map.get("aistablename");
|
|
|
+ String dstn = TaosDatabaseInfo.map.get("distablename");
|
|
|
+ if (astn == null) TaosDatabaseInfo.map.put("aistablename", aiSTablename);
|
|
|
+ if (dstn == null) TaosDatabaseInfo.map.put("distablename", diSTablename);
|
|
|
}
|
|
|
+
|
|
|
+ log.info("创建taos超级表!");
|
|
|
+ String sqlai = "CREATE STABLE IF NOT EXISTS " + mdnm + "." + aiSTablename + " (ts timestamp,val double) " +
|
|
|
+ "TAGS (description nchar(100),station binary(20),category binary(20),facility binary(20),uniformcode binary(20),rate float,remark binary(30));";
|
|
|
+ st.executeUpdate(sqlai);
|
|
|
+ String sqladi = "CREATE STABLE IF NOT EXISTS " + mdnm + "." + diSTablename + " (ts timestamp,val bool) " +
|
|
|
+ "TAGS (description nchar(100),station binary(20),category binary(20),facility binary(20),uniformcode binary(20),rate float,remark binary(30));";
|
|
|
+ st.executeUpdate(sqladi);
|
|
|
+
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
public List<String> showDataBase() throws Exception {
|
|
@@ -70,25 +105,124 @@ public class TaosService {
|
|
|
return ss;
|
|
|
}
|
|
|
|
|
|
- public int createTables() throws Exception {
|
|
|
+ public int createTablesByExcel(List<TaosEntity> entities) throws Exception {
|
|
|
log.info("创建taos表!");
|
|
|
Statement st = taosConfig.getInstance().createStatement();
|
|
|
- List<StringBuilder> excelTaos = fileService.getExcelTaos();
|
|
|
- int i = 0;
|
|
|
+ //List<TaosEntity> entities = fileService.getExcelTaos();
|
|
|
+ List<StringBuilder> excelTaos = taosEntity2CreateStr(entities);
|
|
|
for (StringBuilder excelTao : excelTaos) {
|
|
|
- int i1 = st.executeUpdate(excelTao.toString() + ";");
|
|
|
- i += i1;
|
|
|
+ try {
|
|
|
+ st.executeUpdate(excelTao.toString() + ";");
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.info(excelTao.toString());
|
|
|
+ }
|
|
|
+ log.info(String.valueOf(entities.size()));
|
|
|
}
|
|
|
log.info("taos表创建完成!");
|
|
|
+
|
|
|
+ log.info("更新taos的Tag!");
|
|
|
+ int i = 0;
|
|
|
+ StringBuilder sb;
|
|
|
+ String s;
|
|
|
+ for (TaosEntity entity : entities) {
|
|
|
+ sb = new StringBuilder();
|
|
|
+ if (entity.getPoint() == null) continue;
|
|
|
+ sb.append("ALTER TABLE ").append(entity.getPoint()).append(" SET TAG ");
|
|
|
+ s = sb.toString();
|
|
|
+
|
|
|
+ sb = new StringBuilder();
|
|
|
+ sb.append(s).append("description='").append(entity.getDescription()).append("'");
|
|
|
+ st.executeUpdate(sb.toString());
|
|
|
+
|
|
|
+ sb = new StringBuilder();
|
|
|
+ sb.append(s).append("station='").append(entity.getStation()).append("'");
|
|
|
+ st.executeUpdate(sb.toString());
|
|
|
+
|
|
|
+ sb = new StringBuilder();
|
|
|
+ sb.append(s).append("category='").append(entity.getCategory()).append("'");
|
|
|
+ st.executeUpdate(sb.toString());
|
|
|
+
|
|
|
+ sb = new StringBuilder();
|
|
|
+ sb.append(s).append("facility='").append(entity.getFacility()).append("'");
|
|
|
+ st.executeUpdate(sb.toString());
|
|
|
+
|
|
|
+ sb = new StringBuilder();
|
|
|
+ sb.append(s).append("uniformcode='").append(entity.getUniformcode()).append("'");
|
|
|
+ st.executeUpdate(sb.toString());
|
|
|
+
|
|
|
+ sb = new StringBuilder();
|
|
|
+ sb.append(s).append("rate=").append(entity.getRate());
|
|
|
+ st.executeUpdate(sb.toString());
|
|
|
+
|
|
|
+ sb = new StringBuilder();
|
|
|
+ sb.append(s).append("remark='").append(entity.getRemark()).append("'");
|
|
|
+ st.executeUpdate(sb.toString());
|
|
|
+
|
|
|
+ /*sb.append("ALTER TABLE ").append(entity.getPoint()).append(" SET TAG description='").append(entity.getDescription())
|
|
|
+ .append("',station='").append(entity.getStation()).append("',category='").append(entity.getCategory())
|
|
|
+ .append("',equipment='").append(entity.getEquipment()).append("',uniformcode='")
|
|
|
+ .append(entity.getUniformcode()).append("',rate=").append(entity.getRate()).append(",remark='")
|
|
|
+ .append(entity.getRemark()).append("')");
|
|
|
+ st.executeUpdate(sb.toString());*/
|
|
|
+ //pointAiMapper.alterTags(entity);
|
|
|
+ i++;
|
|
|
+ log.info(String.valueOf(i));
|
|
|
+ }
|
|
|
+ log.info("taos的Tag更新完成!");
|
|
|
return i;
|
|
|
}
|
|
|
|
|
|
- public int createTables2() throws Exception {
|
|
|
+ private List<StringBuilder> taosEntity2CreateStr(List<TaosEntity> entities) {
|
|
|
+ List<StringBuilder> sbList = new ArrayList<>();
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ Map<String, List<TaosEntity>> collect = entities.stream().collect(Collectors.groupingBy(TaosEntity::getDatatype));
|
|
|
+ List<TaosEntity> teDouble = collect.get("double");
|
|
|
+ if (teDouble == null) teDouble = new ArrayList<>();
|
|
|
+ List<TaosEntity> teBool = collect.get("bool");
|
|
|
+ if (teBool == null) teBool = new ArrayList<>();
|
|
|
+
|
|
|
+ String astn = TaosDatabaseInfo.map.get("aistablename");
|
|
|
+ String dstn = TaosDatabaseInfo.map.get("distablename");
|
|
|
+ if (astn == null) astn = taosConfig.getAIStableName();
|
|
|
+ if (dstn == null) dstn = taosConfig.getDIStableName();
|
|
|
+
|
|
|
+ TaosEntity te;
|
|
|
+ for (int i = 0; i < teDouble.size(); i++) {
|
|
|
+ te = teDouble.get(i);
|
|
|
+ if (i % 2999 == 0) {
|
|
|
+ sb = new StringBuilder();
|
|
|
+ sbList.add(sb);
|
|
|
+ sb.append("CREATE TABLE");
|
|
|
+ }
|
|
|
+ sb.append(" IF NOT EXISTS ").append(te.getPoint()).append(" USING ").append(astn).append(" TAGS('")
|
|
|
+ .append(te.getDescription()).append("','").append(te.getStation()).append("','")
|
|
|
+ .append(te.getCategory()).append("','").append(te.getFacility()).append("','")
|
|
|
+ .append(te.getUniformcode()).append("',").append(te.getRate()).append(",'")
|
|
|
+ .append(te.getRemark()).append("')");
|
|
|
+ }
|
|
|
+ for (int i = 0; i < teBool.size(); i++) {
|
|
|
+ te = teBool.get(i);
|
|
|
+ if (i % 2999 == 0) {
|
|
|
+ sb = new StringBuilder();
|
|
|
+ sbList.add(sb);
|
|
|
+ sb.append("CREATE TABLE");
|
|
|
+ }
|
|
|
+ sb.append(" IF NOT EXISTS ").append(te.getPoint()).append(" USING ").append(dstn).append(" TAGS('")
|
|
|
+ .append(te.getDescription()).append("','").append(te.getStation()).append("','")
|
|
|
+ .append(te.getCategory()).append("','").append(te.getFacility()).append("','")
|
|
|
+ .append(te.getUniformcode()).append("',").append(te.getRate()).append(",'")
|
|
|
+ .append(te.getRemark()).append("')");
|
|
|
+ }
|
|
|
+ return sbList;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int createTables() throws Exception {
|
|
|
log.info("创建taos表!");
|
|
|
Statement st = taosConfig.getInstance().createStatement();
|
|
|
|
|
|
Set<String> keys = redisService.getKeys();
|
|
|
- if(keys.size()==0) return 0;
|
|
|
+ if (keys.size() == 0) return 0;
|
|
|
log.info("读取rediskeys成功!!!" + keys.size() + "个点。");
|
|
|
//按类型分点
|
|
|
Map<TsDataType, List<BasicTsPoint>> di = keys.stream().map(ss -> {
|
|
@@ -103,27 +237,46 @@ public class TaosService {
|
|
|
//ai点
|
|
|
List<String> douCollect = di.get(TsDataType.DOUBLE).stream().map(k1 -> k1.getId()).collect(Collectors.toList());
|
|
|
|
|
|
+ //按数量分组,di点
|
|
|
+ List<List<String>> boolStrll = ListUtil.split(boolCollect, 2900);
|
|
|
+ //按数量分组,ai点
|
|
|
+ List<List<String>> douStrll = ListUtil.split(douCollect, 2900);
|
|
|
+
|
|
|
+ String astn = TaosDatabaseInfo.map.get("aistablename");
|
|
|
+ String dastn = TaosDatabaseInfo.map.get("distablename");
|
|
|
+ if (astn == null || dastn == null) return 0;
|
|
|
+
|
|
|
int k = 0;
|
|
|
- for (String s : boolCollect) {
|
|
|
- st.executeUpdate(getDiSql(s));
|
|
|
- log.info(String.valueOf(++k));
|
|
|
+ for (List<String> s : boolStrll) {
|
|
|
+ st.executeUpdate(getDiSql(s, dastn));
|
|
|
+ k += s.size();
|
|
|
+ log.info(String.valueOf(k));
|
|
|
}
|
|
|
- for (String s : douCollect) {
|
|
|
- st.executeUpdate(getAiSql(s));
|
|
|
- log.info(String.valueOf(++k));
|
|
|
+ for (List<String> s : douStrll) {
|
|
|
+ st.executeUpdate(getAiSql(s, astn));
|
|
|
+ k += s.size();
|
|
|
+ log.info(String.valueOf(k));
|
|
|
}
|
|
|
log.info("taos表创建完成!");
|
|
|
- return 1;
|
|
|
+ return k;
|
|
|
}
|
|
|
|
|
|
- private String getAiSql(String s) {
|
|
|
- return "CREATE TABLE IF NOT EXISTS "+taosConfig.getDbName()+"."+s+" USING "+taosConfig.getDbName()
|
|
|
- +"."+taosConfig.getAIStableName()+" TAGS('','','','','',1,'');";
|
|
|
+ private String getAiSql(List<String> ls, String astn) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ sb.append("CREATE TABLE");
|
|
|
+ for (String s : ls) {
|
|
|
+ sb.append(" IF NOT EXISTS ").append(s).append(" USING ").append(astn).append(" TAGS('','','','','',1,'')");
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
}
|
|
|
|
|
|
- private String getDiSql(String s) {
|
|
|
- return "CREATE TABLE IF NOT EXISTS "+taosConfig.getDbName()+"."+s+" USING "+taosConfig.getDbName()
|
|
|
- +"."+taosConfig.getDIStableName()+" TAGS('','','','','',1,'');";
|
|
|
+ private String getDiSql(List<String> ls, String dastn) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ sb.append("CREATE TABLE");
|
|
|
+ for (String s : ls) {
|
|
|
+ sb.append(" IF NOT EXISTS ").append(s).append(" USING ").append(dastn).append(" TAGS('','','','','',1,'')");
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
}
|
|
|
|
|
|
public List<String> deleteTables() throws Exception {
|
|
@@ -164,4 +317,14 @@ public class TaosService {
|
|
|
}
|
|
|
return ss;
|
|
|
}
|
|
|
+
|
|
|
+ public void init() throws Exception {
|
|
|
+ Statement st = taosConfig.getCreateCon().createStatement();
|
|
|
+ String testsql = "SELECT * FROM mydbinfo.dbinfos";
|
|
|
+ ResultSet resultSet = st.executeQuery(testsql);
|
|
|
+ while (resultSet.next()) {
|
|
|
+ TaosDatabaseInfo.map.put(resultSet.getString(2), resultSet.getString(3));
|
|
|
+ log.info("读取表信息" + resultSet.getString(2) + ":" + resultSet.getString(3));
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|