所有的分片規則配置的tableRule標簽中:
rule標簽中的columns標簽內填寫要分片的表字段,algorithm標簽內填寫分片所使用的自定義函數名,要與function函數中的name屬性保持一致
function函數中的property標簽內配置自定義參數。
1)枚舉法:sharding-by-intfile
<tableRule name="sharding-by-intfile">
<rule>
<columns>user_id</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>
<property name="type">0</property>
<property name="defaultNode">0</property>
</function>
mapFile 中是自定義的分片策略文件,需要自己編寫
type表示分片字段的類型,其中1表示字符串,0表示int類型
defaultNode配置是否使用默認節點,默認為0,表示不設置默認節點,這樣會導致遇到不識別的枚舉值時會報錯,如果設置的值大於零,該值就是默認節點,會把不識別的枚舉值分配到默認節點。
這種方法適用於取值固定的場合,例如性別和省份
2)固定分片:rule 1
<tableRule name="rule1">
<rule>
<columns>user_id</columns>
<algorithm>func1</algorithm>
</rule>
</tableRule>
<function name="func1" class="io.mycat.route.function.PartitionByLong">
<property name="partitionCount">2,1</property>
<property name="partitionLength">256,512</property>
</function>
partitionCount 表示分片個數列表,partitionLength 表示分片范圍列表,兩者可以都配單個值或多個值
因為分區長度默認為最大2^n=1024 ,即最大支持1024分區
所以兩個值的點積恆等於1024,也就是說2*256+1*512=1024或者2*512=1024(分為兩片)或者4*256=1024(分為四片)
3)范圍約定:auto-sharding-long
<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文件中編寫分片規則,根據指定的列的范圍進行分片.默認從0節點開始
例如:0-200000=0
200000-400000=1
0-200000范圍分配各節點0
200000-400000范圍分配各節點1
這種方法適用於總數可知的分片場景,但是擴展比較麻煩,短時間大量順序插入會造成單個節點壓力過大
4)求模法:mod-long
<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 -->
<property name="count">3</property>
</function>
根據配置中的count值進行分片,將數據分成配置的count份,然后將數據均勻的分布在各個節點上,適用於單節點查詢,但是查詢量偏高的跨庫查詢會增加耗時。
5)日期列分區法:sharding-by-date
<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>
<property name="sBeginDate">2019-09-20</property>
<property name="sPartionDay">5</property>
</function>
dateFormat為字段格式,sBeginDate為開始日期,sPartionDay為分區天數。
該方法的分區方法為:從開始日期,每隔5天分一個分區;
6)通配取模:sharding-by-pattern
<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">
<property name="patternValue">256</property>
<property name="defaultNode">2</property>
<property name="mapFile">partition-pattern.txt</property>
</function>
patternValue是求模基數,使用int型分片字段與基數取模,根據取模結果和配置文件partition-pattern.txt決定分區,如果文件中配置1-32=1,則取模結果在范圍1-32時分到1區。如果字段值不為int則分配到defaultNode配置的分區中。
7)ASCII碼取模:sharding-by-prefixpattern
<tableRule name="sharding-by-prefixpattern">
<rule>
<columns>user_id</columns>
<algorithm>sharding-by-prefixpattern</algorithm>
</rule>
</tableRule>
<function name="sharding-by-pattern" class="io.mycat.route.function.PartitionByPrefixPattern">
<property name="patternValue">256</property>
<property name="prefixLength">5</property>
<property name="mapFile">partition-pattern.txt</property>
</function>
patternValue 為求模基數,prefixLength ASCII 截取的位數
該方法截取字段值ASCII 碼的指定位數(prefixLength 值)與求模基數(patternValue值)取模,然后根據配置文件partition-pattern.txt的內容分區
8)編程指定:sharding-by-substring
<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>
該方法的指定字段必須為數字,size為截取的位數,partitionCount為分區個數,defaultPartition為默認節點。
給方法從第0個索引的數字截取字段的指定位數,截取到的數字就是分區節點編號,如果沒有傳值,分配到默認分區節點。例:01-55888分配到01分區
9)字符串拆分hash解析:sharding-by-stringhash
<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">
<property name=length>512</property> <!-- zero-based -->
<property name="count">2</property>
<property name="hashSlice">0:2</property>
</function>
函數中length代表字符串hash求模基數,count是分區數,hashSlice為預算位,
該方法根據子字符串中的int值進hash運算,然后得出分區;
例如:hashSlice為0:2時截取字段值得0-2位值進行hash求模
hashSlice為-4:0時截取字段值倒數第四位到第0位值進行hash求模
10)一致性hash:sharding-by-murmur
<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>