mycat的10种分片规则

所有的分片规则配置的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>

猜你喜欢

转载自www.cnblogs.com/KelvinDaniels/p/11572775.html