MyCat常用的分片规则:总共十个(基本够用)

 

 

MyCat常用的分片规则:总共十个(基本够用)

 

 

一、枚举法

<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>

 

partition-hash-int.txt 配置:

10000=0

10010=1

说明:

1)上面columns 标识将要分片的表字段,

2)algorithm 分片函数,

3)其中分片函数配置中,mapFile标识配置文件名称,

5)type默认值为0,0表示Integer,非零表示String,

6)所有的节点配置都是从0开始,及0代表节点1

/**

*  defaultNode 默认节点:小于0表示不设置默认节点,大于等于0表示设置默认节点,结点为指定的值

*

默认节点的作用:枚举分片时,如果碰到不识别的枚举值,就让它路由到默认节点
*                如果不配置默认节点(defaultNode值小于0表示不配置默认节点),碰到
*                不识别的枚举值就会报错,
*                like this:can't find datanode for sharding column:column_name val:ffffffff    
*/

 

 

 

 

 

二、固定分片hash算法

<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>

配置说明:

1)上面columns 标识将要分片的表字段,algorithm 分片函数,

2)partitionCount 分片个数列表,partitionLength 分片范围列表分区长度:默认为最大2^n=1024 ,即最大支持1024分区

约束 :count,length两个数组的长度必须是一致的。
1024 = sum((count[i]*length[i])). count和length两个向量的点积恒等于1024

用法例子:

@Test
public void testPartition() {
    // 本例的分区策略:希望将数据水平分成3份,前两份各占25%,第三份占50%。(故本例非均匀分区)
    // |<---------------------1024------------------------>|
    // |<----256--->|<----256--->|<----------512---------->|
    // | 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&#
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值