es数据同步

This commit is contained in:
yang
2024-12-13 20:20:49 +08:00
parent 09f64bdb08
commit cb8a333b4f
4 changed files with 108 additions and 302 deletions

View File

@@ -21,10 +21,12 @@ import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
@@ -42,64 +44,68 @@ public class PhpOnlineStudyRecordScheduledTasks {
@Resource
RestHighLevelClient restHighLevelClient;
@Value("${spring.profiles.active}")
private String activeProfile;
// todo 定时、分批、数据库名
@XxlJob("phpOnlineStudyRecordSyncEs")
public List<CourseStudyDto> phpOnlineStudyRecordSyncEs(Long syncTimePoint, Integer isOnlyRead) throws IOException {
@XxlJob("phpOnlineStudyRecordSyncEsTask")
public List<CourseStudyDto> phpOnlineStudyRecordSyncEs(Long syncTimePointOfBegin, Long syncTimePointOfEnd, Integer isOnlyRead) throws IOException {
String sql =
"SELECT\n" +
" elc.kid AS courseId,\n" +
" elcr.user_id AS userIdOfPhp,\n" +
" COUNT(1) AS modNum,\n" +
" SUM(CASE WHEN elrc.kid IS NOT NULL THEN 1 ELSE 0 END) AS completeNum\n" +
"FROM\n" +
" elearninglms.eln_ln_course elc\n" +
" INNER JOIN elearninglms.eln_ln_course_reg elcr \n" +
" ON elc.kid = elcr.course_id\n" +
" INNER JOIN (\n" +
" SELECT\n" +
" user_id,\n" +
" course_id \n" +
" FROM\n" +
" elearninglms.eln_ln_res_complete\n" +
" WHERE\n" +
" complete_type = '1'\n" +
" AND complete_status = '2'\n" +
" AND updated_at > ?1\n" +
" AND is_deleted = 0\n" +
" GROUP BY\n" +
" user_id,\n" +
" course_id\n" +
" ) recentFinishStuent \n" +
" ON recentFinishStuent.user_id = elcr.user_id \n" +
" AND recentFinishStuent.course_id = elcr.course_id\n" +
" INNER JOIN elearninglms.eln_ln_mod_res elms \n" +
" ON elms.course_id = elcr.course_id\n" +
" LEFT JOIN elearninglms.eln_ln_res_complete elrc \n" +
" ON elrc.mod_res_id = elms.kid\n" +
" AND elrc.user_id = elcr.user_id\n" +
" AND elrc.complete_type = '1'\n" +
" AND elrc.complete_status = '2'\n" +
"WHERE\n" +
" elc.is_deleted = 0\n" +
" AND elcr.is_deleted = 0\n" +
" AND elcr.reg_state = '1'\n" +
" AND elms.publish_status = '1'\n" +
" AND elms.is_deleted = '0'\n" +
"GROUP BY\n" +
" elc.kid,\n" +
" elcr.user_id\n" +
"HAVING\n" +
" completeNum = modNum\n";
" elc.kid AS courseId,\n" +
" elcr.user_id AS userIdOfPhp,\n" +
" ROUND((SUM(CASE WHEN elrc.kid IS NOT NULL THEN 1 ELSE 0 END) / COUNT(1)) * 100, 0) AS progress\n" +
"FROM\n" +
" elearninglms.eln_ln_course elc\n" +
" INNER JOIN elearninglms.eln_ln_course_reg elcr \n" +
" ON elc.kid = elcr.course_id\n" +
" INNER JOIN (\n" +
" SELECT\n" +
" user_id,\n" +
" course_id \n" +
" FROM\n" +
" elearninglms.eln_ln_res_complete\n" +
" WHERE\n" +
" complete_type = '1'\n" +
" AND complete_status = '2'\n" +
" AND updated_at > ?1 AND updated_at < ?2\n" +
" AND is_deleted = 0\n" +
" GROUP BY\n" +
" user_id,\n" +
" course_id\n" +
" ) recentFinishStuent \n" +
" ON recentFinishStuent.user_id = elcr.user_id \n" +
" AND recentFinishStuent.course_id = elcr.course_id\n" +
" INNER JOIN elearninglms.eln_ln_mod_res elms \n" +
" ON elms.course_id = elcr.course_id\n" +
" LEFT JOIN elearninglms.eln_ln_res_complete elrc \n" +
" ON elrc.mod_res_id = elms.kid\n" +
" AND elrc.user_id = elcr.user_id\n" +
" AND elrc.complete_type = '1'\n" +
" AND elrc.complete_status = '2'\n" +
"WHERE\n" +
" elc.is_deleted = 0\n" +
" AND elcr.is_deleted = 0\n" +
" AND elcr.reg_state = '1'\n" +
" AND elms.publish_status = '1'\n" +
" AND elms.is_deleted = '0'\n" +
"GROUP BY\n" +
" elc.kid,\n" +
" elcr.user_id";
log.info("开始同步PHP学习记录到ES");
// 增量获取PHP中所有已完成的课程
if (syncTimePoint == null) {
LocalDateTime halfAnHourAgo = LocalDateTime.now().minusMinutes(30);
syncTimePoint = halfAnHourAgo.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
if (syncTimePointOfBegin == null || syncTimePointOfEnd == null) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime halfAnHourAgo = now.minusMinutes(30);
syncTimePointOfBegin = halfAnHourAgo.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
syncTimePointOfEnd = now.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
log.info("同步时间点:{}", formatter.format(halfAnHourAgo));
log.info("同步时间点:{}", formatter.format(halfAnHourAgo));
log.info("同步时间终点:{}", formatter.format(now));
}
List<Object[]> objectList1 = phpOnlineCourseDao.sqlFindList(sql, syncTimePoint);
List<Object[]> objectList1 = phpOnlineCourseDao.sqlFindList(sql, syncTimePointOfBegin,syncTimePointOfEnd);
if (CollUtil.isEmpty(objectList1)) {
log.info("没有找到已完成的数据");
@@ -108,19 +114,29 @@ public class PhpOnlineStudyRecordScheduledTasks {
String indexName = "new_study_resource";
List<PhpOnlineDto> finishedCourseList = new ArrayList<>();
List<PhpOnlineDto> recentLearnRecordList = new ArrayList<>();
for (Object[] objects : objectList1) {
String courseId = objects[0].toString();
String userIdOfPhp = objects[1].toString();
Integer progress = ((BigDecimal) objects[2]).intValue();
PhpOnlineDto phpOnlineDto = new PhpOnlineDto();
phpOnlineDto.setCourseId(courseId);
phpOnlineDto.setUserIdOfPhp(userIdOfPhp);
finishedCourseList.add(phpOnlineDto);
phpOnlineDto.setProgress(progress);
recentLearnRecordList.add(phpOnlineDto);
}
// 拼接获取所有新系统用户id这里不选择与上面的联表查询有效率问题
String userBasicDataBase;
if (activeProfile.equals("prod")) {
userBasicDataBase = "user_basic";
} else {
userBasicDataBase = "userbasic";
}
List<String> userIds = objectList1.stream().map(objects -> String.valueOf(objects[1])).distinct().collect(Collectors.toList());
List<Object[]> objectList2 = phpOnlineCourseDao.sqlFindList("select kid,user_id from user_basic.user_account where kid in (?1) and deleted=0 and account_status = 0", userIds);
List<Object[]> objectList2 = phpOnlineCourseDao.sqlFindList("select kid,user_id from " + userBasicDataBase + ".user_account where kid in (?1) and deleted=0 and account_status = 0", userIds);
if (CollUtil.isEmpty(objectList1)) {
log.info("新系统用户数据不存在");
@@ -130,7 +146,7 @@ public class PhpOnlineStudyRecordScheduledTasks {
Map<Object, Object> userIdToKidMap = objectList2.stream().collect(Collectors.toMap(object -> object[0], object -> object[1]));
// 设置新系统用户ID
finishedCourseList = finishedCourseList.stream()
recentLearnRecordList = recentLearnRecordList.stream()
.map(phpOnlineDto -> {
Object userIdOfJavaObj = userIdToKidMap.get(phpOnlineDto.getUserIdOfPhp());
if (userIdOfJavaObj != null) {
@@ -142,21 +158,43 @@ public class PhpOnlineStudyRecordScheduledTasks {
.collect(Collectors.toList());
// 获取ES中没有完成的的课程学习记录
List<CourseStudyDto> notFinishedCourseList = getEsData(finishedCourseList);
// 获取ES中的数据
List<CourseStudyDto> esDataList = getEsData(recentLearnRecordList);
// 构建映射关系
Map<String, PhpOnlineDto> map = recentLearnRecordList.stream()
.collect(Collectors.toMap(
phpOnlineDto -> phpOnlineDto.getUserIdOfJava() + "-" + phpOnlineDto.getCourseId(),
phpOnlineDto -> phpOnlineDto
));
List<CourseStudyDto> toBeUpdatedEs = esDataList.stream()
.map(esDataItem -> {
String key = esDataItem.getAccountId() + "-" + esDataItem.getCourseId();
PhpOnlineDto phpOnlineDto = map.get(key);
// 如果找到相应的 PhpOnlineDto 且进度有变化,则进行更新
if (phpOnlineDto != null && !esDataItem.getProgress().equals(phpOnlineDto.getProgress())) {
esDataItem.setProgress(phpOnlineDto.getProgress());
if (phpOnlineDto.getProgress() == 100){
esDataItem.setStatus(9);
} else {
esDataItem.setStatus(2);
}
return esDataItem;
}
return null; // 返回 null 表示不需要更新
})
.filter(Objects::nonNull) // 去掉返回为 null 的项
.collect(Collectors.toList());
if (isOnlyRead != null && isOnlyRead == 1) {
return notFinishedCourseList;
return toBeUpdatedEs;
}
List<CourseStudyDto> toBeUpdatedEs = notFinishedCourseList.stream().map(courseStudyDto -> {
courseStudyDto.setStatus(9);
courseStudyDto.setProgress(100);
return courseStudyDto;
}).collect(Collectors.toList());
// 更新ES中的未同步为完成的学习记录
toBeUpdatedEs(toBeUpdatedEs, indexName);
// toBeUpdatedEs(toBeUpdatedEs, indexName);
return null;
}

View File

@@ -1,215 +0,0 @@
package com.xboe.school.study.api;
import cn.hutool.core.collection.CollUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xboe.module.course.dto.CourseStudyDto;
import com.xboe.school.study.dao.PhpOnlineCourseDao;
import com.xboe.school.study.dto.PhpOnlineDto;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@Component
@Slf4j
public class PhpOnlineStudyRecordScheduledTasks2 {
@Resource
private PhpOnlineCourseDao phpOnlineCourseDao;
@Resource
RestHighLevelClient restHighLevelClient;
// todo 定时、分批、数据库名
@XxlJob("phpOnlineStudyRecordSyncEs")
public List<CourseStudyDto> phpOnlineStudyRecordSyncEs(Long syncTimePoint, Integer isOnlyRead) throws IOException {
String sql =
"SELECT\n" +
" course_id,user_id,status,progress \n" +
"FROM\n" +
" elearninglms.eln_ln_res_complete \n" +
"WHERE\n" +
" complete_type = '1' \n" +
" AND complete_status = '2' \n" +
" AND updated_at > ?1 \n" +
" AND is_deleted=0\n" +
"GROUP BY\n" +
" course_id,user_id";
log.info("开始同步PHP学习记录到ES");
// 增量获取PHP中所有已完成的课程
if (syncTimePoint == null) {
LocalDateTime halfAnHourAgo = LocalDateTime.now().minusMinutes(30);
syncTimePoint = halfAnHourAgo.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
log.info("同步时间点:{}", formatter.format(halfAnHourAgo));
}
List<Object[]> objectList1 = phpOnlineCourseDao.sqlFindList(sql, syncTimePoint);
if (CollUtil.isEmpty(objectList1)) {
log.info("没有找到已完成的数据");
return null;
}
String indexName = "new_study_resource";
List<PhpOnlineDto> recentLearnRecordList = new ArrayList<>();
for (Object[] objects : objectList1) {
String courseId = objects[0].toString();
String userIdOfPhp = objects[1].toString();
int status = Integer.parseInt(objects[2].toString());
int progress = Integer.parseInt(objects[3].toString());
PhpOnlineDto phpOnlineDto = new PhpOnlineDto();
phpOnlineDto.setCourseId(courseId);
phpOnlineDto.setUserIdOfPhp(userIdOfPhp);
phpOnlineDto.setProgress(progress);
phpOnlineDto.setStatus(status);
recentLearnRecordList.add(phpOnlineDto);
}
// 拼接获取所有新系统用户id这里不选择与上面的联表查询有效率问题
List<String> userIds = objectList1.stream().map(objects -> String.valueOf(objects[1])).distinct().collect(Collectors.toList());
List<Object[]> objectList2 = phpOnlineCourseDao.sqlFindList("select kid,user_id from user_basic.user_account where kid in (?1) and deleted=0 and account_status = 0", userIds);
if (CollUtil.isEmpty(objectList1)) {
log.info("新系统用户数据不存在");
return null;
}
Map<Object, Object> userIdToKidMap = objectList2.stream().collect(Collectors.toMap(object -> object[0], object -> object[1]));
// 设置新系统用户ID
recentLearnRecordList = recentLearnRecordList.stream()
.map(phpOnlineDto -> {
Object userIdOfJavaObj = userIdToKidMap.get(phpOnlineDto.getUserIdOfPhp());
if (userIdOfJavaObj != null) {
phpOnlineDto.setUserIdOfJava(userIdOfJavaObj.toString());
}
return phpOnlineDto;
})
.filter(phpOnlineDto -> phpOnlineDto.getUserIdOfJava() != null)
.collect(Collectors.toList());
// 获取ES中没有完成的的课程学习记录
List<CourseStudyDto> esData = getEsData(recentLearnRecordList);
Map<String, PhpOnlineDto> map = recentLearnRecordList.stream().collect(Collectors.toMap(phpOnlineDto -> phpOnlineDto.getUserIdOfJava() + phpOnlineDto.getCourseId(), phpOnlineDto -> phpOnlineDto));
List<CourseStudyDto> toBeUpdatedEs = esData.stream().filter(courseStudyDto -> {
if (map.containsKey(courseStudyDto.getAccountId())) {
PhpOnlineDto phpOnlineDto = map.get(courseStudyDto.getAccountId());
if (!phpOnlineDto.getStatus().equals(phpOnlineDto.getStatus()) || !phpOnlineDto.getProgress().equals(phpOnlineDto.getProgress())) {
return true;
}
}
return false;
}).collect(Collectors.toList());
if (isOnlyRead != null && isOnlyRead == 1) {
return esData;
}
// 更新ES中的未同步为完成的学习记录
toBeUpdatedEs(toBeUpdatedEs, indexName);
return null;
}
private List<CourseStudyDto> getEsData(List<PhpOnlineDto> finishedCourseList) throws IOException {
SearchRequest searchRequest = new SearchRequest("new_study_resource");
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
for (PhpOnlineDto phpOnlineDto : finishedCourseList) {
if (phpOnlineDto.getUserIdOfJava() == null || phpOnlineDto.getCourseId() == null) {
continue;
}
boolQuery.should(QueryBuilders.boolQuery()
.must(QueryBuilders.termQuery("courseId.keyword", phpOnlineDto.getCourseId()))
.must(QueryBuilders.termQuery("accountId.keyword", phpOnlineDto.getUserIdOfJava()))
);
}
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(boolQuery).timeout(new TimeValue(60, TimeUnit.SECONDS));
searchRequest.source(sourceBuilder);
if (boolQuery.hasClauses()) {
sourceBuilder.query(boolQuery);
}
searchRequest.source(sourceBuilder);
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
List<CourseStudyDto> courseStudyDtoList = new ArrayList<>();
ObjectMapper mapper = new ObjectMapper();
for (SearchHit hit : hits) {
String sourceAsString = hit.getSourceAsString();
try {
CourseStudyDto cft = mapper.readValue(sourceAsString, CourseStudyDto.class);
courseStudyDtoList.add(cft);
} catch (Exception e) {
log.error("转化json到对应失败", sourceAsString);
}
}
return courseStudyDtoList;
}
private void toBeUpdatedEs(List<CourseStudyDto> toBeUpdatedEsData, String indexName) {
if (CollUtil.isEmpty(toBeUpdatedEsData)) {
return;
}
BulkRequest bulkRequest = new BulkRequest();
for (CourseStudyDto courseStudyDto : toBeUpdatedEsData) {
Map<String, Object> docMap = new HashMap<>();
docMap.put("status", courseStudyDto.getStatus());
docMap.put("progress", courseStudyDto.getProgress());
// 创建更新请求并传入单一的docMap
UpdateRequest updateRequest = new UpdateRequest(indexName, courseStudyDto.getId())
.doc(docMap, XContentType.JSON);
// 将请求添加到批量请求中
bulkRequest.add(updateRequest);
}
try {
// 执行批量更新操作
BulkResponse bulkResponse = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
// 检查是否有失败的操作
if (bulkResponse.hasFailures()) {
log.error("批量更新失败: {}", bulkResponse.buildFailureMessage());
} else {
List<String> collect = toBeUpdatedEsData.stream().map(CourseStudyDto::getId).collect(Collectors.toList());
log.info("批量更新成功更新的ES ID列表: {}", collect);
}
} catch (IOException e) {
log.error("执行批量更新时发生错误", e);
}
}
}

View File

@@ -45,8 +45,6 @@ public class StudyCourseESApi extends ApiBaseController{
@Resource
private PhpOnlineStudyRecordScheduledTasks phpOnlineStudyRecordScheduledTasks;
@Resource
private PhpOnlineStudyRecordScheduledTasks2 phpOnlineStudyRecordScheduledTasks2;
@RequestMapping(value="/search",method = {RequestMethod.GET,RequestMethod.POST})
public JsonResponse<PageList<CourseStudyDto>> search(Pagination page, CourseStudyDto dto){
@@ -170,30 +168,12 @@ public class StudyCourseESApi extends ApiBaseController{
return success(true);
}
/**
*
* @param syncTimePoint
* @param isOnlyRead 0 更新ES 1 查询ES
* @return
* @throws IOException
*/
@PostMapping("/phpOnlineStudyRecordSyncEs")
public JsonResponse<List<CourseStudyDto>> phpOnlineStudyRecordSyncEs(Long syncTimePoint,Integer isOnlyRead) throws IOException {
List<CourseStudyDto> courseStudyDtoList = phpOnlineStudyRecordScheduledTasks.phpOnlineStudyRecordSyncEs(syncTimePoint, isOnlyRead);
public JsonResponse<List<CourseStudyDto>> phpOnlineStudyRecordSyncEs(Long syncTimePointOfBegin, Long syncTimePointOfEnd, Integer isOnlyRead) throws IOException {
List<CourseStudyDto> courseStudyDtoList = phpOnlineStudyRecordScheduledTasks.phpOnlineStudyRecordSyncEs(syncTimePointOfBegin,syncTimePointOfEnd, isOnlyRead);
return success(courseStudyDtoList);
}
/**
*
* @param syncTimePoint
* @param isOnlyRead 0 更新ES 1 查询ES
* @return
* @throws IOException
*/
@PostMapping("/phpOnlineStudyRecordSyncEs2")
public JsonResponse<List<CourseStudyDto>> phpOnlineStudyRecordSyncEs2(Long syncTimePoint,Integer isOnlyRead) throws IOException {
List<CourseStudyDto> courseStudyDtoList = phpOnlineStudyRecordScheduledTasks2.phpOnlineStudyRecordSyncEs(syncTimePoint, isOnlyRead);
return success(courseStudyDtoList);
}
}

View File

@@ -25,6 +25,9 @@ public class PhpOnlineDto {
* 课程名称
*/
private String userIdOfJava;
private Integer status;
/**
* 进度
*/
private Integer progress;
}