Mycat探索之旅(5)----常用的分片规则
分片枚举
通过在配置文件中配置可能的枚举id,自己配置分片,本规则适用于特定的场景,比如有些业务需要按照省份或区县来做保存,
而全国省份区县固定的,这类业务使用本条规则,配置如下:
<tableRule name="sharding-by-intfile"> <rule> <!--标识将要分片的表字段--> <columns>user_id</columns> <!--分片函数--> <algorithm>hash-int</algorithm> </rule> </tableRule> <function name="hash-int" class="org.opencloudb.route.function.PartitionByFileMap"> <!--标识配置文件名称--> <property name="mapFile">partition-hash-int.txt</property> <!--type默认值为0,0表示Integer,非零表示String--> <property name="type">0</property> <!-- 默认节点:小于0表示不设置默认节点,大于等于0表示设置默认节点, 所有的节点配置都是从0开始,即0代表节点1 作用: 枚举分片时,如果碰到不识别的枚举值,就让它路由到默认节点 如果不配置默认节点(defaultNode值小于0表示不配置默认节点),碰到不识别的枚举值就会报错, can’t find datanode for sharding column:column_name val:ffffffff --> <property name="defaultNode">0</property> </function>
partition-hash-int.txt 配置:
10000=0 10010=1 DEFAULT_NODE=1
固定分片hash算法
本条规则类似于十进制的求模运算,区别在于是二进制的操作,是取id的二进制低10位,即id二进制&1111111111。
此算法的优点在于如果按照10进制取模运算,在连续插入1-10时候1-10会被分到1-10个分片,增大了插入的事务控制难度,而
此算法根据二进制则可能会分到连续的分片,减少插入事务事务控制难度。
<tableRule name="rule1"> <rule> <columns>user_id</columns> <algorithm>func1</algorithm> </rule> </tableRule> <function name="func1" class="org.opencloudb.route.function.PartitionByLong"> <!--分片个数列表--> <property name="partitionCount">2,1</property> <!--分片范围列表--> <property name="partitionLength">256,512</property> </function>
配置说明:
分区长度:默认为最大2^n=1024 ,即最大支持1024分区
约束 :
count,length两个数组的长度必须是一致的。
1024 = sum((count[i]*length[i])). count和length两个向量的点积恒等于1024
用法例子:
本例的分区策略:希望将数据水平分成3份,前两份各占25%,第三份占50%。(故本例非均匀分区)
// |<———————1024————————>|
// |<—-256—>|<—-256—>|<———-512———->|
// | partition0 | partition1 | partition2 |
// | 共2份,故count[0]=2 | 共1份,故count[1]=1 |
int[] count = new int[] { 2, 1 }; int[] length = new int[] { 256, 512 }; PartitionUtil pu = new PartitionUtil(count, length); // 下面代码演示分别以offerId字段或memberId字段根据上述分区策略拆分的分配结果 int DEFAULT_STR_HEAD_LEN = 8; // cobar默认会配置为此值 long offerId = 12345; String memberId = "qiushuo"; // 若根据offerId分配,partNo1将等于0,即按照上述分区策略,offerId为12345时将会被分配到partition0中 int partNo1 = pu.partition(offerId); // 若根据memberId分配,partNo2将等于2,即按照上述分区策略,memberId为qiushuo时将会被分到partition2中 int partNo2 = pu.partition(memberId, 0, DEFAULT_STR_HEAD_LEN);
如果需要平均分配设置:平均分为4分片,partitionCount*partitionLength=1024
<function name="func1" class="org.opencloudb.route.function.PartitionByLong"> <property name="partitionCount">4</property> <property name="partitionLength">256</property> </function>
范围约定
此分片适用于,提前规划好分片字段某个范围属于哪个分片,
start <= range <= end.
range start-end ,data node index
K=1000,M=10000.
<tableRule name="auto-sharding-long"> <rule> <columns>user_id</columns> <algorithm>rang-long</algorithm> </rule> </tableRule> <function name="rang-long" class="org.opencloudb.route.function.AutoPartitionByLong"> <!--配置文件路径--> <property name="mapFile">autopartition-long.txt</property> <!-- 超过范围后的默认节点。 所有的节点配置都是从0开始,及0代表节点1,即预先制定可能的id范围到某个分片 0-500M=0 500M-1000M=1 1000M-1500M=2 或 0-10000000=0 10000001-20000000=1 --> <property name="defaultNode">0</property> </function>
求模
此规则为对分片字段求摸运算。
<tableRule name="mod-long"> <rule> <columns>user_id</columns> <algorithm>mod-long</algorithm> </rule> </tableRule> <function name="mod-long" class="org.opencloudb.route.function.PartitionByMod"> <!-- how many data nodes --> <property name="count">3</property> </function>
配置说明:
此种配置非常明确即根据id进行十进制求模预算,相比固定分片hash,此种在批量插入时可能存在批量插入单事务插入多数据分
片,增大事务一致性难度。
按日期(天)分片
此规则为按天分片。
<tableRule name="sharding-by-date"> <rule> <columns>create_time</columns> <algorithm>sharding-by-date</algorithm> </rule> </tableRule> <function name="sharding-by-date" class="org.opencloudb.route.function.PartitionByDate"> <!--日期格式--> <property name="dateFormat">yyyy-MM-dd</property> <!--开始日期--> <property name="sBeginDate">2014-01-01</property> <!--分区天数,即默认从开始日期算起,分隔10天一个分区--> <property name="sPartionDay">10</property> </function>
配置说明:
Assert.assertEquals(true, 0 == partition.calculate(“2014-01-01”)); Assert.assertEquals(true, 0 == partition.calculate(“2014-01-10”)); Assert.assertEquals(true, 1 == partition.calculate(“2014-01-11”)); Assert.assertEquals(true, 12 == partition.calculate(“2014-05-01”));
取模范围约束
此种规则是取模运算与范围约束的结合,主要为了后续数据迁移做准备,即可以自主决定取模后数据的节点分布。
<tableRule name="sharding-by-pattern"> <rule> <columns>user_id</columns> <algorithm>sharding-by-pattern</algorithm> </rule> </tableRule> <function name="sharding-by-pattern" class="org.opencloudb.route.function.PartitionByPattern"> <property name="patternValue">256</property> <property name="defaultNode">2</property> <property name="mapFile">partition-pattern.txt</property> </function>
partition-pattern.txt
# id partition range start-end ,data node index ###### first host configuration 1-32=0 33-64=1 65-96=2 97-128=3 ######## second host configuration 129-160=4 161-192=5 193-224=6 225-256=7 0-0=7
配置说明:
patternValue 即求模基数,
defaoultNode 默认节点,如果配置了默认,则不会按照求模运算
mapFile 配置文件路径
配置文件中,1-32 即代表id%256后分布的范围,如果在1-32则在分区1,其他类推,如果id非数据,则会分配在defaoultNode
默认节点
String idVal = “0”; Assert.assertEquals(true, 7 == autoPartition.calculate(idVal)); idVal = “45a”; Assert.assertEquals(true, 2 == autoPartition.calculate(idVal));
ASCII码求模范围约束
此种规则类似于取模范围约束,此规则支持数据符号字母取模。
<tableRule name="sharding-by-prefixpattern"> <rule> <columns>user_id</columns> <algorithm>sharding-by-prefixpattern</algorithm> </rule> </tableRule> <function name="sharding-by-pattern" class="org.opencloudb.route.function.PartitionByPrefixPattern"> <property name="patternValue">256</property> <property name="prefixLength">5</property> <property name="mapFile">partition-pattern.txt</property> </function>
partition-pattern.txt
# range start-end ,data node index # ASCII # 8-57=0-9阿拉伯数字 # 64、65-90=@、A-Z # 97-122=a-z ###### first host configuration 1-4=0 5-8=1 9-12=2 13-16=3 ###### second host configuration 17-20=4 21-24=5 25-28=6 29-32=7 0-0=7
配置说明:
patternValue 即求模基数,
prefixLength ASCII 截取的位数
mapFile 配置文件路径
配置文件中,1-32 即代表id%256后分布的范围,如果在1-32则在分区1,其他类推
此种方式类似方式6只不过采取的是将列种获取前prefixLength位列所有ASCII码的和进行求模sum%patternValue ,获取的值,
在范围内的分片数,
String idVal=“gf89f9a”; Assert.assertEquals(true, 0==autoPartition.calculate(idVal)); idVal=“8df99a”; Assert.assertEquals(true, 4==autoPartition.calculate(idVal)); idVal=“8dhdf99a”; Assert.assertEquals(true, 3==autoPartition.calculate(idVal));
应用指定
此规则是在运行阶段有应用自主决定路由到那个分片。
<tableRule name="sharding-by-substring"> <rule> <columns>user_id</columns> <algorithm>sharding-by-substring</algorithm> </rule> </tableRule> <function name="sharding-by-substring" class="org.opencloudb.route.function.PartitionDirectBySubString"> <property name="startIndex">0</property> <!-- zero-based --> <property name="size">2</property> <property name="partitionCount">8</property> <property name="defaultPartition">0</property> </function>
配置说明:
此方法为直接根据字符子串(必须是数字)计算分区号(由应用传递参数,显式指定分区号)。
例如id=05-100000002
在此配置中代表根据id中从startIndex=0,开始,截取siz=2位数字即05,05就是获取的分区,如果没传默认分配到
defaultPartition
字符串hash解析
此规则是截取字符串中的int数值hash分片。
<tableRule name="sharding-by-stringhash"> <rule> <columns>user_id</columns> <algorithm>sharding-by-stringhash</algorithm> </rule> </tableRule> <function name="sharding-by-stringhash" class="org.opencloudb.route.function.PartitionByString"> <property name=length>512</property> <!-- zero-based --> <property name="count">2</property> <property name="hashSlice">0:2</property> </function>
配置说明:
函数中length代表字符串hash求模基数,count分区数,hashSlice hash预算位
即根据子字符串中int值 hash运算
hashSlice : 0 means str.length(), -1 means str.length()-1
/**
* “2” -> (0,2)
* “1:2” -> (1,2)
* “1:” -> (1,0)
* “-1:” -> (-1,0)
* “:-1” -> (0,-1)
* “:” -> (0,0)
*/
例子:
String idVal=null; rule.setPartitionLength("512"); rule.setPartitionCount("2"); rule.init(); rule.setHashSlice("0:2"); // idVal = "0"; // Assert.assertEquals(true, 0 == rule.calculate(idVal)); // idVal = "45a"; // Assert.assertEquals(true, 1 == rule.calculate(idVal)); //last 4 rule = new PartitionByString(); rule.setPartitionLength("512"); rule.setPartitionCount("2"); rule.init(); //last 4 characters rule.setHashSlice("-4:0"); idVal = "aaaabbb0000"; Assert.assertEquals(true, 0 == rule.calculate(idVal)); idVal = "aaaabbb2359"; Assert.assertEquals(true, 0 == rule.calculate(idVal));
一致性hash
一致性hash预算有效解决了分布式数据的扩容问题。
<tableRule name="sharding-by-murmur"> <rule> <columns>user_id</columns> <algorithm>murmur</algorithm> </rule> </tableRule> <function name="murmur" class="org.opencloudb.route.function.PartitionByMurmurHash"> <property name="seed">0</property><!-- 默认是0--> <property name="count">2</property><!-- 要分片的数据库节点数量,必须指定,否则没法分片--> <property name="virtualBucketTimes">160</property><!-- 一个实际的数据库节点被映射为这么多虚拟节点,默认 是160倍,也就是虚拟节点数是物理节点数的160倍--> <!-- <property name="weightMapFile">weightMapFile</property> 节点的权重,没有指定权重的节点默认是1。以properties文件的格式填写,以从0开始到count-1的 整数值也就是节点索引为key,以节点权重值为值。所有权重值必须是正整数,否则以1代替 --> <!-- <property name="bucketMapPath">/etc/mycat/bucketMapPath</property> 用于测试时观察各物理节点与虚拟节点的分布情况,如果指定了这个属性,会把虚拟节点的murmur hash值与物理节点的映射按行输出到这个文件,没有默认值,如果不指定,就不会输出任何东西 --> </function>
按单月小时拆分
此规则是单月内按照小时拆分,最小粒度是小时,可以一天最多24个分片,最少1个分片,一个月完后下月从头开始循环。
每个月月尾,需要手工清理数据。
<tableRule name="sharding-by-hour"> <rule> <columns>create_time</columns> <algorithm>sharding-by-hour</algorithm> </rule> </tableRule> <function name="sharding-by-hour" class="org.opencloudb.route.function.LatestMonthPartion"> <property name="splitOneDay">24</property> </function>
配置说明:
columns:拆分字段,字符串类型(yyyymmddHH)
splitOneDay : 一天切分的分片数
LatestMonthPartion partion = new LatestMonthPartion(); partion.setSplitOneDay(24); Integer val = partion.calculate("2015020100"); assertTrue(val == 0); val = partion.calculate("2015020216"); assertTrue(val == 40); val = partion.calculate("2015022823"); assertTrue(val == 27 * 24 + 23); Integer[] span = partion.calculateRange("2015020100", "2015022823"); assertTrue(span.length == 27 * 24 + 23 + 1); assertTrue(span[0] == 0 && span[span.length - 1] == 27 * 24 + 23); span = partion.calculateRange("2015020100", "2015020123"); assertTrue(span.length == 24); assertTrue(span[0] == 0 && span[span.length - 1] == 23);
自然月分片
按月份列分区,每个自然月一个分片,格式 between操作解析的范例。
<tableRule name="sharding-by-month"> <rule> <columns>create_time</columns> <algorithm>sharding-by-month</algorithm> </rule> </tableRule> <function name="sharding-by-month" class="org.opencloudb.route.function.PartitionByMonth"> <property name="dateFormat">yyyy-MM-dd</property> <property name="sBeginDate">2014-01-01</property> </function>
配置说明:
columns:分片字段,字符串类型
dateFormat : 日期字符串格式
sBeginDate : 开始日期
PartitionByMonth partition = new PartitionByMonth(); partition.setDateFormat("yyyy-MM-dd"); partition.setsBeginDate("2014-01-01"); partition.init(); Assert.assertEquals(true, 0 == partition.calculate("2014-01-01")); Assert.assertEquals(true, 0 == partition.calculate("2014-01-10")); Assert.assertEquals(true, 0 == partition.calculate("2014-01-31")); Assert.assertEquals(true, 1 == partition.calculate("2014-02-01")); Assert.assertEquals(true, 1 == partition.calculate("2014-02-28")); Assert.assertEquals(true, 2 == partition.calculate("2014-03-1")); Assert.assertEquals(true, 11 == partition.calculate("2014-12-31")); Assert.assertEquals(true, 12 == partition.calculate("2015-01-31")); Assert.assertEquals(true, 23 == partition.calculate("2015-12-31"));
Mycat探索之旅(5)----常用的分片规则的更多相关文章
- mycat常用的分片规则
一.枚举法<tableRule name="sharding-by-intfile"> <rule> <columns>user ...
- Mycat探索之旅(4)----Mycat的自增长主键和返回生成主键ID的实现
说明:MyCAT自增长主键和返回生成主键ID的实现 1) mysql本身对非自增长主键,使用last_insert_id()是不会返回结果的,只会返回0:这里做一个简单的测试 创建测试表 ------ ...
- Mycat探索之旅(3)----Mycat的全局序列号
一.本地文件方式 原理:此方式MyCAT将sequence配置到文件中,当使用到sequence中的配置后,MyCAT会更下classpath中的sequence_conf.properties文件中 ...
- MyCat分片规则--笔记(二)
概述 myCat实现分库分表的策略,对数据量的处理带来很大的便利,这里主要整理下MyCat的使用以及常用路由算法,针对MyCat里面的事务.集群后续再做整理:另外内容整理,不免会参考技术大牛的博客,内 ...
- Mycat水平拆分之十种分片规则
水平切分分片实现 配置schema.xml 在同一个mysql数据库中,创建了三个数据库 testdb1,testdb2,testdb3.并在每个库中都创建了user表 <?xml ...
- 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 ...
- Mysql系列六:(Mycat分片路由原理、Mycat常用分片规则及对应源码介绍)
一.Mycat分片路由原理 我们先来看下面的一个SQL在Mycat里面是如何执行的: , ); 有3个分片dn1,dn2,dn3, id=5000001这条数据在dn2上,id=10000001这条数 ...
- mycat是什么?你是怎么理解的?你们公司分库分表的分片规则是什么?搭建mycat环境常用的配置文件有哪些?
1.mycat是什么? 国内最活跃的.性能最好的开源数据库分库分表中间件 一个彻底开源的,面向企业应用开发的大数据库集群 支持事务.ACID.可以替代MySQL的加强版数据库 一个可以视为MySQL集 ...
- MyCAT常用分片规则之分片枚举
MyCAT支持多种分片规则,下面测试的这种是分片枚举.适用场景,列值的个数是固定的,譬如省份,月份等. 在这里,需定义三个值,规则均是在rule.xml中定义. 1. tableRule 2. fun ...
随机推荐
- KISSY - A Powerful JavaScript Framework
KISSY 是一款跨终端.模块化.高性能.使用简单的 JavaScript 框架.除了完备的工具集合如 DOM.Event.Ajax.Anim 等,它还提供了经典的面向对象.动态加载.性能优化解决方案 ...
- Mysql优化之——启用查询缓存
启用MySQL查询缓存可以极大地减低数据库服务器的CPU使用率,实际使用情况是:开启前CPU使用率120%左右,开启后降到了10%. 查看查询缓存情况: mysql> show variable ...
- [bzoj5017][Snoi2017]炸弹 tarjan缩点+线段树优化建图+拓扑
5017: [Snoi2017]炸弹 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 608 Solved: 190[Submit][Status][ ...
- python 全局解释锁GIL
Python的全局解释器锁GIL用于保护python解释器,使得任意时刻,只有一个线程在解释器中运行.从而保证线程安全 在多线程环境中,Python 虚拟机按以下方式执行: 1. 设置GIL2. 切换 ...
- AMQ学习笔记 - 08. Spring-JmsTemplate之发送
概述 JmsTemplate提供了3组*3,共计9个发送用的方法. 发送的方法有3组: 基本的发送 转换并发送 转换.后处理再发送 必需的资源 必需的资源有: javax.jms.Connecti ...
- floyed算法的一些感想
for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=k;j++) if(f[i][k]+f[k][j]<f[i ...
- 训练指南 UVA - 11354(最小生成树 + 倍增LCA)
layout: post title: 训练指南 UVA - 11354(最小生成树 + 倍增LCA) author: "luowentaoaa" catalog: true ma ...
- [BZOJ5462][APIO2018]新家(线段树+堆)
其实这个题第一反应一定是线段树分治,但是这样反而更难考虑了(实际上是可做的但很难想到),可见即使看上去最贴切的算法也未必能有效果. 考虑这个DS题,没有什么模型的转化,可能用到的无非就是线段树.平衡树 ...
- CodeForces - 981E Addition on Segments
考虑每个点i在什么情况下会成为最大值. 当选的区间子集是 包含i的区间的一个子集的时候,i肯定会是最大值. 所以我们就可以用这种方法得到所有点的可能的最大值是多少... 也就是说,最后的局面可以仅由一 ...
- Problem J: 求方程的解——C语言初学者百题大战之十五
#include<stdio.h> #include<math.h> int main() { float a,b,c,x1,x2,delta; scanf("%f ...