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

MyCat 学习笔记 第八篇.数据分片 之 求摸运算分片的更多相关文章

  1. MyCat 学习笔记 第十篇.数据分片 之 ER分片

    1 应用场景 这篇来说下mycat中自带的er关系分片,所谓er关系分片即可以理解为有关联关系表之间数据分片.类似于订单主表与订单详情表间的分片存储规则. 本文所说的er分片分为两种: a. 依据主键 ...

  2. MyCat 学习笔记 第七篇.数据分片 之 按数据范围分片

    1 应用场景 Mycat 其实自带了2个数据范围分片的方案,一个是纯数据范围的分片,比如 1至 10000 号的数据放到分片1 ,10001 至 20000号数据放到分片2里. 另一个是数据常量形式的 ...

  3. MyCat 学习笔记 第六篇.数据分片 之 按月数据分片

    1 应用场景 Mycat 有很多数据分库规则,接下来几篇就相关觉得常用的规则进行试用与总结. 一般来说,按自然月份来进行数据分片的规则比较适用于商城订单查询,类似最近1周.2周.3个月内的数据.或是报 ...

  4. MyCat 学习笔记 第十三篇.数据分片 之 通过HINT执行存储过程

    1 环境说明 VM 模拟3台MYSQL 5.6 服务器 VM1 192.168.31.187:3307 VM2 192.168.31.212:3307 VM3 192.168.31.150:  330 ...

  5. MyCat 学习笔记 第十一篇.数据分片 之 分片数据查询 ( select * from table_name limit 100000,100 )

    1 环境说明 VM 模拟3台MYSQL 5.6 服务器 VM1 192.168.31.187:3307 VM2 192.168.31.212:3307 VM3 192.168.31.150:  330 ...

  6. ES[7.6.x]学习笔记(八)数据的增删改

    在前面几节的内容中,我们学习索引.字段映射.分析器等,这些都是使用ES的基础,就像在数据库中创建表一样,基础工作做好以后,我们就要真正的使用它了,这一节我们要看看怎么向索引里写入数据.修改数据.删除数 ...

  7. Django学习笔记第八篇--实战练习四--为你的视图函数自定义装饰器

    零.背景: 对于登录后面所有视图函数,都需要验证登录信息,一般而言就是验证cookie里面的一些信息.所以你可以这么写函数: def personinfo(request): ": retu ...

  8. Vue.js学习笔记 第八篇 组件

    全局注册组件 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <ti ...

  9. MyCat 学习笔记 第九篇.数据分片 之 数值分布

    1 应用场景 Mycat 自带了多套数据分片的机制,其实根据数值分片也是比较简单,其实这个和数据取摸是类似的实现. 优.缺点同上一篇 2 环境说明 参考  <MyCat 学习笔记>第六篇. ...

随机推荐

  1. Java多线程学习笔记——信号量的使用

    Java中在控制多线程访问资源的时候使用了信号量可以控制多个线程同时访问一个资源. 有两个构造方法: public Semaphore(int permits) public Semaphore(in ...

  2. 计算几何 : 凸包学习笔记 --- Graham 扫描法

    凸包 (只针对二维平面内的凸包) 一.定义 简单的说,在一个二维平面内有n个点的集合S,现在要你选择一个点集C,C中的点构成一个凸多边形G,使得S集合的所有点要么在G内,要么在G上,并且保证这个凸多边 ...

  3. Web开发者选择的最佳HTML5/CSS3代码生成器

    原文地址:http://codecloud.net/css3-code-generators-for-web-programmers-6672.htmlHTML5 和CSS3是一入门就能用的最好的语言 ...

  4. 用javascript去掉字符串空格的办法

    今天遇到了以关于JavaScript 中怎么去掉 字符串中前后两段的空格 ,我只好向就得js中也后Trim() 函数,后来试试了不 行,就网上找了下解决方法,其中用到了正则表达式 ,整理了下: < ...

  5. 【Android】将Xamarin For VS升级为4.1.0.530版

    分类:C#.Android.VS2015(自带Update2).Win10 创建日期:2016-06-10 2016-08-03说明:该版本已过时,新版本详见本博客置顶的更新. 一.Xamarin f ...

  6. 遇上了artTemplate做的东西

    js现在最牛的地方是 有了Node.js后,前后端的界限几乎都消失了,围绕着它,出现了一整套生态体系. 在生态方面,比php好太多了.

  7. 用php做了下冒泡排序

    大学没好好读书,那会没怎么明白冒泡排序是这么回事 早上睡到九点多起来,就在房间看书.听歌,下午吃完饭做了下冒泡排序,现在把代码贡献如下: <?php /** * Created by PhpSt ...

  8. 「C语言」单链表/双向链表的建立/遍历/插入/删除

    最近临近期末的C语言课程设计比平时练习作业一下难了不止一个档次,第一次接触到了C语言的框架开发,了解了View(界面层).Service(业务逻辑层).Persistence(持久化层)的分离和耦合, ...

  9. Linux初学者指南

    1.为啥我们要学习Linux? 我们干嘛要学习Linux? Linux能给我们带来什么价值呢? Linux给我的感觉就是稳定,免费,性能好. 稳定,体现在哪里?我们使用PC机,安装的操作系统一般是wi ...

  10. 趣味问题:画图(c++实现)

    描述:在一个定义了直角坐标系的纸上,画一个(x1,y1)到(x2,y2)的矩形指将横坐标范围从x1到x2,纵坐标范围从y1到y2之间的区域涂上颜色.下图给出了一个画了两个矩形的例子.第一个矩形是(1, ...