feat(data-align): 新增数据对齐日增量表创建功能

- 新增 CreateTableMapper 接口定义多个创建临时表方法
- 新增 CreateTableMapper.xml 实现具体建表 SQL逻辑
- 修改 CreateTableXxlJob 定时任务,集成表创建逻辑
- 新增 TableConstants 工具类用于构建表名后缀
- 更新 MyBatis 配置文件路径映射及词典配置
- 支持按日期和分片自动创建七种数据对齐相关表结构
This commit is contained in:
2025-10-20 21:43:15 +08:00
parent efd2e51d24
commit f217b8133a
6 changed files with 210 additions and 3 deletions

View File

@@ -20,10 +20,11 @@
<option name="lombokDataAnnotation" value="true" />
<option name="lombokNoArgsConstructor" value="true" />
<option name="mapperAnnotaion" value="true" />
<option name="mapperFilesFolder" value="$PROJECT_DIR$/han-note-auth/src/main/resources/mapperxml" />
<option name="mapperFilesFolder" value="$PROJECT_DIR$/han-note-data-align/src/main/resources/mapperxml" />
<option name="mapperFilesFolderList">
<list>
<option value="$PROJECT_DIR$/han-note-auth/src/main/resources/mapperxml" />
<option value="$PROJECT_DIR$/han-note-data-align/src/main/resources/mapperxml" />
</list>
</option>
<option name="moduleNameToPackageAndPathMap">

View File

@@ -4,6 +4,7 @@
<w>asyn</w>
<w>hannote</w>
<w>hanserwei</w>
<w>jobhandler</w>
<w>nacos</w>
<w>operationlog</w>
<w>rustfs</w>

View File

@@ -0,0 +1,21 @@
package com.hanserwei.hannote.data.align.constant;
public class TableConstants {
/**
* 表名中的分隔符
*/
private static final String TABLE_NAME_SEPARATE = "_";
/**
* 拼接表名后缀
*
* @param hashKey 哈希Keu
* @return 表名后缀
*/
public static String buildTableNameSuffix(String date, int hashKey) {
// 拼接完整的表名
return date + TABLE_NAME_SEPARATE + hashKey;
}
}

View File

@@ -0,0 +1,60 @@
package com.hanserwei.hannote.data.align.domain.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* 自动创建表
*/
@Mapper
public interface CreateTableMapper {
/**
* 创建日增量表:关注数计数变更
*
* @param tableNameSuffix 表名后缀
*/
void createDataAlignFollowingCountTempTable(@Param("tableNameSuffix") String tableNameSuffix);
/**
* 创建日增量表:粉丝数计数变更
*
* @param tableNameSuffix 表名后缀
*/
void createDataAlignFansCountTempTable(@Param("tableNameSuffix") String tableNameSuffix);
/**
* 创建日增量表:笔记收藏数计数变更
*
* @param tableNameSuffix 表名后缀
*/
void createDataAlignNoteCollectCountTempTable(@Param("tableNameSuffix") String tableNameSuffix);
/**
* 创建日增量表:用户被收藏数计数变更
*
* @param tableNameSuffix 表名后缀
*/
void createDataAlignUserCollectCountTempTable(@Param("tableNameSuffix") String tableNameSuffix);
/**
* 创建日增量表:用户被点赞数计数变更
*
* @param tableNameSuffix 表名后缀
*/
void createDataAlignUserLikeCountTempTable(@Param("tableNameSuffix") String tableNameSuffix);
/**
* 创建日增量表:笔记点赞数计数变更
*
* @param tableNameSuffix 表名后缀
*/
void createDataAlignNoteLikeCountTempTable(@Param("tableNameSuffix") String tableNameSuffix);
/**
* 创建日增量表:笔记发布数计数变更
*
* @param tableNameSuffix 表名后缀
*/
void createDataAlignNotePublishCountTempTable(@Param("tableNameSuffix") String tableNameSuffix);
}

View File

