Mycat——常用的分片规则

分片枚举

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

<tableRule name="sharding-by-intfile"> <rule>
	<columns>user_id</columns>
	<algorithm>hash-int</algorithm> </rule>
</tableRule>
<function name="hash-int" class="org.opencloudb.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
DEFAULT_NODE=1
  • columns:标识要分片的表字段
  • argorithm:分片函数
  • mapFile:标识配置文件
  • type:默认为0,标识Integer,非零标识String
  • defaultNod:默认节点,枚举时如果遇到不识别的枚举值,路由到默认分片,节点从0开始计数(如果小于0则表示不配置默认节点,不识别的枚举值就会报错)

根据范围划分

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

<tableRule name="auto-sharding-long"> 
	<rule>
		<columns>user_id</columns>
		<algorithm>rang-long</algorithm> 
	</rule>
</tableRule>

<function name="rang-long" class="org.opencloudb.route.function.AutoPartitionByLong">
	<property name="mapFile">autopartition-long.txt</property>
	<property name="defaultNode">0</property> 
</function>

autopartition-long.txt

0-500M=0
500M-1000M=1
1000M-1500=2
或
0-10000000=0
10000000-20000000=1

求模

此规则为对分片字段求摸运算。

<tableRule name="mod-long"> 
	<rule>
		<columns>user_id</columns>
		<algorithm>mod-long</algorithm> 
	</rule>
</tableRule>
<function name="mod-long" class="org.opencloudb.route.function.PartitionByMod">
	<!-- how many data nodes --> 
	<property name="count">3</property>
</function>

此种配置非常明确即根据id进行十进制求模预算,相比固定分片hash,此种在批量插入时可能存在批量插入单事务插入多数据分 片,增大事务一致性难度。

按日期(天)分片

<tableRule name="sharding-by-date"> 
	<rule>
		<columns>create_time</columns>
		<algorithm>sharding-by-date</algorithm> 
	</rule>
</tableRule>
<function name="sharding-by-date" class="org.opencloudb.route.function.PartitionByDate">
	<property name="dateFormat">yyyy-MM-dd</property> 
	<property name="sBeginDate">2014-01-01</property> 
	<property name="sPartionDay">10</property>
</function>

配置说明:

  • columns :标识将要分片的表字段
  • algorithm :分片函数
  • dateFormat :日期格式
  • sBeginDate :开始日期
  • sBeginDate :结束日期,代表数据达到了这个日期的分片后循环从开始分片插入
  • sPartionDay :分区天数,即默认从开始日期算起,分隔10天一个分区
  • 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”));

固定分片Hash算法

本条规则类似于十进制的求模运算,区别在于是二进制的操作,是取id的二进制低10位,即id二进制&1111111111。 此算法的优点在于如果按照10进制取模运算,在连续插入1-10时候1-10会被分到1-10个分片,增大了插入的事务控制难度,而 此算法根据二进制则可能会分到连续的分片,减少插入事务事务控制难度。

<tableRule name="rule1"> 
	<rule>
		<columns>user_id</columns>
		<algorithm>func1</algorithm> 
	</rule>
</tableRule>
<function name="func1" class="org.opencloudb.route.function.PartitionByLong"> 
	<property name="partitionCount">2,1</property>
	<property name="partitionLength">256,512</property>
</function>
  • partitionCount 分片个数列表
  • partitionLength 分片范围列表 分区长度:默认为最大2^n=1024 ,即最大支持1024分区

约束 :
count和length两个向量的点积恒等于1024

用法例子: 本例的分区策略:希望将数据水平分成3份,前两份各占25%,第三份占50%。(故本例非均匀分区)
// |<———————1024————————>|
// |<—-256—>|<—-256—>|<———-512———->|
// | partition0 | partition1 | partition2 |
// | 共2份,故count[0]=2 | 共1份,故count[1]=1 |

取模范围约束

此种规则是取模运算与范围约束的结合,主要为了后续数据迁移做准备,即可以自主决定取模后数据的节点分布。

<tableRule name="sharding-by-pattern"> 
	<rule>
		<columns>user_id</columns>
		<algorithm>sharding-by-pattern</algorithm>
	</rule>
</tableRule>
<function name="sharding-by-pattern" class="org.opencloudb.route.function.PartitionByPattern">
	<property name="patternValue">256</property>
	<property name="defaultNode">2</property>
	<property name="mapFile">partition-pattern.txt</property>
</function>

partition-pattern.txt

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 即求模基数,defaoultNode 默认节点,如果配置了默认,则不会按照求模运算
mapFile 配置文件路径
配置文件中,1-32 即代表id%256后分布的范围,如果在1-32则在分区1,其他类推,如果id非数据,则会分配在defaoultNode 默认节点

ASCII码期末范围约束

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

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

patternValue 即求模基数,prefixLength ASCII 截取的位数
配置文件中,1-32 即代表id%256后分布的范围,如果在1-32则在分区1,其他类推

此种方式类似方式6只不过采取的是将列种获取前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">
 <!-- zero-based --> 
  <property name="startIndex">0</property>
  <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

字符串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=length>512</property> <!-- zero-based --> 		
	<property name="count">2</property>
	<property name="hashSlice">0:2</property>
</function>

函数中length代表字符串hash求模基数,count分区数,hashSlice hash预算位 即根据子字符串中int值 hash运算

hashSlice : 0 means str.length(), -1 means str.length()-1

一致性hash

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

<tableRule name="sharding-by-murmur"> 
	<rule>
		<columns>user_id</columns>
		<algorithm>murmur</algorithm> </rule>
</tableRule>

<function name="murmur" class="org.opencloudb.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>

按单月小时拆分

此规则是单月内按照小时拆分,最小粒度是小时,可以一天最多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="org.opencloudb.route.function.LatestMonthPartion">
	<property name="splitOneDay">24</property> 
</function>

columns: 拆分字段,字符串类型(yyyymmddHH)
splitOneDay : 一天切分的分片数

自然月分片

按月份列分区 ,每个自然月一个分片,格式 between操作解析的范例。

<tableRule name="sharding-by-month"> 
	<rule>
		<columns>create_time</columns>
		<algorithm>sharding-by-month</algorithm> 
	</rule>
</tableRule>
<function name="sharding-by-month" class="org.opencloudb.route.function.PartitionByMonth">
	<property name="dateFormat">yyyy-MM-dd</property>
	<property name="sBeginDate">2014-01-01</property> 
</function>

columns: 分片字段,字符串类型
dateFormat : 日期字符串格式
sBeginDate : 开始日期

Guess you like

Origin blog.csdn.net/cold___play/article/details/107500938