1. 程式人生 > >mycat1.6.5分片(按小時)

mycat1.6.5分片(按小時)

mycat one群:106088787。

說明:根據欄位按照一天內的小時分片。

註釋:欄位為字串型別,yyyymmddHH 10位。

一、conf/schema.xml檔案

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
	<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
		<table name="sharding_by_hour" primaryKey="id" dataNode="dn$0-2"
			   rule="sharding-by-hour" />   
	</schema>
	<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"
				   password="123456">
		</writeHost>
	</dataHost>
</mycat:schema>
二、 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="sharding-by-hour">
		<rule>
			<columns>sharding_col</columns>
			<algorithm>sharding-by-hour</algorithm>
		</rule>
	</tableRule>
	<function name="sharding-by-hour"
		class="io.mycat.route.function.LatestMonthPartion">
		<property name="splitOneDay">3</property>
	</function>
</mycat:rule>

三、規則檔案資訊

四、測試用到的sql

=============按照create_time欄位 按 小時分================
CREATE TABLE  sharding_by_hour  (create_time   timestamp NULL ON UPDATE CURRENT_TIMESTAMP  ,`db_nm`  varchar(20) NULL,sharding_col varchar(10) null);
INSERT INTO sharding_by_hour (sharding_col,create_time,db_nm) VALUES ('2017100101','2017-10-01', database());
INSERT INTO sharding_by_hour (sharding_col,create_time,db_nm) VALUES ('2017100108','2017-10-01', database());
INSERT INTO sharding_by_hour (sharding_col,create_time,db_nm) VALUES ('2017100109','2017-10-01', database());
INSERT INTO sharding_by_hour (sharding_col,create_time,db_nm) VALUES ('2017100116','2017-10-01', database());
INSERT INTO sharding_by_hour (sharding_col,create_time,db_nm) VALUES ('2017100117','2017-10-01', database());
INSERT INTO sharding_by_hour (sharding_col,create_time,db_nm) VALUES ('2017100123','2017-10-01', database());
select * from sharding_by_hour;
五、注意事項

1、分片數必須跟dataNode節點數一致

2、最大分24個分片

3、分片欄位的值必須是字串,如:2017102112.(即為2017年10月21日12點)

六、引數說明

columns: 拆分欄位,字串型別(yyyymmddHH)

splitOneDay : 一天切分的分片數