mycat1.6.5分片(字串拆分hash)
阿新 • • 發佈:2018-12-19
mycat one群:106088787。
分片規則:字串拆分hash
一、conf/schema.xml檔案
二、 conf/rule.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="partition_by_string" primaryKey="ord_no" dataNode="dn$0-2" rule="partition-by-string" /> </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>
三、規則檔案資訊<?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"> <rule> <columns>ord_no</columns> <algorithm>partition-by-string</algorithm> </rule> </tableRule> <function name="partition-by-string" class="io.mycat.route.function.PartitionByString"> <property name="partitionLength">512</property> <!-- zero-based --> <property name="partitionCount">2</property> <property name="hashSlice">-6:0</property> </function> </mycat:rule>
無
四、測試用到的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進行計算