当前位置:   article > 正文

ShardSphere算法介绍(实现按年月日分片自定义算法)_时间间隔分片 interval sharding algorithm

时间间隔分片 interval sharding algorithm

内置算法

https://shardingsphere.apache.org/document/5.2.0/cn/user-manual/common-config/builtin-algorithm/sharding/
官网介绍已经很清楚了,但是官网的例子不多,下面是使用时间分片算法的具体例子:

Properties properties = new Properties();
//分片键的时间戳格式,必须遵循 Java DateTimeFormatter 的格式 识别数据
properties.setProperty("datetime-pattern", dateTimePattern);
//设置 起始时间 当前时间 2024-03-27 时间分片下界值,格式与 datetime-pattern 定义的时间戳格式一致
properties.setProperty("datetime-lower", dateTimeLower);
//设置 截止时间 
properties.setProperty("datetime-upper", dateTimeUpper);
//对应数据库表后缀名202211
properties.setProperty("sharding-suffix-pattern", suffixPattern);
//设置时间间隔
properties.setProperty("datetime-interval-unit", "MONTHS"); //MONTHS DAYS YEARS
//设置时间间隔数量
properties.setProperty("datetime-interval-amount", "1");
shardingAlgorithms.put(shardingAlgorithmName, new AlgorithmConfiguration("INTERVAL", properties));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

自定义算法

自定义算法支持2种配置:
首先实现自定义算法类:

@Getter
@CommonsLog
@Component
public class HisDataMonthShardingAlgorithm implements StandardShardingAlgorithm<String> {
   

    private Properties props;
    /**
     * 设置该参数的原因是,如果在范围查找的时候我们没有设置最小值,比如下面的查询
     * where acquisition_time < '2022-08-11 00:00:00'
     * 这个时候范围查找就只有上限而没有下限,这时候就需要有一个下限值兜底,不能一致遍历下去
     */
    private LocalDate tableLowerDate;

    private String suffixPattern;

    private ChronoUnit chronoUnit;

    /**
     * 在配置文件中配置算法的时候会配置 props 参数,框架会将props中的配置放在 properties 参数中,并且初始化算法的时候被调用
     *
     * @param properties properties
     */
    @Override
    public void init(Properties properties) {
   
        this.props = properties;
        String dateTimeLower = properties.getProperty("dateTimeLower");
        suffixPattern = properties.getProperty("suffixPa
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/756772
推荐阅读
相关标签
  

闽ICP备14008679号