Mycat分片规则

目录

分片枚举

固定分片hash算法

范围约定

取模

按日期(天)分片

取模范围约束

截取数字做hash求模范围约束 

应用指定

截取数字hash解析 

一致性hash

按单月小时拆分

范围取模分片

日期范围hash分片

冷热数据分片

自然月分片


分片枚举

通过在配置文件中配置可能的枚举id,自己配置分片,本规则适用于特定的场景,比如有些业务需要按照省 份或区县来做保存,而全国省份区县固定的,这类业务使用本条规则,配置如下:

<tableRule name="sharding-by-intfile">
	<rule>
    	<columns>sharding_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 

配置说明:

columns :标识将要分片的表字段

algorithm :分片函数

mapFile:切分配置文件名称

type:默认为0,0表示Integer,非零表示String

defaultNode:默认节点。节点配置从0开始,即0代表节点1

                       分片时如果没有识别到枚举值,就会路由到默认节点;如果不配置默认结点,识别不到枚举值时会报错

固定分片hash算法

本条规则类似于十进制的求模运算,区别在于是二进制的操作,是取id的二进制低10位,即id二进制 &1111111111。

此算法的优点在于如果按照 10进制取模运算,在连续插入1-10 时候1-10会被分到1-10个分片,增 大了插入的事务控制难度,而此算法根据二进制则可能会分到连续的分片,减少插入事务事务控制难度。 

<tableRule name="rule1">
	<rule>
	    <columns>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>

配置说明:

columns :标识将要分片的表字段

algorithm :分片函数

partitionCount:分片个数列表

partitionLength:分片范围列表

分区长度默认最大1024

1024 = sum((count[i]*length[i])). count 和length两个向量的点积恒等于1024

范围约定

此分片适用于提前规划好分片字段某个范围属于哪个分片

<tableRule name="auto-sharding-long">
	<rule>
		<columns>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>
    <property name="defaultNode">0</property>
</function>

autopartition-long.txt文件配置
# range start-end ,data node index
# K=1000,M=10000.
0-500M=0
500M-1000M=1
1000M-1500M=2

 配置说明:

columns :标识将要分片的表字段

algorithm :分片函数

mapFile:切分配置文件名称

defaultNode:超出范围后默认路由节点

取模

对分片字段取模运算,根据分片字段进行十进制求模预算,相比固定分片hash,此种在批量插入时可能存在批量插入单 事务插入多数据分片,增大事务一致性难度。 

<tableRule name="mod-long">
	<rule>
		<columns>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>

  配置说明:

columns :标识将要分片的表字段

algorithm :分片函数

count:分片的数量

按日期(天)分片

<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">2018-08-01</property> 
    <property name="sEndDate">2018-08-31</property> 
    <property name="sPartionDay">15</property>
</function>

配置说明:

 columns :标识将要分片的表字段

algorithm :分片函数

dateFormat :日期格式

sBeginDate :开始日期

sEndDate:结束日期

sPartionDay :分区天数,即默认从开始日期算起,分隔 10天一个分区

注:

sEndDate代表数据达到了这个日期的分片后后循环从开始分片插入,若不配置数据到达分片时间范围后会报错

取模范围约束

取模运算与范围约束的结合,先取模然后根据取模值范围分片,主要为了后续数据迁移做准备,即可以自主决定取模后数据的节点 分布

<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">0</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=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 

配置说明:

 columns :标识将要分片的表字段

algorithm :分片函数

patternValue:取模基数

defaultNode:默认节点

mapFile:切分配置文件名称

截取数字做hash求模范围约束 

则类似于取模范围约束,此规则支持数据符号字母取模。 

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

partition-pattern.txt文件配置
# range start-end ,data node index
# ASCII
# 8-57=0-9 阿拉伯数字
# 64、65-90=@、A-Z 
# 97-122=a-z 
###### first host configuration
1-4=0
5-8=1
9-12=2
13-16=3
###### second host configuration
17-20=4
21-24=5
25-28=6
29-32=7
0-0=7

配置说明:

 columns :标识将要分片的表字段

algorithm :分片函数

patternValue:取模基数

prefixLength :ASCII 截取的位数 ,种获取前prefixLength位列所有ASCII 码的和进行求模 sum%patternValue

应用指定

在运行阶段由应用自主决定路由到那个分片,直接根据字符子串(必须是数字)计算分区号(由应用传递参数,显式指定分区号)

<tableRule name="sharding-by-substring">
    <rule>
        <columns>user_id</columns>
        <algorithm>sharding-by-substring</algorithm>
    </rule>
</tableRule>
<function name="sharding-by-substring" 
        class="org.opencloudb.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>

 配置说明:

 columns :标识将要分片的表字段

algorithm :分片函数

startIndex:截取开始位置

size:截取长度

partitionCount:分区数量

defaultPartition:默认分区号

例:id=05-100000002 在此配置中代表根据id中从 startIndex=0,开始,截取 siz=2位数字即05,05就是获取的分区,如果没传默认分配到defaultPartition 

截取数字hash解析 

是截取字符串中的int数值hash 分片

<tableRule name="sharding-by-stringhash">
    <rule>
        <columns>user_id</columns>
        <algorithm>sharding-by-stringhash</algorithm>
    </rule>
</tableRule>
<function name="sharding-by-stringhash"                 
        class="org.opencloudb.route.function.PartitionByString">
    <property name="partitionLength">512</property><!-- zero-based -->
    <property name="partitionCount">2</property>
    <property name="hashSlice">0:2</property>
</function>

配置说明:

 columns :标识将要分片的表字段

algorithm :分片函数

partitionLength:字符串hash求模基数

partitionCount:分区数

