sharding jdbc初探

2018年12月01日 随笔杂谈 评论 2 条
摘要:

sharding jdbc初探

官方网址:http://shardingsphere.io

与其他中间件对比

Sharding-JDBC的整体架构图


SQL支持详细列表

http://shardingsphere.io/document/legacy/2.x/cn/01-start/sql-supported/

JDBC未支持列表

http://shardingsphere.io/document/legacy/2.x/cn/01-start/limitations/

简单分表实例:
maven 引用

<dependency>
    <groupId>io.shardingjdbc</groupId>
    <artifactId>sharding-jdbc-core</artifactId>
    <version>2.0.0.M3</version>
</dependency>
<dependency>
    <groupId>io.shardingjdbc</groupId>
    <artifactId>sharding-jdbc-spring-namespace</artifactId>
<version>2.0.0.M3</version>

分库分表见:
http://shardingsphere.io/document/legacy/2.x/cn/02-guide/sharding/
实现了按某个字段所带的日期进行分表,供参考,后台的表半年一个,例如XXX_2018_1_6,XXX_2018_7_12

public final class PreciseModuloTableShardingAlgorithm implements PreciseShardingAlgorithm<String> {
    @Override
    public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<String> shardingValue) {
        int year, month;
        if (null != shardingValue && null != shardingValue.getValue()) {
            String value = shardingValue.getValue();
            year = Integer.valueOf(value.substring(0, 4));
            month = Integer.valueOf(value.substring(4, 6));
        } else {
            LocalDate today = LocalDate.now();
            year = today.getYear();
            month = today.getMonthValue();
        }
        for (String each : availableTargetNames) {
            //获取后缀
            String[] split1 = each.split("_");
            if (Integer.valueOf(split1[split1.length - 3]).equals(year)) {
                if (Integer.valueOf(split1[split1.length - 2]) <= month && month <= Integer.valueOf(split1[split1.length - 1])) {
                    return each;
                }
            }
        }
        throw new UnsupportedOperationException();
    }
}

源码分析参考:

2 条留言  访客:2 条  博主:0 条

  1. It抯 arduous to search out knowledgeable people on this matter, however you sound like you realize what you抮e talking about! Thanks

  2. I抦 impressed, I must say. Really not often do I encounter a blog that抯 each educative and entertaining, and let me tell you, you could have hit the nail on the head. Your idea is outstanding; the difficulty is one thing that not sufficient individuals are talking intelligently about. I am very happy that I stumbled across this in my seek for one thing relating to this.

给我留言

Copyright © 知乐思维 保留所有权利.   Ality主题 粤ICP备14062485

用户登录

分享到: