feat(data-align): 添加删除日增量临时表功能
- 新增 DeleteTableMapper 接口及 XML 映射文件 - 实现删除多种日增量表的 SQL 语句- 创建 DeleteTableXxlJob 定时任务 - 支持按日期和分片批量删除临时表- 集成 XXL-JOB 执行日志记录 - 自动清理最近一个月的历史数据表
This commit is contained in:
@@ -0,0 +1,58 @@
|
|||||||
|
package com.hanserwei.hannote.data.align.domain.mapper;
|
||||||
|
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除表
|
||||||
|
*/
|
||||||
|
public interface DeleteTableMapper {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除日增量表:关注数计数变更
|
||||||
|
*
|
||||||
|
* @param tableNameSuffix 表名后缀
|
||||||
|
*/
|
||||||
|
void deleteDataAlignFollowingCountTempTable(@Param("tableNameSuffix") String tableNameSuffix);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除日增量表:粉丝数计数变更
|
||||||
|
*
|
||||||
|
* @param tableNameSuffix 表名后缀
|
||||||
|
*/
|
||||||
|
void deleteDataAlignFansCountTempTable(@Param("tableNameSuffix") String tableNameSuffix);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除日增量表:笔记收藏数计数变更
|
||||||
|
*
|
||||||
|
* @param tableNameSuffix 表名后缀
|
||||||
|
*/
|
||||||
|
void deleteDataAlignNoteCollectCountTempTable(@Param("tableNameSuffix") String tableNameSuffix);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除日增量表:用户被收藏数计数变更
|
||||||
|
*
|
||||||
|
* @param tableNameSuffix 表名后缀
|
||||||
|
*/
|
||||||
|
void deleteDataAlignUserCollectCountTempTable(@Param("tableNameSuffix") String tableNameSuffix);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除日增量表:用户被点赞数计数变更
|
||||||
|
*
|
||||||
|
* @param tableNameSuffix 表名后缀
|
||||||
|
*/
|
||||||
|
void deleteDataAlignUserLikeCountTempTable(@Param("tableNameSuffix") String tableNameSuffix);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除日增量表:笔记点赞数计数变更
|
||||||
|
*
|
||||||
|
* @param tableNameSuffix 表名后缀
|
||||||
|
*/
|
||||||
|
void deleteDataAlignNoteLikeCountTempTable(@Param("tableNameSuffix") String tableNameSuffix);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除日增量表:笔记发布数计数变更
|
||||||
|
*
|
||||||
|
* @param tableNameSuffix 表名后缀
|
||||||
|
*/
|
||||||
|
void deleteDataAlignNotePublishCountTempTable(@Param("tableNameSuffix") String tableNameSuffix);
|
||||||
|
}
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
package com.hanserwei.hannote.data.align.job;
|
||||||
|
|
||||||
|
import com.hanserwei.hannote.data.align.constant.TableConstants;
|
||||||
|
import com.hanserwei.hannote.data.align.domain.mapper.DeleteTableMapper;
|
||||||
|
import com.xxl.job.core.context.XxlJobHelper;
|
||||||
|
import com.xxl.job.core.handler.annotation.XxlJob;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除最近一个月的日增量临时表
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class DeleteTableXxlJob {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 表总分片数
|
||||||
|
*/
|
||||||
|
@Value("${table.shards}")
|
||||||
|
private int tableShards;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private DeleteTableMapper deleteTableMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 1、简单任务示例(Bean模式)
|
||||||
|
*/
|
||||||
|
@XxlJob("deleteTableJobHandler")
|
||||||
|
public void deleteTableJobHandler() {
|
||||||
|
XxlJobHelper.log("## 开始删除最近一个月的日增量临时表");
|
||||||
|
// 今日
|
||||||
|
LocalDate today = LocalDate.now();
|
||||||
|
|
||||||
|
// 日期格式
|
||||||
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
|
||||||
|
|
||||||
|
LocalDate startDate = today;
|
||||||
|
// 从昨天开始往前推一个月
|
||||||
|
LocalDate endDate = today.minusMonths(1);
|
||||||
|
|
||||||
|
// 循环最近一个月的日期,不包括今天
|
||||||
|
while (startDate.isAfter(endDate)) {
|
||||||
|
// 往前推一天
|
||||||
|
startDate = startDate.minusDays(1);
|
||||||
|
// 日期字符串
|
||||||
|
String date = startDate.format(formatter);
|
||||||
|
|
||||||
|
for (int hashKey = 0; hashKey < tableShards; hashKey++) {
|
||||||
|
// 表名后缀
|
||||||
|
String tableNameSuffix = TableConstants.buildTableNameSuffix(date, hashKey);
|
||||||
|
XxlJobHelper.log("删除表后缀: {}", tableNameSuffix);
|
||||||
|
|
||||||
|
// 删除表
|
||||||
|
deleteTableMapper.deleteDataAlignFollowingCountTempTable(tableNameSuffix);
|
||||||
|
deleteTableMapper.deleteDataAlignFansCountTempTable(tableNameSuffix);
|
||||||
|
deleteTableMapper.deleteDataAlignNoteCollectCountTempTable(tableNameSuffix);
|
||||||
|
deleteTableMapper.deleteDataAlignUserCollectCountTempTable(tableNameSuffix);
|
||||||
|
deleteTableMapper.deleteDataAlignUserLikeCountTempTable(tableNameSuffix);
|
||||||
|
deleteTableMapper.deleteDataAlignNoteLikeCountTempTable(tableNameSuffix);
|
||||||
|
deleteTableMapper.deleteDataAlignNotePublishCountTempTable(tableNameSuffix);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
XxlJobHelper.log("## 结束删除最近一个月的日增量临时表");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||||
|
<mapper namespace="com.hanserwei.hannote.data.align.domain.mapper.DeleteTableMapper">
|
||||||
|
<delete id="deleteDataAlignFollowingCountTempTable" parameterType="map">
|
||||||
|
DROP TABLE IF EXISTS `t_data_align_following_count_temp_${tableNameSuffix}`;
|
||||||
|
</delete>
|
||||||
|
|
||||||
|
<delete id="deleteDataAlignFansCountTempTable" parameterType="map">
|
||||||
|
DROP TABLE IF EXISTS `t_data_align_fans_count_temp_${tableNameSuffix}`;
|
||||||
|
</delete>
|
||||||
|
|
||||||
|
<delete id="deleteDataAlignNoteCollectCountTempTable" parameterType="map">
|
||||||
|
DROP TABLE IF EXISTS `t_data_align_note_collect_count_temp_${tableNameSuffix}`;
|
||||||
|
</delete>
|
||||||
|
|
||||||
|
<delete id="deleteDataAlignUserCollectCountTempTable" parameterType="map">
|
||||||
|
DROP TABLE IF EXISTS `t_data_align_user_collect_count_temp_${tableNameSuffix}`;
|
||||||
|
</delete>
|
||||||
|
|
||||||
|
<delete id="deleteDataAlignUserLikeCountTempTable" parameterType="map">
|
||||||
|
DROP TABLE IF EXISTS `t_data_align_user_like_count_temp_${tableNameSuffix}`;
|
||||||
|
</delete>
|
||||||
|
|
||||||
|
<delete id="deleteDataAlignNoteLikeCountTempTable" parameterType="map">
|
||||||
|
DROP TABLE IF EXISTS `t_data_align_note_like_count_temp_${tableNameSuffix}`;
|
||||||
|
</delete>
|
||||||
|
|
||||||
|
<delete id="deleteDataAlignNotePublishCountTempTable" parameterType="map">
|
||||||
|
DROP TABLE IF EXISTS `t_data_align_note_publish_count_temp_${tableNameSuffix}`;
|
||||||
|
</delete>
|
||||||
|
</mapper>
|
||||||
Reference in New Issue
Block a user