1,业务需求
上次分析聊天业务按照月进行拆。
具体拆分方案已经有了:

但是在操作的时候还是有点小问题,但基本上是按照这个设计实现的。
首先是mycat的,mycat正式版本是1.3.0.3-release,但是这个不包括PartitionByMonth这个类,其次PartitionByMonth 这个类的输入参数是日期也不好按月进行分表。
还好这类可以转换月,不用修改代码,也可以将就着用。
打包PartitionByMonth这个类生成一个jar。这个类在1.4-rc包里面有。将新jar放到lib目录下面。
#打包类io.mycat.route.function.PartitionByMonth。
jar -cvf Mycat-server-PartitionByMonth.jar *
PartitionByMonth这个类非常简单,对比下日期然后返回分区的序号。
如果业务复杂不是一个月一个月的分区可以直接写死逻辑然后打包使用,比如按季度分区,半个月一分区,或者在2015-06月以前是一个表以后是按月分区等等。
public class PartitionByMonth { private String sBeginDate; private String dateFormat; private Calendar beginDate; public void init() { try { beginDate = Calendar.getInstance(); beginDate.setTime(new SimpleDateFormat(dateFormat) .parse(sBeginDate)); } catch (ParseException e) { throw new java.lang.IllegalArgumentException(e); } } //通过时间计算返回分区号 0-n public Integer calculate(String columnValue) { try { Calendar curTime = Calendar.getInstance(); curTime.setTime(new SimpleDateFormat(dateFormat).parse(columnValue)); return (curTime.get(Calendar.YEAR) - beginDate.get(Calendar.YEAR)) * 12 + curTime.get(Calendar.MONTH) - beginDate.get(Calendar.MONTH); } catch (ParseException e) { throw new java.lang.IllegalArgumentException(e); } }
2,mycat 配置
首先创建数据库,默认分4个表,所有创建4个数据库,同理可以直接创建好一年的12个表,这里只是举例子。
CREATE DATABASE msg_201501 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; CREATE DATABASE msg_201502 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; CREATE DATABASE msg_201503 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; CREATE DATABASE msg_201504 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
在这4个数据库中创建表,表做10个分区(具体分区数可根据业务量划定,每个月的mysql分区可以不一样),按照gid做分区。
CREATE TABLE `msg` ( `id` bigint(20) NOT NULL, `gid` bigint(20) DEFAULT NULL COMMENT '群id,mysql分区字段', `content` varchar(4000), `create_time` datetime DEFAULT NULL COMMENT '创建时间', `create_month` int(6) DEFAULT NULL COMMENT '按月分表字段,如201501,不能为空。', PRIMARY KEY (`id`,`gid`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 PARTITION BY KEY(`gid`) PARTITIONS 10;
配置mycat 的rule.xml,这里用到了一个小技巧。month的格式化是
create_month
sharding-by-month
yyyyMM
201501
schema.xml配置:
select 1
server.xml配置:
druidparser
msg
msg
3,mysql 客户端测试
如果mycat启动正常,查看logs/wrapper.log没有异常,且数据库连接已经创建。
# mysql -umsg -pmsg -P8066 -h 127.0.0.1 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.5.8-mycat-1.3 MyCat Server (OpenCloundDB) Copyright (c) 2000, 2013, 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> use msg; mysql> mysql> insert into msg(`id`,`gid`,`content`,`create_time`,`create_month`) values(1,1,'java',now(),201501); Query OK, 1 row affected (0.00 sec) mysql> insert into msg(`id`,`gid`,`content`,`create_time`,`create_month`) values(2,1,'oracle',now(),201501); Query OK, 1 row affected (0.01 sec) mysql> insert into msg(`id`,`gid`,`content`,`create_time`,`create_month`) values(1,2,'ibm',now(),201501); Query OK, 1 row affected (0.00 sec) mysql> insert into msg(`id`,`gid`,`content`,`create_time`,`create_month`) values(2,2,'mys