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. 译:重置/还原Windows IIs设置为默认设置

    译文出处:http://www.codeproject.com/Tips/870858/Reset-Restore-IIS-Settings-to-its-Default-in-Windo 简介: I ...

  2. URL与图像格式

    绝对/相对URL “绝对URL”是指资源的完整的地址,通常以“http://”打头: “相对URL”是指Internet上资源相对于当前页面的地址,它包含从当前页面指向目标资源位置的路径,不以“htt ...

  3. Android中的消息通知(NotificationManager和Notification)

    下面来谈谈notification,这个notification一般用在电话,短 信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这 ...

  4. asp.net.mvc 的单文件上传和多文件上传的简单例子

    首先打开vs2012,创建空的mvc4项目,名称为MVCStudy,选择基本模板

  5. Wijmo金融图表系列之平均K线图&砖形图

    2015年7月16日将会发布有史以来最令人兴奋的控件-Wijmo 金融图表,它的一体化设计为单个自定义集合提供了所有主要的金融图表,这是市场上的其他控件都不具备的独一无二的好处.它像Wijmo其他任意 ...

  6. OAuth2.0 基础概述

    web:http://oauth.net/2/ rfc:http://tools.ietf.org/html/rfc6749 doc:http://oauth.net/documentation/ c ...

  7. ACdream 1214---矩阵连乘

    ACdream 1214---矩阵连乘 Problem Description You might have noticed that there is the new fashion among r ...

  8. [moka同学笔记]yii2场景的使用(摘录)

    前半部分为自己使用的过程,下边为转载的,具体地址见:http://blog.sina.com.cn/s/blog_88a65c1b0101j717.html 1.在model中 public func ...

  9. javascript 之拼接html字符串

    // var one = {"id":1, "leasetime":2, "ney":0,"myhuifangshi": ...

  10. 【Asphyre引擎】关于AsphyreTypes中OverlapRect的改动,都是泪啊!!!

    OverlapRect改动:两个参数对调了.想问问LP,这样真的好吗? Sphinx304版本的代码: function OverlapRect(const Rect1, Rect2: TRect): ...