java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java sharding-jdbc按年月分片

Java中sharding-jdbc按年月分片的示例代码

作者:我的芒果

本文主要介绍了Java中sharding-jdbc按年月分片的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

Pom依赖

        <!--shardingjdbc分片,和Druid不兼容,如果不使用sharding则需要注释-->
        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.1.1</version>
        </dependency>

Yml配置

spring:
  autoconfigure:
    exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
  main:
    allow-bean-definition-overriding: true
  shardingsphere:
    #配置数据源
    datasource:
      names: ds-master
      ds-master:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://***:3306/aihosp?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=GMT%2B8&rewriteBatchedStatements=true
        username: ***
        password: ***
    sharding:
      tables:
        table1:
          actual-data-nodes: ds-master.table1_$->{2021..2025}  #按年分表
          tableStrategy:
            standard: #用于单分片键的标准分片场景
              sharding-column: create_time
              precise-algorithm-class-name: com.**.common.algorithm.PreciseRangeShardingAlgorithm # 精确分片算法类名称,用于=和IN。该类需实现PreciseShardingAlgorithm接口并提供无参数的构造器
              range-algorithm-class-name: com.**.common.algorithm.PreciseRangeShardingAlgorithm #范围分片算法类名称,用于BETWEEN,可选。该类需实现RangeShardingAlgorithm接口并提供无参数的构造器
          key-generator:
            column: id
            type: SNOWFLAKE #分布式全局ID(雪花算法)
            retry-interval-milliseconds: 500
        table2:
          actual-data-nodes: ds-master.table2_$->{2022..2025}0$->{1..9},ds-master.table2_$->{2022..2025}1$->{0..2}  #按月分表
          tableStrategy:
            standard: #用于单分片键的标准分片场景
              sharding-column: create_date
              precise-algorithm-class-name: com.**.common.algorithm.PreciseRangeShardingAlgorithm
              range-algorithm-class-name: com.**.common.algorithm.PreciseRangeShardingAlgorithm
          key-generator:
            column: id
            type: SNOWFLAKE #分布式全局ID(雪花算法)
            retry-interval-milliseconds: 500
    #其他运行属性
    props:
      sql:
        show: false # 是否显示日志

时间策略

/**
 *
 * 按年分片
 * 精准分库PreciseShardingDBAlgorithm
 *
 * 范围分库RangeShardingDBAlgorithm
 *
 * 精准分表PreciseShardingTableAlgorithm
 *
 * 范围分表RangeShardingTableAlgorithm:
 */
@Slf4j
public class PreciseRangeShardingAlgorithm implements PreciseShardingAlgorithm<String>,RangeShardingAlgorithm<String> {
    /**
     *  RangeShardingAlgorithm的重写  根据传入的分片健的值,对所有待选择的表中 根据自己的业务逻辑进行判断,选择符合条件的表返回
     * @param tableNameList 返回需要查询的表
     * @param shardingValue 传入的分片健的值
     * @return 返回符合条件的表名称
     */
    @Override
    public Collection<String> doSharding(Collection<String> tableNameList, RangeShardingValue<String> shardingValue) {
        System.out.println("[MyTableRangeShardingAlgorithm] shardingValue: [{}]\n"+ shardingValue);
        Set<String> tableNameResultList = new LinkedHashSet<>();
        Range<String> rangeValue = shardingValue.getValueRange();
        String flag = "year";
        for (String tableName : tableNameList) {
            if (tableName.startsWith("table2")) {
                flag = "month";
                break;
            }
        }
        if ("year".equals(flag)) {
            int lowInt = Integer.parseInt(rangeValue.lowerEndpoint().substring(0,5).replaceAll("-",""));
            int upperInt = Integer.parseInt(rangeValue.upperEndpoint().substring(0,5).replaceAll("-",""));
            for (String tableNameItem : tableNameList) {
                String substring = tableNameItem.substring(tableNameItem.length() - 4);
                int tableItem = Integer.valueOf(substring);
                if(tableItem >=  lowInt && tableItem <= upperInt ){
                    tableNameResultList.add(tableNameItem);
                }
            }
        } else if ("month".equals(flag)) {
            int lowInt = Integer.parseInt(rangeValue.lowerEndpoint().substring(0,7).replaceAll("-",""));
            int upperInt = Integer.parseInt(rangeValue.upperEndpoint().substring(0,7).replaceAll("-",""));
            for (String tableNameItem : tableNameList) {
                String substring = tableNameItem.substring(tableNameItem.length() - 6,tableNameItem.length());
                int tableItem = Integer.valueOf(substring);
                if(tableItem >=  lowInt && tableItem <= upperInt ){
                    tableNameResultList.add(tableNameItem);
                }
            }
        }
        return tableNameResultList;
    }
    /** PreciseShardingAlgorithm的重写 */
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<String> preciseShardingValue) {
        String s = buildShardingTable(preciseShardingValue.getLogicTableName(), preciseShardingValue.getValue());
        return s;
    }
    /**
     * 构建分片后的表名
     * @param logicTableName
     * @param date
     * @return
     */
    private String buildShardingTable(String logicTableName, String date) {
        StringBuffer stringBuffer = new StringBuffer(logicTableName).append("_").append(date, 0, 4);
        if (logicTableName.startsWith("table2")) {
            // 月分表
           stringBuffer = new StringBuffer(logicTableName).append("_").append(date, 0, 4)
                    .append(date, 5, 7);
        }
        return stringBuffer.toString();
    }
}

到此这篇关于Java中sharding-jdbc按年月分片的示例代码的文章就介绍到这了,更多相关Java sharding-jdbc按年月分片内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文