mysql(47) : 天分区表自动管理

说明
分区字段为int类型,并且为主键,如下示例的date

CREATE TABLE `mytest` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '(id)',
  `gmt_create` datetime DEFAULT NULL,
  `gmt_modify` datetime DEFAULT NULL,
  `name` varchar(50) DEFAULT NULL COMMENT '名称',
  `age` int DEFAULT NULL COMMENT '年龄',
  `sdate` int NOT NULL COMMENT '天(分区键,20231129)',
  PRIMARY KEY (`id`,`sdate`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci AVG_ROW_LENGTH=585 ROW_FORMAT=DYNAMIC COMMENT='';


job


import com.alibaba.gts.cdyj.task.biz.dao.mapper.PartitionManagerDayMapper;
import com.alibaba.gts.flm.common.utils.DateUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Configuration
@Slf4j
public class PartitionManagerDayJob {

    @Resource
    private PartitionManagerDayMapper partitionManagerMapper;

    private static final String prefix = "p_";

    private static final List<Config> configs = new ArrayList<Config>() {{
        add(new Config("mytest", "sdate", 7));
    }};

    @PostConstruct
    private void init() {
        handle();
    }


    /**
     * 0点和12点执行
     */
    @Scheduled(cron = "0 0 0,12 * * ? ")
    public void task() {
        handle();
    }

    public void handle() {
        log.info("分区管理逻辑开始执行... configs.size:{}", configs.size());
        for (Config config : configs) {
            String table = config.getTable();
            String field = config.getField();
            Integer partitionCount = partitionManagerMapper.selectCountPartition(table);
            log.info("table:{} 分区数量:{}", table, partitionCount);
            if (partitionCount > 0) {
                // 已有分区
                /// 创建新分区(创建未来2天分区)
                for (int i = 0; i < 2; i++) {
                    String day = DateUtil.format(System.currentTimeMillis() + 1000 * 60 * 60 * 24 * i, DateUtil.PATTERN_YYYYMMDD);
                    if (!exist(table, prefix + day)) {
                        partitionManagerMapper.createPartition(table, field, prefix + day, day);
                        log.info("table:{} 创建分区:{}", table, prefix + day);
                    }
                }
                long deleteTime = System.currentTimeMillis() - (config.getExpireDay() * 1000L * 60 * 60 * 24);
                String deleteDay = DateUtil.format(deleteTime, DateUtil.PATTERN_YYYYMMDD);
                /// 删除旧分区
                if (exist(table, prefix + deleteDay)) {
                    partitionManagerMapper.deletePartition(table, prefix + deleteDay);
                    log.info("table:{} 删除分区:{}", table, prefix + deleteDay);
                }
            } else {
                // 无分区
                // 初始化分区,初始化当前小时分区
                String currentDay = DateUtil.format(System.currentTimeMillis(), DateUtil.PATTERN_YYYYMMDD);
                partitionManagerMapper.initPartition(table, field, prefix + currentDay, currentDay);
                log.info("table:{} 初始化分区:{}", table, prefix + currentDay);
                // 创建未来4天分区
                for (int i = 0; i < 4; i++) {
                    String day = DateUtil.format(System.currentTimeMillis() + 1000L * 60 * 60 * 24 * i, DateUtil.PATTERN_YYYYMMDD);
                    if (!exist(table, prefix + day)) {
                        partitionManagerMapper.createPartition(table, field, prefix + day, day);
                        log.info("table:{} 创建分区:{}", table, prefix + day);
                    }
                }
            }
        }
        log.info("分区管理逻辑执行完成");
    }

    private Boolean exist(String table, String partitionName) {
        return partitionManagerMapper.selectPartitionIsExist(table, partitionName) > 0;
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Config {
        /* 表名 */
        private String table;
        /* 分区字段 */
        private String field;
        /* 多少小时后过期 */
        private Integer expireDay;
    }
}

mapper


import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * @Author: liyue
 * @Date: 2023/04/25/15:15
 * @Description: 组件库
 */
@Mapper
public interface PartitionManagerDayMapper {

    @Select("SELECT count(1) FROM INFORMATION_SCHEMA.partitions WHERE TABLE_SCHEMA = schema() AND TABLE_NAME=#{tableName}  and PARTITION_NAME is not null;")
    Integer selectCountPartition(@Param("tableName") String tableName);

    @Select("SELECT count(1) FROM INFORMATION_SCHEMA.partitions WHERE TABLE_SCHEMA = schema() AND TABLE_NAME=#{tableName} AND partition_name = #{partitionName};")
    Integer selectPartitionIsExist(@Param("tableName") String tableName, @Param("partitionName") String partitionName);

    @Update("ALTER TABLE ${tableName} DROP PARTITION ${partitionName};")
    int deletePartition(@Param("tableName") String tableName, @Param("partitionName") String partitionName);

    @Update("ALTER TABLE ${table} partition by range (${timeField})( partition ${partitionName} values less than(${partitionVal} ));")
    int initPartition(@Param("table") String table, @Param("timeField") String timeField, @Param("partitionName") String partitionName, @Param("partitionVal") String partitionVal);

    @Update("ALTER TABLE ${table}  ADD PARTITION ( partition ${partitionName} values less than(${partitionVal} ))")
    int createPartition(@Param("table") String table, @Param("timeField") String timeField, @Param("partitionName") String partitionName, @Param("partitionVal") String partitionVal);
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值