概述

myCat实现分库分表的策略,对数据量的处理带来很大的便利,这里主要整理下MyCat的使用以及常用路由算法,针对MyCat里面的事务、集群后续再做整理;另外内容整理,不免会参考技术大牛的博客,内容雷同,实属正常;基于业务区分数据源,主要为了实现如下的数据库

常规使用

配置schema.xml  在同一个mysql数据库中,创建了三个数据库 testdb1,testdb2,testdb3。并在每个库中都创建了user表
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/"> <schema name="testdb" checkSQLschema="false" sqlMaxLimit="100” >
<!——指定rule 分片规则-->
<table name="user" dataNode="dn1,dn2,dn3" rule="sharding-by-intfile" />
</schema> <dataNode name="dn1" dataHost="host" database="testdb1" />
<dataNode name="dn2" dataHost="host" database="testdb2" />
<dataNode name="dn3" dataHost="host" database="testdb3" /> <dataHost name="host" maxCon="1000" minCon="10" balance="0"
writeType="0" dbType="mysql" dbDriver="native">
<heartbeat>select 1</heartbeat>
<writeHost host="hostM1" url="localhost:3306" user="root" password="123" />
</dataHost> </mycat:schema>
配置server.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/">
<system>
<property name="defaultSqlParser">druidparser</property>
</system>
<user name="mycat">
<property name="password">mycat</property>
<property name="schemas">testdb</property>
</user>
</mycat:server>
配置rule.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://io.mycat/“>
<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>
</function>
</mycat:rule>

常用的分片规则

一、枚举法

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

这个是针对Int类型的枚举算法,如果是标识字符串枚举,可将function做如下调整: <property name="type">1</property>

partition-hash-int.txt 文件配置:

10000=0

10010=1

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

其中分片函数配置中,mapFile标识配置文件名称,type默认值为0,0表示Integer,非零表示String

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

/**

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

*

默认节点的作用:枚举分片时,如果碰到不识别的枚举值,就让它路由到默认节点

*                如果不配置默认节点(defaultNode值小于0表示不配置默认节点),碰到

*                不识别的枚举值就会报错,

*                like this:can't find datanode for sharding column:column_name val:ffffffff

*/

二、固定分片hash算法(总体长度1024)

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

配置说明:

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

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,即按照上述分区策略,offerId为12345时将会被分配到partition0中

int partNo1 = pu.partition(offerId);

// 若根据memberId分配,partNo2将等于2,即按照上述分区策略,memberId为qiushuo时将会被分到partition2中

int partNo2 = pu.partition(memberId, 0, DEFAULT_STR_HEAD_LEN);

Assert.assertEquals(0, partNo1);

Assert.assertEquals(2, partNo2);

}

如果需要平均分配设置:平均分为4分片,partitionCount*partitionLength=1024

<function name="func1" class="org.opencloudb.route.function.PartitionByLong">

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

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

</function>

三、范围约定

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

# range start-end ,data node index

# K=1000,M=10000.

0-500M=0

500M-1000M=1

1000M-1500M=2

0-10000000=0

10000001-20000000=1

配置说明:

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

rang-long 函数中mapFile代表配置文件路径

所有的节点配置都是从0开始,及0代表节点1,此配置非常简单,即预先制定可能的id范围到某个分片

四、求模法

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

配置说明:

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

此种配置非常明确即根据id与count(你的结点数)进行求模预算,相比方式1,此种在批量插入时需要切换数据源,id不连续

五、日期列分区法

<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">2014-01-01</property>
<property name="sPartionDay">10</property>
</function>

配置说明:

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

配置中配置了开始日期,分区天数,即默认从开始日期算起,分隔10天一个分区

六、通配取模

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

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 即求模基数,defaoultNode 默认节点,如果不配置了默认,则默认是0即第一个结点

mapFile 配置文件路径

配置文件中,1-32 即代表id%256后分布的范围,如果在1-32则在分区1,其他类推,如果id非数字数据,则会分配在defaoultNode 默认节点

String idVal = "0";

Assert.assertEquals(true, 7 == autoPartition.calculate(idVal));

idVal = "45a";

Assert.assertEquals(true, 2 == autoPartition.calculate(idVal));

七、ASCII码求模通配

<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

# 48-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 截取的位数

mapFile 配置文件路径

配置文件中,1-32 即代表id%256后分布的范围,如果在1-32则在分区1,其他类推

此种方式类似方式6只不过采取的是将列种获取前prefixLength位列所有ASCII码的和进行求模sum%patternValue ,获取的值,在通配范围内的

即 分片数,

/**

* ASCII编码:

* 48-57=0-9阿拉伯数字

* 64、65-90=@、A-Z

* 97-122=a-z

*/

String idVal="gf89f9a";

Assert.assertEquals(true, 0==autoPartition.calculate(idVal));

idVal="8df99a";

Assert.assertEquals(true, 4==autoPartition.calculate(idVal));

idVal="8dhdf99a";

Assert.assertEquals(true, 3==autoPartition.calculate(idVal));

八、其他分区:按月(12个月)和天分区(24区)

<function name="latestMonth"
class="io.mycat.route.function.LatestMonthPartion">
<property name="splitOneDay">24</property>
</function>
<function name="partbymonth"
class="io.mycat.route.function.PartitionByMonth">
<property name="dateFormat">yyyy-MM-dd</property>
<property name="sBeginDate">2015-01-01</property>
</function>

尝试MyCat总结

1、MyCat宣称对Oracle数据库进行支持,但是也仅仅局限于常规的语句,对MyCat的链接驱动还是要mysql,一些常规的登录,转到Oracle语句就报错;如果想基于MyCat做分库分表机制,还是建议DB选择:mySQL

