mycat分片规则之分片枚举

转载自 https://blog.51cto.com/goome/2058959

在conf/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="sharding-by-infile" />

现在定义的是表 t1,规则是sharding-by-infile就是分片枚举规则。

在conf/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>

看看conf/paritition-hash-int.txt的内容

  [root@mysql1 conf]# cat partition-hash-int.txt 
	10000=0
	10010=1

以上都是默认的内容,现在修改成我们想要的样子,想法是,我的t1表格用来存取各地公司员工信息。

想根据地区来分片。

 t1表格定义四列:id,name,bu,city。

 根据city来分片。

 

需要配置的文件:schema.xml设置t1分片表和定义分片规则;rule.xml根据实际情况修改分片表的列字段;

partition-hash-int.txt定义不同分片分别存储到哪个datanode。

 

前面配置好了schema.xml里 t1表分片和分片规则。

修改rule.xml里的分片表列字段

  <tableRule name="sharding-by-intfile">
                <rule>
                        <columns>city</columns>
                        <algorithm>hash-int</algorithm>
                </rule>
    </tableRule>

# columns内容改成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数据库,创建t1。

   mysql> create table t1(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 t1(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>

成功了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值