https://blog.csdn.net/webnum/article/details/78313525
分片规则:字符串拆分hash
一、conf/schema.xml文件
-
-
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
-
<mycat:schema xmlns:mycat="http://io.mycat/">
-
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
-
<table name="partition_by_string" primaryKey="ord_no" dataNode="dn$0-2"
-
rule="partition-by-string" />
-
-
<dataNode name="dn0" dataHost="dh-1" database="db0"/>
-
<dataNode name="dn1" dataHost="dh-1" database="db1"/>
-
<dataNode name="dn2" dataHost="dh-1" database="db2"/>
-
<dataHost name="dh-1" maxCon="1000" minCon="10" balance="0"
-
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
-
<heartbeat>select user()</heartbeat>
-
<writeHost host="hostM1" url="localhost:3306" user="root"
-
-
-
-
二、conf/rule.xml文件
-
<?xml version="1.0" encoding="UTF-8"?>
-
<!DOCTYPE mycat:rule SYSTEM "rule.dtd">
-
<mycat:rule xmlns:mycat="http://io.mycat/">
-
<tableRule name="partition-by-string">
-
-
<columns>ord_no</columns>
-
<algorithm>partition-by-string</algorithm>
-
-
-
<function name="partition-by-string"
-
class="io.mycat.route.function.PartitionByString">
-
<property name="partitionLength">512</property>
-
<property name="partitionCount">2</property>
-
<property name="hashSlice">-6:0</property>
-
-
三、规则文件信息
无
四、测试用到的sql
-
=============按照ord_no字段 字符串hash ================
-
CREATE TABLE partition_by_string (ord_no varchar(20) NULL,`db_nm` varchar(20) NULL);
-
INSERT INTO `partition_by_string` (ord_no,db_nm) VALUES (171022237582, database());
-
INSERT INTO `partition_by_string` (ord_no,db_nm) VALUES (171022553756, database());
-
select * from partition_by_string;
五、注意事项
分片数量必须小于等于dataNode数
六、参数说明
length代表字符串hash求模基数,count分区数,其中length*count=1024
hashSlice hash预算位,即根据子字符串中int值 hash运算
0 代表 str.length(), -1 代表 str.length()-1,大于0只代表数字自身
可以理解为substring(start,end),start为0则只表示0
例1:值“45abc”,hash预算位0:2 ,取其中45进行计算
例2:值“aaaabbb2345”,hash预算位-4:0 ,取其中2345进行计算