Mycat_枚举分片

版本:

Jdk1.8

Mycat 1.6

Mysql 5.5.53

 

运用场景:

一般用于固定数字或者字符串分类的数据表,例如按省份,区号等分类。

Rule.xml

<tableRule name="sharding-by-intfile">

<rule>

<columns>name</columns>

<algorithm>hash-int</algorithm>

</rule>

</tableRule>

 

sharding-by-intfile是规则名,在schema.xml中用

columns是对指定字段进行分片

algorithm是算法名,该算法必须在function中定义。

 

<!--枚举分片函数-->

<function name="hash-int"

class="io.mycat.route.function.PartitionByFileMap">

<property name="mapFile">partition-hash-int.txt</property>

<property name="type">1</property>

<property name="defaultNode">0</property>

</function>

mapFile是配置文件名

Type分片类型,默认值为00表示Integer,非零表示String

defaultNode 默认节点小于0表示不设置默认节点,大于等于0表示设置默认节点

 

cq=0

sz=1

hn=2

 

Cq会被分发到第一个节点中,sz分发被第二个节点中,hn第三个节点中。

 

总结:

1. 刚开始没有指定functiontype的值,即<property name="type">1</property>。导致insert时报以下错误,因为默认的分片类型是integer,而不是string;

 

2. 如果没有指定functiondefaultNode值,则插入不识别的枚举值时,会报错;

 

3. MyCAT支持动态加载功能

defaultNode的值定义到partition-hash-int-test.txt前提注释掉functiondefaultNode值,不然function中的defaultNode值会覆盖掉partition-hash-int-test.txtdefaultNode值。

cq=0

sz=1

hn=2

defaultNode=2

 

posted @ 2020-04-13 13:59  啄木鸟chens  阅读(205)  评论(0编辑  收藏  举报