MyCat 学习笔记 第八篇.数据分片 之 求摸运算分片

时间:2023-03-09 19:41:45
MyCat 学习笔记 第八篇.数据分片 之 求摸运算分片

1 应用场景

Mycat 自带了多套数据分片的机制,其实根据数值取摸应该是最简单的一种。

优点:数据离散概率较为平均,可以有效的提高应用的数据吞吐。

缺点:比较明显,后期数据运维与迁移比较困难。好在Mycat有对应的解决方案,具体后期验证或可直接参考Mycat权威指南相应章节。

2 环境说明

参考  《MyCat 学习笔记》第六篇.数据分片 之 按月数据分片  http://www.cnblogs.com/kaye0110/p/5160826.html

3 参数配置

3.1 server.xml 配置

同上参考

3.2 schema.xml 配置

<!-- 配置 t_mod_long 数据表,分片规则为 mod-sharding-long ,数据结点有 4 个,注意在下面的 rule.xml 文件中其实只配置了3个, 因此dn7 不应该有数据才对 -->

<schema name="RANGEDB" checkSQLschema="false" sqlMaxLimit="100">
  <table name="t_range_date" dataNode="dn4,dn5,dn6,dn7,dn8,dn9,dn10,dn11" rule="sharding-by-date" />
  <table name="t_range_long" dataNode="dn4,dn5,dn6" rule="sharding-long" />
  <table name="t_mod_long" dataNode="dn4,dn5,dn6,dn7" rule="mod-sharding-long" />
</schema>

3.3 rule.xml 配置

<!-- 定义取摸运算规则 并设定 sharding_mod_id 为指定分片规则字段 -->

<tableRule name="mod-sharding-long">
  <rule>
    <columns>sharding_mod_id</columns>
    <algorithm>mod-long</algorithm>
  </rule>
</tableRule>

<!-- 取摸运算的具体实现类,数字节点设为3个 dn4\dn5\dn6  -->

<function name="mod-long" class="org.opencloudb.route.function.PartitionByMod">
  <!-- how many data nodes -->
  <property name="count">3</property>
</function>

4 数据验证

4.1 Mycat 建表

mysql> CREATE TABLE `t_mod_long` (
-> `id` INT NOT NULL,
-> `sharding_mod_id` VARCHAR(45) NULL,
-> `context` VARCHAR(45) NULL,
-> PRIMARY KEY (`id`));
Query OK, 0 rows affected (0.02 sec)

4.2 数据插入与查询

insert into t_mod_long (id,sharding_mod_id,context) values (1,1,'test 1');
insert into t_mod_long (id,sharding_mod_id,context) values (2,2,'test 2');
insert into t_mod_long (id,sharding_mod_id,context) values (3,3,'test 3');
insert into t_mod_long (id,sharding_mod_id,context) values (4,4,'test 4');
insert into t_mod_long (id,sharding_mod_id,context) values (5,5,'test 5');
insert into t_mod_long (id,sharding_mod_id,context) values (6,6,'test 6');
insert into t_mod_long (id,sharding_mod_id,context) values (7,7,'test 7');
insert into t_mod_long (id,sharding_mod_id,context) values (8,8,'test 8');
insert into t_mod_long (id,sharding_mod_id,context) values (9,9,'test 9');
insert into t_mod_long (id,sharding_mod_id,context) values (10,10,'test 10');
insert into t_mod_long (id,sharding_mod_id,context) values (11,11,'test 11');
insert into t_mod_long (id,sharding_mod_id,context) values (12,12,'test 12');
insert into t_mod_long (id,sharding_mod_id,context) values (13,13,'test 13');
insert into t_mod_long (id,sharding_mod_id,context) values (14,14,'test 14');

Query OK, 1 row affected (0.01 sec)

...

mysql> select * from t_mod_long;
+----+-----------------+----------+
| id | sharding_mod_id | context |
+----+-----------------+----------+
| 3 | 3 | test 3 |
| 6 | 6 | test 6 |
| 9 | 9 | test 9 |
| 12 | 12 | test 12 |
| 1 | 1 | test 1 |
| 4 | 4 | test 4 |
| 7 | 7 | test 7 |
| 10 | 10 | test 10 |
| 13 | 13 | test 13 |
| 2 | 2 | test 2 |
| 5 | 5 | test 5 |
| 8 | 8 | test 8 |
| 11 | 11 | test 11 |
| 14 | 14 | test 14 |
+----+-----------------+----------+
14 rows in set (0.02 sec)

其实从数据反馈的角度已经可以很明确的发现,当前记录是进入了分片规则。

4.3 物理库查询

一共往数据库中新增记录 14 条,其实取摸后前 4 条记录进入 dn4 , dn5和dn6分别插入记录5条,dn7没有数据,验证通过。

mysql> select * from range_db_4.t_mod_long;
+----+-----------------+----------+
| id | sharding_mod_id | context |
+----+-----------------+----------+
| 3 | 3 | test 3 |
| 6 | 6 | test 6 |
| 9 | 9 | test 9 |
| 12 | 12 | test 12 |
+----+-----------------+----------+
4 rows in set (0.00 sec)

mysql> select * from range_db_5.t_mod_long;
+----+-----------------+----------+
| id | sharding_mod_id | context |
+----+-----------------+----------+
| 1 | 1 | test 1 |
| 4 | 4 | test 4 |
| 7 | 7 | test 7 |
| 10 | 10 | test 10 |
| 13 | 13 | test 13 |
+----+-----------------+----------+
5 rows in set (0.00 sec)

mysql> select * from range_db_6.t_mod_long;
+----+-----------------+----------+
| id | sharding_mod_id | context |
+----+-----------------+----------+
| 2 | 2 | test 2 |
| 5 | 5 | test 5 |
| 8 | 8 | test 8 |
| 11 | 11 | test 11 |
| 14 | 14 | test 14 |
+----+-----------------+----------+
5 rows in set (0.00 sec)

mysql> select * from range_db_7.t_mod_long;
Empty set (0.00 sec)

本篇完。

个人原创学习资料,若要转载,请加入原始地址 http://www.cnblogs.com/kaye0110/p/5162957.html