/*+ mycat:createTable{
"schemaName":"testSchema",
"shardingTable":{
"createTableSQL":"create table sharding(\nid int(11) NOT NULL AUTO_INCREMENT,\nuser_id int(11) ,\nuser_name varchar(128), \nPRIMARY KEY (`id`), \n GLOBAL INDEX `g_i_user_id`(`user_id`) COVERING (`user_name`) dbpartition by btree(`user_id`) \n)ENGINE=InnoDB DEFAULT CHARSET=utf8 ",
"function":{
"clazz":"io.mycat.router.custom.HttpCustomRuleFunction",
"properties":{
"name":"test",
"shardingDbKeys":"",
"shardingTableKeys":"id",
"shardingTargetKeys":"",
"allScanPartitionTimeout":5,
"fetchTimeout":60000,
"routerServiceAddress":"http://127.0.0.1:9066/router_service_address"
}
},
"partition":{
}
},
"tableName":"sharding"
} */;
"properties":{
"name":"test",//分片算法名字,用于识别ER关系,名字相同意味着有相同的数据分布
"shardingDbKeys":"",//分库键
"shardingTableKeys":"id",//分表键
"shardingTargetKeys":"",//分实例键
"allScanPartitionTimeout":5,//全表扫描结果缓存时间,单位:秒
"fetchTimeout":60000,//调用http服务超时时间,单位:毫秒
"routerServiceAddress":"http://127.0.0.1:9066/router_service_address"
//http服务地址
}
http://127.0.0.1:9066/router_service_address
是post请求,需要接收body
属于9066端口内置的例子,会自动映射到原型库上同库同表名的物理表
[{"targetName":"prototype","dbIndex":0,"tableIndex":1,"index":1,"table":"sharding","schema":"testschema"}]
在没有条件的时候,要返回全分片(分区)的数组
PHP代码
<?php
$data=$_POST;
$condition=json_decode($data['condition'],true);
if($condition['name']['value']=='test1')//name字段的值为test1
{
$result=array
(
'0' => Array
(
'targetName' => 'erp1',
'dbIndex' => 0,
'tableIndex' => 1,
'index' => 1,
'table' => 'student',
'schema' => 'db3',
),
);
}
elseif($condition['name']['value']=='test2')
{
$result=array
(
'0' => Array
(
'targetName' => 'erp2',
'dbIndex' => 1,
'tableIndex' => 1,
'index' => 2,
'table' => 'student',
'schema' => 'db3',
),
);
}
else{
$result=xxxxxx;//返回全分区
}
die(json_encode($result));
?>
每个根据分片条件返回的分区集合必须是全分区的子集