MyCat专题(十三)-MyCat分片规则之程序指定分片

1.简介

实现方式:根据指定的分片字段以及分片规则,截取分片字段的字符串子串,根据字符串的子串(必须是数字)计算分区号(由调用方传递参数,显示指定分区号),例如,id=0-zhangsan,其中 id 是从 startIndex = 0,size=1,即截取的子串是0(数字) ,0 就是获取的分区,如果大于分区数,则分配到 defaultPartition 分区中。

优点:可以在运行阶段,由应用自主决定路由到那个分片
缺点:需要我们自己在应用里面实现分片规则

2.程序指定分片

【a】 创建数据库和表

create database custon_partition1;
use custon_partition1;
CREATE TABLE `user` (
  `id` bigint(20) NOT NULL,
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;
 
create database custon_partition2;
use custon_partition2;
CREATE TABLE `user` (
  `id` bigint(20) NOT NULL,
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

在这里插入图片描述
【b】配置server.xml

<user name="root">
        <property name="password">123456</property>
        <property name="schemas">TESTDB</property>
 
        <!-- 表级 DML 权限设置 -->
        <!--            
        <privileges check="false">
                <schema name="TESTDB" dml="0110" >
                        <table name="tb01" dml="0000"></table>
                        <table name="tb02" dml="1111"></table>
                </schema>
        </privileges>           
         -->
</user>
 
<user name="user">
        <property name="password">123456</property>
        <property name="schemas">TESTDB</property>
        <property name="readOnly">true</property>
</user>

【c】配置schema.xml分片表、分片节点等

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
 
    <schema name="TESTDB" checkSQLschema="true" sqlMaxLimit="1000">
        <table name="user" dataNode="dn1,dn2" primaryKey="id" rule="sharding-substring-partition" />
	</schema>
	 
	<dataNode name="dn1" dataHost="dataHost01" database="custom_partition1" />
	<dataNode name="dn2" dataHost="dataHost01" database="custom_partition2" />
	 
	<dataHost name="dataHost01" maxCon="1000" minCon="10" balance="0"
					  writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
			<heartbeat>select user()</heartbeat>
			<writeHost host="hostM1" url="192.168.70.128:3306" user="root" password="123" />
	</dataHost>
 
</mycat:schema>

【d】配置rule.xml分片规则

<tableRule name="sharding-substring-partition">
                <rule>
                         <columns>name</columns>
                        <algorithm>sharding-by-substring-user</algorithm>
                </rule>
        </tableRule>
 
        <function name="sharding-by-substring-user" class="io.mycat.route.function.PartitionDirectBySubString">
                 <property name="startIndex">0</property>
                <property name="size">1</property>
                <property name="partitionCount">2</property>
                <property name="defaultPartition">0</property>
        </function>

注意点:partitionCount分片数量最好与dataNode分片节点的个数保持一致。

【e】测试插入数据
在这里插入图片描述

insert into user(id,name) values(111,'0-zhangsan');
insert into user(id,name) values(222,'0-lisi');
insert into user(id,name) values(333,'1-wangwu');
insert into user(id,name) values(444,'3-zhaoliu');  --测试默认节点

在这里插入图片描述
如上图可见,成功实现了截取name的第一位作为分片节点的序号,也实现了没有满足提交分片节点的情况下默认分片的效果。

发布了301 篇原创文章 · 获赞 82 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/BruceLiu_code/article/details/104758332