1 应用场景

Mycat 自带了多套数据分片的机制,其实根据数值分片也是比较简单,其实这个和数据取摸是类似的实现。

优、缺点同上一篇

2 环境说明

参考  《MyCat 学习笔记》第六篇.数据分片 之 按月数据分片  http://www.cnblogs.com/kaye0110/p/5160826.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 学习笔记 第九篇.数据分片 之 数值分布的更多相关文章

  1. Django学习笔记第九篇--实战练习五--关于数据的改、删操作、数据库字段属性的设置和类视图

    一.首先上代码.关于类视图: class register(View): #template_name = "templates/register.html" def get(se ...

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

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

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

    1 应用场景 Mycat 自带了多套数据分片的机制,其实根据数值取摸应该是最简单的一种. 优点:数据离散概率较为平均,可以有效的提高应用的数据吞吐. 缺点:比较明显,后期数据运维与迁移比较困难.好在M ...

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

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

  5. PHP学习笔记 - 进阶篇(11)

    PHP学习笔记 - 进阶篇(11) 数据库操作 PHP支持哪些数据库 PHP通过安装相应的扩展来实现数据库操作,现代应用程序的设计离不开数据库的应用,当前主流的数据库有MsSQL,MySQL,Syba ...

  6. PHP学习笔记 - 进阶篇(7)

    PHP学习笔记 - 进阶篇(7) 文件操作 读取文件内容 PHP具有丰富的文件操作函数,最简单的读取文件的函数为file_get_contents,可以将整个文件全部读取到一个字符串中. $conte ...

  7. PHP学习笔记 - 进阶篇(6)

    PHP学习笔记- 进阶篇(6) 会话控制(session与cookie) 当前的Cookie为: cookie简介 Cookie是存储在客户端浏览器中的数据,我们通过Cookie来跟踪与存储用户数据. ...

  8. PHP学习笔记 - 进阶篇(5)

    PHP学习笔记 - 进阶篇(5) 正则表达式 什么叫正则表达式 正则表达式是对字符串进行操作的一种逻辑公式,就是用一些特定的字符组合成一个规则字符串,称之为正则匹配模式. $p = '/apple/' ...

  9. PHP学习笔记 - 进阶篇(3)

    PHP学习笔记 - 进阶篇(3) 类与面向对象 1.类和对象 类是面向对象程序设计的基本概念,通俗的理解类就是对现实中某一个种类的东西的抽象, 比如汽车可以抽象为一个类,汽车拥有名字.轮胎.速度.重量 ...

随机推荐

  1. mac ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

    好久不用mysql,今天突然想用的时候, mysql -uroot -p 直接报了下面的错误 ERROR 2002 (HY000): Can't connect to local MySQL serv ...

  2. 启动Mysql时发生的一个关于PID文件错误问题

      今天启动mysql时出现了如下错误: [root@host1 /]# service mysql start Starting MySQL.. ERROR! The server quit wit ...

  3. IEnumerable接口

    IEnumerable接口顾名思义就是 可枚举的,可列举的. 接口也很简单,返回一个 枚举器对象 IEnumerator . [ComVisible(true), Guid("496B0AB ...

  4. Requested registry access is not allowed(不允许所请求的注册表访问权)

    尝试创建自定义事件日志时,将会收到“Requested registry access is not allowed(不允许所请求的注册表访问权)”错误消息 EventLog.CreateEventS ...

  5. 面向对象的JavaScript(3):私有成员和公开成员

    在小项目中对于JavaScript使用,只要写几个function就行了.但在大型项目中,尤其是在开发追求 良好的用户体验的网站中,如SNS,就会 用到大量的JavaScrpt,有时JavaScrip ...

  6. wpf 查找页面的所有TextBox

    private void EnumVisual(Visual myVisual) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount( ...

  7. #define lowbit(x) ((x)&(-x))原理详解

    #define lowbit(x) ((x)&(-x)) 也可以写成如下形式: int Lowbit(x) { return x&(-x); } 例如: 1> x = 1: 十进 ...

  8. ExecutorService常用方法和newFixedThreadPool创建固定大小的线程池

    1.ExecutorService: 是一个接口,继承了Executor: public interface ExecutorService extends Executor { } 2.Execut ...

  9. Spring框架之AOP

    SpringAop: 1.加入 jar 包 com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver ...

  10. jquery.cookie.js 用法

    jquery.cookie.js 用法   一个轻量级的cookie 插件,可以读取.写入.删除 cookie. jquery.cookie.js 的配置 首先包含jQuery的库文件,在后面包含 j ...