1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
刚开始看教程资料的时候,看教程文档感觉模糊,完全没明白分片枚举是个什么样的概念。于是网上搜素别人做的
案例来看,终于让我搜索到一份完整的测试案例,见如下地址:
  
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> 
 
 
成功了。