1. 程式人生 > >mycat分片規則之分片枚舉(sharding-by-intinfile)

mycat分片規則之分片枚舉(sharding-by-intinfile)

別人 ade 員工信息 cat oracle server register 正常 cor

剛開始看教程資料的時候,看教程文檔感覺模糊,完全沒明白分片枚舉是個什麽樣的概念。於是網上搜素別人做的 案例來看,終於讓我搜索到一份完整的測試案例,見如下地址: https://www.cnblogs.com/ivictor/archive/2016/01/25/5155123.html 看完這個案例,恍然大悟教程裏說的按照省份區縣保存的意思。謝謝網上無償分享文檔的人們。 好了。來開始測試。 在schema.xml裏定義一個分片表,如下: [root@mysql1 conf]# vi schema.xml <schema name="hello" checkSQLschema="false" sqlMaxLimit="100"> <!-- auto sharding by id (long) --> <table name="t1" dataNode="dn1,dn2,dn3" rule="auto-sharding-long" /> <table name="t6" dataNode="dn1,dn2,dn3" rule="sharding-by-intfile" /> t1是前面定義的範圍分片表,現在定義的是t6,規則是sharding-by-infile就是分片枚舉規則。 在rule.xml裏看看規則的定義 <tableRule name="sharding-by-intfile"> <rule> <columns>sharding_id</columns> <algorithm>hash-int</algorithm> </rule> </tableRule> </function> <function name="hash-int" class="io.mycat.route.function.PartitionByFileMap"> <property name="mapFile">partition-hash-int.txt</property> </function> 看看paritition-hash-int.txt的內容 [root@mysql1 conf]# cat partition-hash-int.txt 10000=0 10010=1 以上都是默認的內容,現在修改成我們想要的樣子,想法是,我的t6表格用來存取各地公司員工信息。 想根據地區來分片。 t6表格定義四列:id,name,bu,city。 根據city來分片。 需要配置的文件:schema.xml設置t6分片表和定義分片規則;rule.xml根據實際情況修改分片表的列字段; partition-hash-int.txt定義不同分片分別存儲到哪個datanode。 前面配置好了schema.xml裏 t6表分片和分片規則。 修改rule.xml裏的分片表列字段 <tableRule name="sharding-by-intfile"> <rule> <columns>city</columns> <algorithm>hash-int</algorithm> </rule> </tableRule> 改成city,我們是用city這個字段列來分片。 修改partition-hash-int.txt。這是測試,我準備插入三條數據,city分別是bj,gz,sz。規劃bj的數據放入datanode1, gz的數據放入datanode2,sz的數據放入datanode3。 配置如下: [root@mysql1 conf]# cat partition-hash-int.txt #10000=0 #10010=1 bj=0 gz=1 sz=2 重啟mycat服務,然後測試。 [root@mysql1 conf]# ../bin/mycat restart Stopping Mycat-server... Stopped Mycat-server. Starting Mycat-server... [root@mysql1 conf]# 重啟後 [root@mysql1 conf]# mysql -uroot -p123456 -P8066 -h 127.0.0.1 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.6.29-mycat-1.6-RELEASE-20161028204710 MyCat Server (OpenCloundDB) Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> 數據庫進來了 選擇hello數據庫,創建t6。 mysql> create table t6(id int not null,name varchar(20) not null,bu varchar(20) not null,city varchar(10) not null); Query OK, 0 rows affected (1.03 sec) 插入數據測試下 mysql> insert into t6(id,name,bu,city)values(1,'am1','caiwu','bj'); ERROR 1064 (HY000): columnValue:bj Please check if the format satisfied. 發現報錯。看文檔教程: <property name="type">0</property> <property name="defaultNode">0</property> 說明如下:函數配置中,ype默認值為0,0表示Integer,非零表示String, 所有的節點配置都是從0開始,及0代表節點1。 /** * defaultNode 默認節點:小於0表示不設置默認節點,大於等於0表示設置默認節點 * 默認節點的作用:枚舉分片時,如果碰到不識別的枚舉值,就讓它路由到默認節點 * 如果不配置默認節點(defaultNode值小於0表示不配置默認節點),碰到 * 不識別的枚舉值就會報錯, * like this:can’t find datanode for sharding column:column_name val:ffffffff */ 應該是這個問題,我的city字段是字符,而默認是integer。修改測試看看。 修改rule.xml,改成如下: </function> <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> 重啟mycat服務。 再插入數據,正常 mysql> insert into t6(id,name,bu,city)values(1,'am1','caiwu','bj'); Query OK, 1 row affected (0.45 sec) mysql> insert into t6(id,name,bu,city)values(2,'am2','caiwu','gz'); Query OK, 1 row affected (0.02 sec) mysql> insert into t6(id,name,bu,city)values(3,'am3','caiwu','sz'); Query OK, 1 row affected (0.02 sec) mysql> 看看實現分片了嗎? mysql> select * from t6; +----+------+-------+------+ | id | name | bu | city | +----+------+-------+------+ | 2 | am2 | caiwu | gz | | 3 | am3 | caiwu | sz | | 1 | am1 | caiwu | bj | +----+------+-------+------+ 3 rows in set (0.37 sec) mysql> explain select * from t6; +-----------+----------------------------+ | DATA_NODE | SQL | +-----------+----------------------------+ | dn1 | SELECT * FROM t6 LIMIT 100 | | dn2 | SELECT * FROM t6 LIMIT 100 | | dn3 | SELECT * FROM t6 LIMIT 100 | +-----------+----------------------------+ 3 rows in set (0.01 sec) mysql> 成功了。


mycat分片規則之分片枚舉(sharding-by-intinfile)