rule.xml 里面就定义了咱们对表进行拆分所涉及到的规则定义。咱们能够灵活的对表使用不一样的分片算法。这个文件里面主要有 tableRule 和 function 这两个标签。在具体使用过程当中能够按照需求添加 tableRule 和 function。node
<tableRule name="rule1"> #name为规则的惟一名称,用于标识不一样的表规则 <rule> <columns>id</columns> #columns 为表示对表的哪一个字段进行拆分 #algorithm 使用function标签中的name属性。链接表规则和具体路由算法。固然,多个表规则能够链接到同一个路由算法上。 table 标签内使用。让逻辑表使用这个规则进行分片 <algorithm>func1</algorithm> </rule> </tableRule>
#name 指定算法的名字,供algorithm引用 #class 制定路由算法具体的类名字。 <function name="hash-int" class="org.opencloudb.route.function.PartitionByFileMap"> #property 为具体算法须要用到的一些属性。 <property name="mapFile">partition-hash-int.txt</property> </function>
<tableRule name="sharding-by-intfile"> <rule> <columns>name</columns> <algorithm>hash-int</algorithm> </rule> </tableRule> <function name="hash-int" class="io.mycat.route.function.PartitionByFileMap"> <property name="mapFile">partition-hash-int.txt</property> #type为type默认值为0,0表示mapFile中的枚举类型Integer,非零表示mapFile中的枚举类型String <property name="type">1</property> #默认节点:小于0表示不设置默认节点,大于等于0表示设置默认节点,结点为指定的值(全部的节点配置都是从0开始,及0表明节点1),默认节点的做用:枚举分片时,若是碰到不识别的枚举值,就让它路由到默认节点,若是不配置默认节点(defaultNode值小于0表示不配置默认节点),碰到不识别的枚举值就会报错 <property name="defaultNode">0</property> </function>
partition-hash-int.txt 配置:算法
张飞=0 刘备=1 关羽=2
<tableRule name="mod-long"> <rule> <columns>user_id</columns> <algorithm>mod-long</algorithm> </rule> </tableRule> <function name="mod-long" class="io.mycat.route.function.PartitionByMod"> <!-- how many data nodes --> #此种配置很是明确即根据id与count(你的结点数)进行求模预算,相比方式1,此种在批量插入时须要切换数据源,id不连续 <property name="count">3</property> </function>
<tableRule name="sharding-by-date"> <rule> <columns>create_time</columns> <algorithm>sharding-by-date</algorithm> </rule> </tableRule> <function name="sharding-by-date" class="io.mycat.route.function..PartitionByDate"> <property name="dateFormat">yyyy-MM-dd</property> #配置中配置了开始日期,分区天数,即默认从开始日期算起,分隔10天一个分区 <property name="sBeginDate">2014-01-01</property> <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="auto-sharding-long"> <rule> <columns>user_id</columns> <algorithm>rang-long</algorithm> </rule> </tableRule> <function name="rang-long" class="io.mycat.route.function.AutoPartitionByLong"> <property name="mapFile">autopartition-long.txt</property> </function>
autopartition-long.txt编程
# range start-end ,data node index # K=1000,M=10000. 0-500M=0 500M-1000M=1 1000M-1500M=2 或 0-10000000=0 10000001-20000000=1
<tableRule name="sharding-by-pattern"> <rule> <columns>user_id</columns> <algorithm>sharding-by-pattern</algorithm> </rule> </tableRule> <function name="sharding-by-pattern" class="io.mycat.route.function.PartitionByPattern"> #patternValue 即求模基数 <property name="patternValue">256</property> #defaoultNode 默认节点,若是不配置默认节点,则默认是0即第一个节点 <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 即表明id%256后分布的范围,若是在1-32则在分区1,其余类推,若是id非数字数据,则会分配在defaoultNode 默认节点 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
<tableRule name="sharding-by-substring"> <rule> <columns>user_id</columns> <algorithm>sharding-by-substring</algorithm> </rule> </tableRule> <function name="sharding-by-substring" class="io.mycat.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测试
<tableRule name="sharding-by-stringhash"> <rule> <columns>user_id</columns> <algorithm>sharding-by-stringhash</algorithm> </rule> </tableRule> <function name="sharding-by-substring" class="io.mycat.route.function.PartitionByString"> #length表明字符串hash求模基数,其中length*count=1024 <property name=length>512</property> <!-- zero-based --> #count分区数,其中length*count=1024 <property name="count">2</property> #hashSlice hash预算位,即根据子字符串中int值 hash运算 <property name="hashSlice">0:2</property> </function>
0 表明 str.length(), -1 表明 str.length()-1,大于0只表明数字自身 能够理解为substring(start,end),start为0则只表示0 例1:值“45abc”,hash预算位0:2 ,取其中45进行计算 例2:值“aaaabbb2345”,hash预算位-4:0 ,取其中2345进行计算 /** * “2” -> (0,2) * “1:2” -> (1,2) * “1:” -> (1,0) * “-1:” -> (-1,0) * “:-1” -> (0,-1)125 * “:” -> (0,0) */
<tableRule name="sharding-by-murmur"> <rule> <columns>user_id</columns> <algorithm>murmur</algorithm> </rule> </tableRule> <function name="murmur" class="io.mycat.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>
一致性hash预算有效解决了分布式数据的扩容问题,前1-9中id规则都多少存在数据扩容难题,而10规则解决了数据扩容难点。spa