132 lines
4.4 KiB
Java
132 lines
4.4 KiB
Java
|
|
package com.common.service;
|
||
|
|
|
||
|
|
import com.common.entity.AppLog;
|
||
|
|
import com.common.entity.AppLogEntity;
|
||
|
|
import lombok.extern.slf4j.Slf4j;
|
||
|
|
import org.springframework.beans.BeanUtils;
|
||
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
|
import org.springframework.stereotype.Service;
|
||
|
|
|
||
|
|
import java.time.Instant;
|
||
|
|
import java.time.LocalDate;
|
||
|
|
import java.time.LocalDateTime;
|
||
|
|
import java.time.ZoneId;
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.stream.Collectors;
|
||
|
|
|
||
|
|
@Slf4j
|
||
|
|
@Service
|
||
|
|
public class EsToDbSyncService {
|
||
|
|
|
||
|
|
@Autowired
|
||
|
|
private ElasticsearchService elasticsearchService;
|
||
|
|
|
||
|
|
@Autowired
|
||
|
|
private ApplogService ApplogService;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 同步指定日期的ES日志到数据库
|
||
|
|
*/
|
||
|
|
public void syncEsLogsToDatabase(LocalDate date) {
|
||
|
|
log.info("开始同步ES日志到数据库,日期: {}", date);
|
||
|
|
|
||
|
|
try {
|
||
|
|
// 从ES获取指定日期的日志
|
||
|
|
List<AppLog> esLogs = elasticsearchService.getLogsByDate(date);
|
||
|
|
|
||
|
|
if (esLogs.isEmpty()) {
|
||
|
|
log.info("ES中未找到 {} 的日志数据", date);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
log.info("从ES索引 applog-{} 获取到 {} 条日志",
|
||
|
|
date.format(java.time.format.DateTimeFormatter.ofPattern("yyyyMMdd")),
|
||
|
|
esLogs.size());
|
||
|
|
|
||
|
|
// 转换为数据库实体
|
||
|
|
List<AppLogEntity> dbLogs = esLogs.stream()
|
||
|
|
.map(this::convertToEntity)
|
||
|
|
.collect(Collectors.toList());
|
||
|
|
|
||
|
|
// 批量保存到数据库(自动去重)
|
||
|
|
int savedCount = ApplogService.batchSaveAppLogsWithDeduplication(dbLogs);
|
||
|
|
|
||
|
|
log.info("同步完成,成功保存 {} 条日志到数据库", savedCount);
|
||
|
|
|
||
|
|
} catch (Exception e) {
|
||
|
|
log.error("同步ES日志到数据库失败,日期: {}", date, e);
|
||
|
|
throw new RuntimeException("同步失败", e);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 同步指定索引的ES日志到数据库
|
||
|
|
*/
|
||
|
|
public void syncEsLogsByIndexToDatabase(String indexName) {
|
||
|
|
log.info("开始同步ES索引 {} 的日志到数据库", indexName);
|
||
|
|
|
||
|
|
try {
|
||
|
|
// 从ES获取指定索引的日志
|
||
|
|
List<AppLog> esLogs = elasticsearchService.getLogsByIndex(indexName);
|
||
|
|
|
||
|
|
if (esLogs.isEmpty()) {
|
||
|
|
log.info("ES索引 {} 中未找到日志数据", indexName);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
log.info("从ES索引 {} 获取到 {} 条日志", indexName, esLogs.size());
|
||
|
|
|
||
|
|
// 转换为数据库实体
|
||
|
|
List<AppLogEntity> dbLogs = esLogs.stream()
|
||
|
|
.map(this::convertToEntity)
|
||
|
|
.collect(Collectors.toList());
|
||
|
|
|
||
|
|
// 批量保存到数据库(自动去重)
|
||
|
|
int savedCount = ApplogService.batchSaveAppLogsWithDeduplication(dbLogs);
|
||
|
|
|
||
|
|
log.info("同步完成,成功保存 {} 条日志到数据库", savedCount);
|
||
|
|
|
||
|
|
} catch (Exception e) {
|
||
|
|
log.error("同步ES索引 {} 的日志到数据库失败", indexName, e);
|
||
|
|
throw new RuntimeException("同步失败", e);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 将ES实体转换为数据库实体
|
||
|
|
*/
|
||
|
|
private AppLogEntity convertToEntity(AppLog esLog) {
|
||
|
|
AppLogEntity entity = new AppLogEntity();
|
||
|
|
|
||
|
|
// 复制ES元数据字段
|
||
|
|
entity.setEsId(esLog.getId());
|
||
|
|
entity.setEsIndex(esLog.getIndex());
|
||
|
|
entity.setEsType(esLog.getType());
|
||
|
|
entity.setEsScore(esLog.getScore());
|
||
|
|
|
||
|
|
// 复制日志内容字段
|
||
|
|
entity.setDtTime(esLog.getDtTime());
|
||
|
|
entity.setCollectTime(esLog.getCollectTime());
|
||
|
|
entity.setLogType(esLog.getLogType());
|
||
|
|
entity.setTraceId(esLog.getTraceId());
|
||
|
|
entity.setMethod(esLog.getMethod());
|
||
|
|
entity.setAppName(esLog.getAppName());
|
||
|
|
entity.setIp(esLog.getIp());
|
||
|
|
entity.setClassName(esLog.getClassName());
|
||
|
|
entity.setEnv(esLog.getEnv());
|
||
|
|
entity.setContent(esLog.getContent());
|
||
|
|
entity.setThreadName(esLog.getThreadName());
|
||
|
|
entity.setLogLevel(esLog.getLogLevel());
|
||
|
|
entity.setSeq(esLog.getSeq());
|
||
|
|
entity.setIndexedAt(esLog.getIndexedAt());
|
||
|
|
|
||
|
|
// 转换时间字段
|
||
|
|
if (esLog.getDtTime() != null) {
|
||
|
|
entity.setLogDate(Instant.ofEpochMilli(esLog.getDtTime())
|
||
|
|
.atZone(ZoneId.systemDefault())
|
||
|
|
.toLocalDateTime());
|
||
|
|
}
|
||
|
|
|
||
|
|
return entity;
|
||
|
|
}
|
||
|
|
}
|