mycat分表扩展之分片枚举

mycat分表扩展之分片枚举
1.修改schema.xml配置文件

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
	<schema name="orders1" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn1">
		<table name="customer" dataNode="dn2" ></table>
		<table name="orders" dataNode="dn1,dn2" rule="mod_rule" autoIncrement="true" fetchStoreNodeByJdbc="true">
			<!--orders_detail与orders表根据order_id关联-->
			<childTable name="orders_detail" primaryKey="id" joinKey="order_id" parentKey="id" />
		</table>
		<!--定义全局表-->
	   <table name="dict_order_type" dataNode="dn1,dn2" type="global" ></table>
	   <!--分片枚举-->
	   <table name="orders_ware_info" dataNode="dn1,dn2" rule="sharding_by_intfile" ></table>
	</schema>

	<dataNode name="dn1" dataHost="host1" database="orders1" />
	<dataNode name="dn2" dataHost="host2" database="orders1" />
	
	<dataHost name="host1" maxCon="1000" minCon="10" balance="0"
			  writeType="0" dbType="mysql" dbDriver="jdbc" switchType="1"  slaveThreshold="100">
		<heartbeat>select user()</heartbeat>
		<writeHost host="hostM1" url="jdbc:mysql://192.168.2.130:3306" user="root"
				   password="kht123">
		</writeHost>
	</dataHost>

	<dataHost name="host2" maxCon="1000" minCon="10" balance="0"
			  writeType="0" dbType="mysql" dbDriver="jdbc" switchType="1"  slaveThreshold="100">
		<heartbeat>select user()</heartbeat>
		<writeHost host="hostM2" url="jdbc:mysql://192.168.2.131:3306" user="root"
				   password="kht123">
		</writeHost>
	</dataHost>
</mycat:schema>

2.修改rule.xml配置文件

	<tableRule name="mod_rule">
			<rule>
				<columns>customer_id</columns>
				<algorithm>mod-long</algorithm>
			</rule>
	</tableRule>
	<!--
	# columns:分片字段,algorithm:分片函数
	# mapFile:标识配置文件名称,type:0为int型、非0为String,
	#defaultNode:默认节点:小于 0 表示不设置默认节点,大于等于 0 表示设置默认节点,
	#设置默认节点如果碰到不识别的枚举值,就让它路由到默认节点,如不设置不识别就报错
	-->
	<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>

3.修改partition-hash-int.txt配置文件

110=0
120=1
#订单归属区域信息表
CREATE TABLE orders_ware_info
(
`id` INT AUTO_INCREMENT comment '编号',
`order_id` INT comment '订单编号',
`address` VARCHAR(200) comment '地址',
`areacode` VARCHAR(20) comment '区域编号',
PRIMARY KEY(id)
);
INSERT INTO orders_ware_info(id, order_id,address,areacode) VALUES (1,1,'北京','110');
INSERT INTO orders_ware_info(id, order_id,address,areacode) VALUES (2,2,'天津','120');

4.查询Mycat、dn1、dn2可以看到数据分片效果

posted @ 2021-09-09 09:00  kht  阅读(71)  评论(0编辑  收藏  举报