【Mycat 1.6之TableRule分片规则介绍】

1、TableRule介绍

1)schema.xml文件

<!-- 配置逻辑数据库名字,以及配置需要分片的表,也可以配置字典全局表或者不分片的表,总之你要纳入mycat管理的表就要在此配置 -->

<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">

<!-- auto sharding by id (long) -->

<table name="travelrecord" dataNode="dn1,dn2,dn3" rule="rule1" />

<!-- dataNode配置数据节点名字 ,分片规则参照rule.xml -->

<table name="t_gaojs" dataNode="dn1,dn2,dn3" rule="mod-long" />

</schema>

2)Rule.xml文件

<mycat:rule xmlns:mycat="http://io.mycat/">

<tableRule name="rule1">

<rule>

<columns>id</columns>

<algorithm>func1</algorithm>

</rule>

</tableRule>

<function name="func1" class="io.mycat.route.function.PartitionByLong">

<property name="partitionCount">8</property>

<property name="partitionLength">128</property>

</function>

</mycat:rule>

tableRule配置是

name 为schema.xml 中table 标签中对应的 rule="rule1" ,也就是配置表的分片规则,

columns 是表的切分字段: id创建日期。

algorithm 是规则对应的切分规则:映射到function 的name即func1

function 配置是分片规则的配置。

name 为切分规则的名称,名字人员取,但是需要与tableRule 中匹配。

class 是切分规则对应的切分类,写死,需要哪种规则则配置哪种,例如本例子是PartitionByLong分片:org.opencloudb.route.function.PartitionByLong

property 标签是切分规则对应的不同属性,不同的切分规则配置不同。partitionCount、partitionLength



 

2、自定义分片函数

一般情况是extends AbstractPartitionAlgorithm implements  RuleAlgorithm

分布式数据库系统中,分片规则用于定义数据与分片的路由关系,也就是 insert,delete,update,select

的基本 sql 操作中,如何将 sql 路由到对应的分片执行。

分片规则类 PartitionByDate 的配置属性与类的成员变量对应。 Mycat 的配置文件装载机制中,会根据 property 自动设置类的成员变量,因此只要设置了 Set…方法就可

以赋值。

init 方法:

主要处理每种规则的自定义处理

calculate 方法:

计算路由分片的核心方法,计算出偏移量即是分片节点,所有的分片节点编号都是从 0 开始编码。

calculateRange 方法:

calculateRange 方法默认根据继承的抽象类规则,可不实现,默认实现是获取分片字段的值连续范围内的所有分片,主要用于类似:update test where id<5; 这种语句中,通过解析条件 id<15 解析出所有的 id 值域分

片的对应关系,依次路由执行,[1->dn0,2->dn1,3->dn2,4->dn3].

package io.mycat.route.function;

import io.mycat.config.model.rule.RuleAlgorithm;

/**

 * 这是一个自定义的分片函数框架

 * @author gaojingsong<mail:[email protected]>

 *

 */

public class MyFun  extends AbstractPartitionAlgorithm implements RuleAlgorithm {

@Override

public void init() {

super.init();

}

@Override

public Integer calculate(String columnValue) {

return null;

}

}



 

猜你喜欢

转载自gaojingsong.iteye.com/blog/2339030