2、MyCat配置完整之后,数据表对接,都是小写的;如果应用框架(Spring-Oracle)采用Table名称大写查询操作,MyCat是没法予以支持;如果是(Spring-Mysql)框架模式,到时可以修改Mysql配置,不区分大写小属性完成;

MyCat分片规则--笔记(二)的更多相关文章

  1. mycat分片规则之分片枚举(sharding-by-intinfile)

    mycat分片规则之分片枚举(sharding-by-intinfile) http://blog.51cto.com/goome/2058959 mycat安装及分片初体验 https://blog ...

  2. Mycat 分片规则详解--单月小时分片

    实现方式:单月内按照小时拆分,最小粒度是小时,一天最多可以有24个分片,最少1个分片,下个月从头开始循环 优点:使数据按照小时来进行分时存储,颗粒度比日期(天)分片要小,适用于数据采集类存储分片 缺点 ...

  3. Mycat 分片规则详解--日期(天)分片

    实现方式:按照日期来分片 优点:使数据按照日期来进行分时存储 缺点:由于数据是连续的,所以该方案不能有效的利用资源 配置示例: <tableRule name="sharding-by ...

  4. Mycat 分片规则详解--应用指定分片

    实现方式:根据字符串的子串(必须是数字)计算分区号(由调用方传递参数,显示指定分区号),例如,id=05-12232323,其中 id 是从 startIndex = 0,size=2,即截取的子串是 ...

  5. Mycat 分片规则详解--取模分片

    实现方式:切分规则根据配置中输入的数值n.此种分片规则将数据分成n份(通常dn节点也为n),从而将数据均匀的分布于各节点上. 优点:这种策略可以很好的分散数据库写的压力.比较适合于单点查询的情景 缺点 ...

  6. Mycat 分片规则详解--枚举分片

    实现方式:切分规则根据文件(partition-hash-int.txt)配置的可能的枚举来进行分片,此种分片规则理解为枚举分区,会比较适合于取值固定的场合,比如说省份(固定值) 优点:适用于按照省份 ...

  7. mycat系列-Mycat 分片规则

    分片规则概述 在数据切分处理中,特别是水平切分中,中间件最终要的两个处理过程就是数据的切分.数据的聚合.选择合适的切分规则,至关重要,因为它决定了后续数据聚合的难易程度,甚至可以避免跨库的数据聚合处理 ...

  8. Mycat分片规则详解

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

  9. Mycat 分片规则详解--数据迁移及节点扩容

    使用的是 Mycat 提供的 dataMigrate 脚本进行对数据进行迁移和节点扩容,目前支持的 Mycat 是1.6 版本,由于 Mycat 是由 Java 编写的因此在做数据迁移及节点扩容时需要 ...

随机推荐

  1. MySQL基于GTIDs的MySQL Replication

    MySQL M-S GTID 基于GTIDs的MySQL Replication 什么是GTIDs以及有什么特定? 1.GTIDs(Global transaction identifiers)全局事 ...

  2. 关于objdump的博文整理

    objdump主要用于查看对象文件的内容信息 objdump一些基本命令:http://www.169it.com/article/330129798173630299.html 使用readelf和 ...

  3. Oracle windows64位 百度云下载链接

    oracle11g安装包 去官网需要登录 这个是百度云盘链接 链接:https://pan.baidu.com/s/18lYrkqqHG8u4aDdQekHc3g 提取码:fg2v

  4. 什么是Annotation

    Annotation 被称为注解,在Java开发中是相当常见的,通过注解,我们可以简化代码提高开发效率.例如Override Annotation,这个应该算是在开发过程中使用最多的注解了.注解(An ...

  5. 状压dp(状态压缩&&dp结合)学习笔记(持续更新)

    嗯,作为一只蒟蒻,今天再次学习了状压dp(学习借鉴的博客) 但是,依旧懵逼·································· 这篇学习笔记是我个人对于状压dp的理解,如果有什么不对的 ...

  6. 【转】Redis学习笔记(四)如何用Redis实现分布式锁(1)—— 单机版

    原文地址:http://bridgeforyou.cn/2018/09/01/Redis-Dsitributed-Lock-1/ 为什么要使用分布式锁 这个问题,可以分为两个问题来回答: 为什么要使用 ...

  7. Web_0002:关于MongoDB的操作

    1,启动moggdb服务端 打开cmd命令窗口进入到MongoDB的安装目录bin文件下: 如:  cd /d F:\Program Files\mongodb\bin   执行如下命令(该命令窗口为 ...

  8. Swift 4 关于Darwin这个Module

    大家在做app或者framework的时候经常会用到生成随机数的方法arc4random系列,或者取绝对值abs()等.所以我有一次好奇的想看看在Developer Document里 是怎么描述这些 ...

  9. IIS中报错弹出调试,系统日志-错误应用程序名称: w3wp.exe,版本: 8.5.9600.16384,时间戳: 0x5215df96(360主机卫士)

    偶遇一次特殊情况,在使用Web系统导入数据模版(excel)时,服务端IIS会报错并弹出调试框,然后整个网站都处于卡死的debug状态,如果点否不进行调试,则IIS会中断调试,Web系统继续执行,运行 ...

  10. luogu P3810 三维偏序(陌上花开)cdq分治

    题目链接 思路 对一维排序后,使用$cdq$分治,以类似归并排序的方法处理的二维,对于满足$a[i].b \leq a[j].b$的点对,用树状数组维护$a[i].c$的数量.当遇到$a[i].b&g ...