数据备份新增

This commit is contained in:
huguanghan 2025-01-13 17:33:39 +08:00
parent 2910ea7ee0
commit 160650c086
5 changed files with 130 additions and 2 deletions

View File

@ -67,6 +67,9 @@ public class TDEngineService {
} }
} }
public HikariDataSource getHikariDataSource(){
return hikariDataSource;
}
/** /**
* 创建超级表 * 创建超级表
*/ */

View File

@ -25,6 +25,7 @@ import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.StopWatch; import org.springframework.util.StopWatch;
import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function; import java.util.function.Function;

View File

@ -0,0 +1,120 @@
package com.das.modules.data.service.impl;
import cn.hutool.core.util.ZipUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.das.modules.data.service.TDEngineService;
import com.das.modules.equipment.entity.SysEquipment;
import com.das.modules.equipment.mapper.SysEquipmentMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.TimeUnit;
@Slf4j
@Service
public class ExportTdDataServiceImpl {
@Autowired
SysEquipmentMapper sysEquipmentMapper;
@Autowired
private TDEngineService tdEngineService;
@Value("${zipUrl}")
private String fileUrl;
public void exportDataCsvZip() throws IOException, SQLException {
StopWatch stopWatch = new StopWatch();
stopWatch.start("导出风机数据zip");
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, -1);
String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
//获取所有风机数据
QueryWrapper<SysEquipment> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("object_type", 10002);
List<SysEquipment> sysEquipments = sysEquipmentMapper.selectList(queryWrapper);
//获取临时目录文件路径
String tempDir = String.valueOf(Files.createTempDirectory(null));
File zipFile = new File(fileUrl + "/data_"+yesterday+".zip");
List<File> fileList = new ArrayList<>();
try {
for (SysEquipment item : sysEquipments) {
String hTableName = "h" + item.getId();
String lTableName = "l" + item.getId();
File fileHighSpeed = new File(tempDir +"/" + "h_"+item.getCode()+"_"+yesterday + ".csv");
File fileLowSpeed = new File(tempDir +"/" + "l_"+item.getCode()+"_"+yesterday + ".csv");
getYesterdayData(hTableName, yesterday, fileHighSpeed.getPath());
getYesterdayData(lTableName, yesterday, fileLowSpeed.getPath());
fileList.add(fileLowSpeed);
fileList.add(fileHighSpeed);
}
//压缩文件
// 将文件添加到压缩文件中
ZipUtil.zip(zipFile, false, fileList.toArray(new File[0]));
stopWatch.stop();
log.debug(stopWatch.prettyPrint(TimeUnit.SECONDS));
}catch (Exception e){
log.error("导出风机数据zip失败{}",e);
}
// finally {
// if (!CollectionUtils.isEmpty(fileList)){
// FileUtil.del(new File(fileList.get(0).getParent()));
// }
// }
}
public void getYesterdayData(String tableName, String time, String csvFilePath) {
// TDengine 查询语句
String query = "SELECT * FROM " + tableName + " WHERE updatetime >= '" + time + " 00:00:00' AND updatetime < '" + time + " 23:59:59.999'";
try (Connection conn = tdEngineService.getHikariDataSource().getConnection();
Statement smt = conn.createStatement();
ResultSet rs = smt.executeQuery(query)) {
writeResultSetToCsv(rs, csvFilePath);
}catch (Exception e){
log.error(tableName +"获取数据异常{}",e);
}
}
private void writeResultSetToCsv(ResultSet resultSet, String csvFilePath) throws IOException, SQLException {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(csvFilePath))) {
// 写入列名
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
writer.write(metaData.getColumnName(i));
if (i < columnCount) writer.write(",");
}
writer.newLine();
// 写入数据
while (resultSet.next()) {
for (int i = 1; i <= columnCount; i++) {
String value = resultSet.getString(i);
writer.write(value == null ? "" : resultSet.getString(i));
if (i < columnCount) writer.write(",");
}
writer.newLine();
}
}catch (Exception e){
log.error("数据写入csv文件失败{}",e);
}
}
}

View File

@ -110,4 +110,6 @@ minio:
url: http://192.168.109.187:9000 url: http://192.168.109.187:9000
bucket: das bucket: das
accessKey: das accessKey: das
secretKey: zaq12WSX secretKey: zaq12WSX
zipUrl: /log/zip/

View File

@ -113,4 +113,6 @@ minio:
bucket: das bucket: das
publicBucket: das-dock publicBucket: das-dock
accessKey: das accessKey: das
secretKey: zaq12WSX secretKey: zaq12WSX
zipUrl: /log/zip/