hashSlice:hash预算位,即根据子字符串中int值 hash运算,0 代表 str.length(), -1 代表 str.length()-1

例:值“45abc”,hash预算位0:2 ,取其中45进行计算

       值“aaaabbb2345”,hash预算位-4:0 ,取其中2345进行计算

一致性hash

一致性hash预算有效解决了分布式数据的扩容问题

<tableRule name="sharding-by-murmur">
    <rule>
        <columns>create_time</columns>
        <algorithm>sharding-by-murmur</algorithm>
    </rule>
</tableRule>
<function name="sharding-by-murmur"
        class="io.mycat.route.function.PartitionByMurmurHash">
    <property name="seed">0</property>
    <property name="count">3</property>
    <property name="virtualBucketTimes">160</property>
    <!--<property name="bucketMapPath">/etc/mycat/bucketMap</property>
    <property name="weightMapFile">weightMapFile</property> -->
</function>

配置说明:

 columns :标识将要分片的表字段

algorithm :分片函数

seed:hash 种子值

count: 要分片的数据库节点数量,必须指定,否则没法分片

virtualBucketTimes:一个实际的数据库节点被映射为多少虚拟节点,默认是 160 倍,也就是虚拟节点数是物理节点数的 160 倍

bucketMapPath:用于测试时观察各物理节点与虚拟节点的分布情况,如果指定了这个属性,会把虚拟节点的 murmur hash 值与物理节点的映射按行输出到这个文件,没有默认值,如果不指定,就不会输出任何东西 

weightMapFile:节点的权重,没有指定权重的节点默认是 1。以 properties 文件的格式填写,以从 0 开始到 count-1 的整数值也就是节点索引为 key,以节点权重值为值。所有权重值必须是正整数,否则以 1 代

按单月小时拆分

此规则是单月内按照小时拆分,最小粒度是小时,可以一天最多 24个分片,最少1个分片,一个月完后下月 从头开始循环。 每个月月尾,需要手工清理数据

<tableRule name="sharding-by-hour">
    <rule>
        <columns>create_time</columns>
        <algorithm>sharding-by-hour</algorithm>
    </rule>
</tableRule>
<function name="sharding-by-hour" class="io.mycat.route.function.LatestMonthPartion">
    <property name="splitOneDay">24</property>
</function>

配置说明:

columns: 拆分字段,字符串类型(yyyymmddHH)

algorithm :分片函数

splitOneDay : 一天切分的分片数

范围取模分片

<tableRule name="auto-sharding-rang-mod">
    <rule>
        <columns>id</columns>
        <algorithm>rang-mod</algorithm>
    </rule>
</tableRule>
<function name="rang-mod" class="io.mycat.route.function.PartitionByRangeMod">
    <property name="mapFile">partition-range-mod.txt</property>
    <property name="defaultNode">0</property>
</function>

partition-range-mod.txt文件配置
# range start-end ,data node group size
0-200M=5
200M1-400M=1
400M1-600M=4
600M1-800M=4
800M1-1000M=6

配置说明:

 columns :标识将要分片的表字段

algorithm :分片函数

mapFile:切分配置文件名称

defaultNode:默认节点

注:0-200M=5 表示在 0-200M范围中,有5个dataNode

日期范围hash分片

思想与范围求模一致,当由于日期在取模会有数据集中问题,所以改成 hash方法。 先根据日期分组,再根据时间hash使得短期内数据分布的更均匀,可以避免扩容时的数据迁移,又可以一定程度上避免范围分片的热点问题 

<tableRule name="sharding-by-range-date-hash">
    <rule>
        <columns>create_time</columns>
        <algorithm>sharding-by-range-date-hash</algorithm>
    </rule>
</tableRule>
<function name="sharding-by-range-date-hash"
        class="io.mycat.route.function.PartitionByRangeDateHash">
    <property name="dateFormat">yyyy-MM-dd HH:mm:ss</property>
    <property name="sBeginDate">2018-08-01 00:00:00</property>
    <property name="sPartionDay">3</property>
    <property name="groupPartionSize">6</property>
</function>

配置说明:

 columns :标识将要分片的表字段

algorithm :分片函数

dateFormat:日期格式

sBeginDate:开始日期,必须与dateFormat一致

sPartionDay:多少天分一个组

groupPartionSize:每组的分片数量
注:要求日期格式尽量精确些,不然达不到局部均匀的目的 

冷热数据分片

根据日期查询日志数据 冷热数据分布 ,最近n个月的到实时交易库查询,超过n个月的按照m天分片

<tableRule name="sharding-by-date">
	<rule>
		<columns>create_time</columns>
		<algorithm>sharding-by-hotdate</algorithm>
	</rule>
</tableRule>
<function name="sharding-by-hotdate" class="io.mycat.route.function.PartitionByHotDate">
	<property name="dateFormat">yyyy-MM-dd</property>
	<property name="sLastDay">10</property>
	<property name="sPartionDay">30</property>
</function>

配置说明:

 columns :标识将要分片的表字段

algorithm :分片函数

dataFormat:时间格式化

sLastDay:热数据的天数

sPartionDay:冷数据的分片天数(按照天数分片)

自然月分片

按照月份列分片,每个自然月一个分片

<tableRule name="sharding-by-month">
    <rule>
        <columns>create_time</columns>
        <algorithm>sharding-by-month</algorithm>
    </rule>
</tableRule>
<function name="sharding-by-month" class="io.mycat.route.function.PartitionByMonth">
    <property name="dateFormat">yyyy-MM-dd</property>
    <property name="sBeginDate">2018-08-01</property>
</function>

配置说明:

 columns :标识将要分片的表字段

algorithm :分片函数

dataFormat:时间格式化

sBeginDate:开始日期

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值