
详解 mycat分片.doc
18页MYcat 分片规则. 枚举法: 通过在配置文件中配置可能的枚举 id,自己配置分片,使用规则: user_idhash-intpartition-hash-int.txt00partition-hash-int.txt 配置:10000=010010=1DEFAULT_NODE=1上面 columns 标识将要分片的表字段,algorithm 分片函数, 其中分片函数配置中,mapFile 标识配置文件名称,type 默认值为 0,0 表示Integer,非零表示 String, 所有的节点配置都是从 0 开始,及 0 代表节点 1 /*** defaultNode 默认节点:小于 0 表示不设置默认节点,大于等于 0 表示设置默认节点* 默认节点的作用:枚举分片时,如果碰到不识别的枚举值,就让它路由到默认节点* 如果不配置默认节点(defaultNode 值小于 0 表示不配置默认节点),碰到* 不识别的枚举值就会报错,* like this:can't find datanode for sharding column:column_name val:ffffffff */ 2.固定分片 hash 算法 user_idfunc12,1256,512配置说明: 上面 columns 标识将要分片的表字段,algorithm 分片函数, partitionCount 分片个数列表,partitionLength 分片范 围列表分区长度:默认为最大 2^n=1024 ,即最大支持 1024 分区 约束 : count,length 两个数组的长度必 须是一致的。
1024 = sum((count[i]*length[i])). count 和 length 两个向量的点积恒等于 1024 用法例子: 本例的分区策略:希望将数据水平分成 3 份,前两份各占 25%,第三份占 50%故本例非均匀分区)// ||// ||||// | 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); 如果需要平均分配 设置: 2,1256,5123.范围约定 user_idrang-longautopartition-long.txt# range start-end ,data node index# K=1000,M=10000.0-500M=0500M-1000M=11000M-1500M=2或0-10000000=010000001-20000000=1配置说明: 上面 columns 标识将要分片的表字段, algorithm 分片函数, rang-long 函数中 mapFile 代表配置文件路径 所有的节点配置都是从 0 开始,及 0 代表节点 1,此配置非常简单,即预先制定可能的 id 范围到某个分片4.求模法 user_idmod-long3配置说明: 上面 columns 标识将要分片的表字段, algorithm 分片函数, 此种配置非常明确即根据 id 进行十进制求模预算,相比方式 1,此种在批量插入时需要切换数据源,id 不连续 5.日期列分区法 create_timesharding-by-dateyyyy-MM-dd2014-01-0110配置说明: 上面 columns 标识将要分片的表字段, algorithm 分片函数, 配置中配置了开始日期,分区天数,即默认从开始日期算起,分隔 10天一个分区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"));6.通配取模 user_idsharding-by-pattern2562partition-pattern.txtpartition-pattern.txt # id partition range start-end ,data node index###### first host configuration1-32=033-64=165-96=297-128=3######## second host configuration129-160=4161-192=5193-224=6225-256=70-0=7配置说明: 上面 columns 标识将要分片的表字段, algorithm 分片函数,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));7. ASCII码求模通配 user_idsharding-by-prefixpattern 2565partition-pattern.txtpartition-pattern.txt# range start-end ,data node index# ASCII# 48-57=0-9# 64銆�5-90=@銆丄-Z# 97-122=a-z###### first host configuration1-4=05-8=19-12=213-16=3###### second host configuration17-20=421-24=525-28=629-32=70-0=7配置说明: 上面 columns 标识将要分片的表字段, algorithm 分片函数,patternValue 即求模基数,prefixLength ASCII 截取的位数 mapFile 配置文件路径 配置文件中,1-32 即代表 id%256 后分布的范围,如果在 1-32 则在分区 1,其他类推 此种方式类似方式 6 只不过采取的是将列种获取前 prefixLength 位列所有 ASCII码的和进行求模 sum%patternValue ,获取的值,在通配范围内的 即 分片数, /*** ASCII 编码:* 48-57=0-9 阿拉伯数字* 64、65-90=@、A-Z* 97-122=a-z**/如 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));8.编程指定 user_idsharding-by-substring0 280配置说明: 上面 columns 标识将要分片的表字段, algorithm 分片函数 此方法为直接根据字符子串(必须是数字)计算分区号(由应用传递参数,显式指定分区号)。
例如 id=05-100000002 在此配置中代表根据 id 中从 startIndex=0,开始,截取 siz=2 位数字即 05,05 就是获取的分区,如果没传默认分配到 defaultPartition9.字符串 hash 解析 user_idsharding-by-stringhash512 20:2配置说明: 上面 columns 标识将要分片的表字段, algorithm 分片函数 函数中 length代表字符串 hash求模基数,count 分区数,hashSlice hash 预算位即根据子字符串 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 4rule = new PartitionByString();rule.setPartitionLength("512");rule.setPartitionCount("2");rule.init();//last 4 charactersrule.setHashSlice("-4:0");idVal = "aaaabbb0000";Assert.assertEquals(true, 0 == rule.calculate(idVal));idVal = "aaaabbb2359";Assert.assertEquals(true, 0 == rule.calculate(idVal));10,一致性。
