MyCat 学习笔记 第九篇.数据分片 之 数值分布
2016-01-28 08:44
197 查看
1 应用场景
Mycat 自带了多套数据分片的机制,其实根据数值分片也是比较简单,其实这个和数据取摸是类似的实现。
优、缺点同上一篇
2 环境说明
参考 《MyCat 学习笔记》第六篇.数据分片 之 按月数据分片 /article/5879428.html
3 参数配置
3.1 server.xml 配置
同上参考
3.2 schema.xml 配置
<!-- 配置 t_sharding_long 数据表,分片规则为 sharding-by-long ,数据结点有 4 个 -->
<schema name="RANGEDB" checkSQLschema="false" sqlMaxLimit="100">
<table name="t_sharding_long" dataNode="dn4,dn5,dn6,dn7" rule="sharding-by-long" />
</schema>
3.3 rule.xml 配置
<tableRule name="sharding-by-long">
<rule>
<columns>sharding_long</columns>
<algorithm>func2</algorithm>
</rule>
</tableRule>
<!-- partitionCount 与 partitionLength 相乘的值需要等于 1024 -->
<function name="func2" class="org.opencloudb.route.function.PartitionByLong">
<property name="partitionCount">4</property>
<property name="partitionLength">256</property>
</function>
4 数据验证
4.1 Mycat 建表
mysql> CREATE TABLE t_sharding_long (
-> `id` INT NOT NULL,
-> `sharding_long` VARCHAR(45) NULL,
-> `context` VARCHAR(45) NULL,
-> PRIMARY KEY (`id`));
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 1
Current database: RANGEDB
4.2 数据插入与查询
insert into t_sharding_long (id,sharding_long,context) values (1,255,'test 255');
insert into t_sharding_long (id,sharding_long,context) values (2,256,'test 256');
insert into t_sharding_long (id,sharding_long,context) values (3,257,'test 257');
insert into t_sharding_long (id,sharding_long,context) values (4,511,'test 511');
insert into t_sharding_long (id,sharding_long,context) values (5,512,'test 512');
insert into t_sharding_long (id,sharding_long,context) values (6,513,'test 513');
insert into t_sharding_long (id,sharding_long,context) values (7,767,'test 767');
insert into t_sharding_long (id,sharding_long,context) values (8,768,'test 768');
insert into t_sharding_long (id,sharding_long,context) values (9,769,'test 769');
insert into t_sharding_long (id,sharding_long,context) values (10,1023,'test 1023');
insert into t_sharding_long (id,sharding_long,context) values (11,1024,'test 1024');
insert into t_sharding_long (id,sharding_long,context) values (12,1025,'test 1025');
Query OK, 1 row affected (0.01 sec)
...
mysql> select * from t_sharding_long;
+----+---------------+------------+
| id | sharding_long | context |
+----+---------------+------------+
| 2 | 256 | test 256 |
| 3 | 257 | test 257 |
| 4 | 511 | test 511 |
| 1 | 255 | test 255 |
| 11 | 1024 | test 1024 |
| 12 | 1025 | test 1025 |
| 5 | 512 | test 512 |
| 6 | 513 | test 513 |
| 7 | 767 | test 767 |
| 8 | 768 | test 768 |
| 9 | 769 | test 769 |
| 10 | 1023 | test 1023 |
+----+---------------+------------+
12 rows in set (0.06 sec)
其实从数据反馈的角度已经可以很明确的发现,当前记录是进入了分片规则。
4.3 物理库查询
一共往数据库中新增记录 10 条,sharding_long字段除256后进行不同的分片,验证通过。
mysql> select * from range_db_4.t_sharding_long;
+----+---------------+------------+
| id | sharding_long | context |
+----+---------------+------------+
| 1 | 255 | test 255 |
| 11 | 1024 | test 1024 |
| 12 | 1025 | test 1025 |
+----+---------------+------------+
3 rows in set (0.00 sec)
mysql> select * from range_db_5.t_sharding_long;
+----+---------------+-----------+
| id | sharding_long | context |
+----+---------------+-----------+
| 2 | 256 | test 256 |
| 3 | 257 | test 257 |
| 4 | 511 | test 511 |
+----+---------------+-----------+
3 rows in set (0.00 sec)
mysql> select * from range_db_6.t_sharding_long;
+----+---------------+-----------+
| id | sharding_long | context |
+----+---------------+-----------+
| 5 | 512 | test 512 |
| 6 | 513 | test 513 |
| 7 | 767 | test 767 |
+----+---------------+-----------+
3 rows in set (0.00 sec)
mysql> select * from range_db_7.t_sharding_long;
+----+---------------+------------+
| id | sharding_long | context |
+----+---------------+------------+
| 8 | 768 | test 768 |
| 9 | 769 | test 769 |
| 10 | 1023 | test 1023 |
+----+---------------+------------+
3 rows in set (0.00 sec)
本篇完。
Mycat 自带了多套数据分片的机制,其实根据数值分片也是比较简单,其实这个和数据取摸是类似的实现。
优、缺点同上一篇
2 环境说明
参考 《MyCat 学习笔记》第六篇.数据分片 之 按月数据分片 /article/5879428.html
3 参数配置
3.1 server.xml 配置
同上参考
3.2 schema.xml 配置
<!-- 配置 t_sharding_long 数据表,分片规则为 sharding-by-long ,数据结点有 4 个 -->
<schema name="RANGEDB" checkSQLschema="false" sqlMaxLimit="100">
<table name="t_sharding_long" dataNode="dn4,dn5,dn6,dn7" rule="sharding-by-long" />
</schema>
3.3 rule.xml 配置
<tableRule name="sharding-by-long">
<rule>
<columns>sharding_long</columns>
<algorithm>func2</algorithm>
</rule>
</tableRule>
<!-- partitionCount 与 partitionLength 相乘的值需要等于 1024 -->
<function name="func2" class="org.opencloudb.route.function.PartitionByLong">
<property name="partitionCount">4</property>
<property name="partitionLength">256</property>
</function>
4 数据验证
4.1 Mycat 建表
mysql> CREATE TABLE t_sharding_long (
-> `id` INT NOT NULL,
-> `sharding_long` VARCHAR(45) NULL,
-> `context` VARCHAR(45) NULL,
-> PRIMARY KEY (`id`));
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 1
Current database: RANGEDB
4.2 数据插入与查询
insert into t_sharding_long (id,sharding_long,context) values (1,255,'test 255');
insert into t_sharding_long (id,sharding_long,context) values (2,256,'test 256');
insert into t_sharding_long (id,sharding_long,context) values (3,257,'test 257');
insert into t_sharding_long (id,sharding_long,context) values (4,511,'test 511');
insert into t_sharding_long (id,sharding_long,context) values (5,512,'test 512');
insert into t_sharding_long (id,sharding_long,context) values (6,513,'test 513');
insert into t_sharding_long (id,sharding_long,context) values (7,767,'test 767');
insert into t_sharding_long (id,sharding_long,context) values (8,768,'test 768');
insert into t_sharding_long (id,sharding_long,context) values (9,769,'test 769');
insert into t_sharding_long (id,sharding_long,context) values (10,1023,'test 1023');
insert into t_sharding_long (id,sharding_long,context) values (11,1024,'test 1024');
insert into t_sharding_long (id,sharding_long,context) values (12,1025,'test 1025');
Query OK, 1 row affected (0.01 sec)
...
mysql> select * from t_sharding_long;
+----+---------------+------------+
| id | sharding_long | context |
+----+---------------+------------+
| 2 | 256 | test 256 |
| 3 | 257 | test 257 |
| 4 | 511 | test 511 |
| 1 | 255 | test 255 |
| 11 | 1024 | test 1024 |
| 12 | 1025 | test 1025 |
| 5 | 512 | test 512 |
| 6 | 513 | test 513 |
| 7 | 767 | test 767 |
| 8 | 768 | test 768 |
| 9 | 769 | test 769 |
| 10 | 1023 | test 1023 |
+----+---------------+------------+
12 rows in set (0.06 sec)
其实从数据反馈的角度已经可以很明确的发现,当前记录是进入了分片规则。
4.3 物理库查询
一共往数据库中新增记录 10 条,sharding_long字段除256后进行不同的分片,验证通过。
mysql> select * from range_db_4.t_sharding_long;
+----+---------------+------------+
| id | sharding_long | context |
+----+---------------+------------+
| 1 | 255 | test 255 |
| 11 | 1024 | test 1024 |
| 12 | 1025 | test 1025 |
+----+---------------+------------+
3 rows in set (0.00 sec)
mysql> select * from range_db_5.t_sharding_long;
+----+---------------+-----------+
| id | sharding_long | context |
+----+---------------+-----------+
| 2 | 256 | test 256 |
| 3 | 257 | test 257 |
| 4 | 511 | test 511 |
+----+---------------+-----------+
3 rows in set (0.00 sec)
mysql> select * from range_db_6.t_sharding_long;
+----+---------------+-----------+
| id | sharding_long | context |
+----+---------------+-----------+
| 5 | 512 | test 512 |
| 6 | 513 | test 513 |
| 7 | 767 | test 767 |
+----+---------------+-----------+
3 rows in set (0.00 sec)
mysql> select * from range_db_7.t_sharding_long;
+----+---------------+------------+
| id | sharding_long | context |
+----+---------------+------------+
| 8 | 768 | test 768 |
| 9 | 769 | test 769 |
| 10 | 1023 | test 1023 |
+----+---------------+------------+
3 rows in set (0.00 sec)
本篇完。
相关文章推荐
- CLRS第十三章思考题
- H5小内容(三)
- 被解放的姜戈03 所谓伊人
- qt 静态成员函数
- win10 build 11102突变Build 14251到底怎么回事?
- JS实现动态生成表格并提交表格数据向后端
- Linux 基金会引发巨大争议,它到底是企业的还是草根的?
- Spring MVC +Mybatis + Maven 配置之Transaction配置
- 将ImageView中的图片保存到本地相冊
- C++ 读取INI文件
- ElasticSearch 2 (1) - Getting Start
- ffmpeg写mp4时不断打印:Error parsing ADTS frame header!
- HDOJ 1094 A+B for Input-Output Practice (VI)
- nyoj 523 亡命逃窜 【BFS】
- ubuntu安装和配置SVN
- 代码开发管理: 持续集成
- ERROR: Packet for query is too large (2034> 1024)
- 20. Valid Parentheses
- 数学之路(机器学习实践指南)-文本挖掘与NLP(2)
- IAR中ICF文件分析与应用