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