1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > shardingjdbc全局表_sharding-jdbc实现按年分库按月分表

shardingjdbc全局表_sharding-jdbc实现按年分库按月分表

时间:2021-02-27 04:08:50

相关推荐

shardingjdbc全局表_sharding-jdbc实现按年分库按月分表

最终数据库结果如下

image.png

例如有如下sql语句

select * from ips where flowtime = '1202';

我们规定flowtime是我们的分片键,通过值1202确定年份为,月份为12,所以需要定位到库sharding_中的表ips_12查询,

所以实际发出的查询语句是

select * from `sharding_`.ips_12 where flowtime = '1202';

具体实现

maven导出具体sharding需要的包

io.shardingsphere

sharding-jdbc-spring-namespace

3.0.0.M3

io.shardingsphere

sharding-jdbc-orchestration-spring-namespace

3.0.0.M3

配置数据库连接

由于我们需要一年一库,所以我们取~来建库,这步骤需要手工建立。查了资料好像shardingjdbc不支持自动建库,例如我们如果按照上面一年一库的规则,我们就需要自己手动建立对应的库。一年一库感觉还好,正常来说一个产品最多用已经很久了,所以手动预先建立好库没什么太大的工作量。所以我们先在数据库建立好表后,然后配置对应的数据源

applicationContext-database.xml

xmlns:xsi="/2001/XMLSchema-instance"

xmlns:context="/schema/context"

xsi:schemaLocation="/schema/beans

/schema/beans/spring-beans-4.1.xsd /schema/context /schema/context/spring-context.xsd /schema/aop /schema/aop/spring-aop.xsd">

这里解释下dataSource_default这个数据库用来干嘛的。由于在正常项目中并不是所有的数据都需要进行分库分表,例如用户表和用户记录表,一般用户记录表一般是千万级的,需要分库分表,但是用户表不需要。我们这里配置一个默认的数据源,对于不分库分表的数据就存放在这个默认的数据库中

applicationContext-sharding.xml

xmlns:xsi="/2001/XMLSchema-instance"

xmlns:context="/schema/context"

xmlns:tx="/schema/tx"

xmlns:sharding="http://shardingsphere.io/schema/shardingsphere/sharding"

xsi:schemaLocation="/schema/beans

/schema/beans/spring-beans.xsd

/schema/tx

/schema/tx/spring-tx.xsd

/schema/context

/schema/context/spring-context.xsd

http://shardingsphere.io/schema/shardingsphere/sharding

http://shardingsphere.io/schema/shardingsphere/sharding/sharding.xsd">

true

解释下如上配置的意思:

表示对应的库跟表的分片算法,shardingjdbc支持多种的分片算法,具体可以参考# sharding-jdbc—分片策略里的介绍。举个例子,我们需要进行分库分表,肯定需要定义一些规则,例如select * from ips where flowtime = '1212'查询语句,我是通过flowtime分片,且SQL语句是in或者=的查询,我就需要实现shardingjdbc提供的特定的分片算法接口,在里面通过计算出1212具体是哪年哪月,shardingjdbc才能帮我们定位到对应的数据库

......

data-source-names 主要列举所有的数据源。default-data-source-name为默认的数据源,通过这两项配置告诉sharding我的数据源列表和默认的数据源

这里我对三个表都配置了分片规则,其实是一样的,我们取其中一个来看。

actual-data-nodes="dataSource_${..}.flow_0${1..9},dataSource_${..}.flow_1${0..2}"

主要配置实际的库表,格式为 数据库.表 。支持使用inline表达式。上面的配置shardingjdbc将为解析成dataSource_.flow_01 ~ dataSource_.flow_12。具体参考行表达式

logic-table 表示实际表

database-strategy-ref 表示对应的库分片算法

table-strategy-ref 表示对应的表分片算法

分片算法

库分片算法 PreciseModuloDatabaseShardingAlgorithm

public class PreciseModuloDatabaseShardingAlgorithm implements PreciseShardingAlgorithm {

@Override

public String doSharding(Collection collection, PreciseShardingValue preciseShardingValue) {

//对于库的分片collection存放的是所有的库的列表,这里代表dataSource_~dataSource_

//配置的分片的sharding-column对应的值

String timeValue = preciseShardingValue.getValue();

//分库时配置的sharding-column

String time = preciseShardingValue.getColumnName();

//需要分库的逻辑表

String table = preciseShardingValue.getLogicTableName();

if(StringUtils.isBlank(timeValue)){

throw new UnsupportedOperationException("preciseShardingValue is null");

}

//按年路由

for (String each : collection) {

String value = StringUtils.substring(timeValue,0,4); //获取到年份

if(each.endsWith(value)){

// //这里返回回去的就是最终需要查询的库名

return each;

}

}

throw new UnsupportedOperationException();

}

表分片算法 PreciseModuloTableShardingAlgorithm

/**

* @author xuzhiyong

* @createDate -01-28-22:30

* 按表

*/

public class PreciseModuloTableShardingAlgorithm implements PreciseShardingAlgorithm {

@Override

public String doSharding(Collection collection, PreciseShardingValue preciseShardingValue) {

//对于库的分片collection存放的是所有的库的列表,这里代表flow_01~flow_12

//配置的分片的sharding-column对应的值

String timeValue = preciseShardingValue.getValue();

//分库时配置的sharding-column

String time = preciseShardingValue.getColumnName();

//需要分库的逻辑表

String table = preciseShardingValue.getLogicTableName();

if(StringUtils.isBlank(timeValue)){

throw new UnsupportedOperationException("preciseShardingValue is null");

}

//按月路由

for (String each : collection) {

String value = StringUtils.substring(timeValue,4,6); //获取到月份

if(each.endsWith(value)){

//这里返回回去的就是最终需要查询的表名

return each;

}

}

return null;

}

}

测试

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = {"classpath:applicationContext.xml", "classpath:applicationContext-database.xml", "classpath:applicationContext-sharding.xml"})

public class ShardingTest {

@Resource(name = "jdbcTemplate")

private JdbcTemplate jdbcTemplate;

@Test

public void testCreateTable() {

jdbcTemplate.update("CREATE TABLE IF NOT EXISTS ips (flowtime VARCHAR(50) NOT NULL, value INT NOT NULL)");

}

}

查看控制台,已经帮我们创建了对应的表

image.png

测试插入数据

@Test

public void testInsertOne(){

//测试一条记录多条插入

jdbcTemplate.update("INSERT IGNORE INTO flow(flowtime,value) VALUES ('0525',1),('0526',2),('0527',2)");

}

根据对应的规则插入到不同的表

image.png

@Test

public void query(){

List> list = jdbcTemplate.queryForList("select * from flow where flowtime = '0818'");

}

image.png

进行分库分表的思考

待续

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。