@@ -1,20 +1,58 @@
package com.hanserwei.hannote.data.align.job;
import com.hanserwei.hannote.data.align.constant.TableConstants;
import com.hanserwei.hannote.data.align.domain.mapper.CreateTableMapper;
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.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
@Component
@RefreshScope
@SuppressWarnings("unused")
public class CreateTableXxlJob {
@Resource
private CreateTableMapper createTableMapper;
/**
* 表总分片数
*/
@Value("${table.shards}")
private int tableShards;
/**
* 1、简单任务示例Bean模式
*/
@SuppressWarnings("unused")
@XxlJob("createTableJobHandler")
public void createTableJobHandler() throws Exception {
public void createTableJobHandler() {
XxlJobHelper.log("## 开始初始化明日增量数据表...");
String date = LocalDate.now().plusDays(1) // 明日的日期
.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
XxlJobHelper.log("## 开始创建日增量数据表,日期: {}...", date);
if (tableShards > 0) {
for (int hashKey = 0; hashKey < tableShards; hashKey++) {
// 表名后缀
String tableNameSuffix = TableConstants.buildTableNameSuffix(date, hashKey);
// TODO
// 创建表
// 创建表
createTableMapper.createDataAlignFollowingCountTempTable(tableNameSuffix);
createTableMapper.createDataAlignFansCountTempTable(tableNameSuffix);
createTableMapper.createDataAlignNoteCollectCountTempTable(tableNameSuffix);
createTableMapper.createDataAlignUserCollectCountTempTable(tableNameSuffix);
createTableMapper.createDataAlignUserLikeCountTempTable(tableNameSuffix);
createTableMapper.createDataAlignNoteLikeCountTempTable(tableNameSuffix);
createTableMapper.createDataAlignNotePublishCountTempTable(tableNameSuffix);
}
}
XxlJobHelper.log("## 创建日增量数据表成功,表名后缀: {}...", date);
}
}

View File

@@ -0,0 +1,86 @@
<?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.CreateTableMapper">
<insert id="createDataAlignFollowingCountTempTable" parameterType="map">
CREATE TABLE IF NOT EXISTS `t_data_align_following_count_temp_${tableNameSuffix}`
(
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`user_id` bigint unsigned NOT NULL COMMENT '用户ID',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `uk_user_id` (`user_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_unicode_ci COMMENT ='数据对齐日增量表:关注数';
</insert>
<insert id="createDataAlignFansCountTempTable" parameterType="map">
CREATE TABLE IF NOT EXISTS `t_data_align_fans_count_temp_${tableNameSuffix}`
(
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`user_id` bigint unsigned NOT NULL COMMENT '用户ID',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `uk_user_id` (`user_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_unicode_ci COMMENT ='数据对齐日增量表:粉丝数';
</insert>
<insert id="createDataAlignNoteCollectCountTempTable" parameterType="map">
CREATE TABLE IF NOT EXISTS `t_data_align_note_collect_count_temp_${tableNameSuffix}`
(
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`note_id` bigint unsigned NOT NULL COMMENT '笔记ID',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `uk_note_id` (`note_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_unicode_ci COMMENT ='数据对齐日增量表:笔记获得收藏数';
</insert>
<insert id="createDataAlignUserCollectCountTempTable" parameterType="map">
CREATE TABLE IF NOT EXISTS `t_data_align_user_collect_count_temp_${tableNameSuffix}`
(
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`user_id` bigint unsigned NOT NULL COMMENT '用户ID',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `uk_user_id` (`user_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_unicode_ci COMMENT ='数据对齐日增量表:用户获得收藏数';
</insert>
<insert id="createDataAlignUserLikeCountTempTable" parameterType="map">
CREATE TABLE IF NOT EXISTS `t_data_align_user_like_count_temp_${tableNameSuffix}`
(
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`user_id` bigint unsigned NOT NULL COMMENT '用户ID',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `uk_user_id` (`user_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_unicode_ci COMMENT ='数据对齐日增量表:用户获得点赞数';
</insert>
<insert id="createDataAlignNoteLikeCountTempTable" parameterType="map">
CREATE TABLE IF NOT EXISTS `t_data_align_note_like_count_temp_${tableNameSuffix}`
(
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`note_id` bigint unsigned NOT NULL COMMENT '笔记ID',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `uk_note_id` (`note_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_unicode_ci COMMENT ='数据对齐日增量表:笔记获得点赞数';
</insert>
<insert id="createDataAlignNotePublishCountTempTable" parameterType="map">
CREATE TABLE IF NOT EXISTS `t_data_align_note_publish_count_temp_${tableNameSuffix}`
(
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`note_id` bigint unsigned NOT NULL COMMENT '笔记ID',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `uk_note_id` (`note_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_unicode_ci COMMENT ='数据对齐日增量表:用户发布笔记数';
</insert>
</mapper>