1. /*+ mycat:createTable{
    2. "schemaName":"testSchema",
    3. "shardingTable":{
    4. "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 ",
    5. "function":{
    6. "clazz":"io.mycat.router.custom.HttpCustomRuleFunction",
    7. "properties":{
    8. "name":"test",
    9. "shardingDbKeys":"",
    10. "shardingTableKeys":"id",
    11. "shardingTargetKeys":"",
    12. "allScanPartitionTimeout":5,
    13. "fetchTimeout":60000,
    14. "routerServiceAddress":"http://127.0.0.1:9066/router_service_address"
    15. }
    16. },
    17. "partition":{
    18. }
    19. },
    20. "tableName":"sharding"
    21. } */;
    1. "properties":{
    2. "name":"test",//分片算法名字,用于识别ER关系,名字相同意味着有相同的数据分布
    3. "shardingDbKeys":"",//分库键
    4. "shardingTableKeys":"id",//分表键
    5. "shardingTargetKeys":"",//分实例键
    6. "allScanPartitionTimeout":5,//全表扫描结果缓存时间,单位:秒
    7. "fetchTimeout":60000,//调用http服务超时时间,单位:毫秒
    8. "routerServiceAddress":"http://127.0.0.1:9066/router_service_address"
    9. //http服务地址
    10. }

    http://127.0.0.1:9066/router_service_address

    是post请求,需要接收body

    属于9066端口内置的例子,会自动映射到原型库上同库同表名的物理表

    1. [{"targetName":"prototype","dbIndex":0,"tableIndex":1,"index":1,"table":"sharding","schema":"testschema"}]

    在没有条件的时候,要返回全分片(分区)的数组

    PHP代码

    1. <?php
    2. $data=$_POST;
    3. $condition=json_decode($data['condition'],true);
    4. if($condition['name']['value']=='test1')//name字段的值为test1
    5. {
    6. $result=array
    7. (
    8. '0' => Array
    9. (
    10. 'targetName' => 'erp1',
    11. 'dbIndex' => 0,
    12. 'tableIndex' => 1,
    13. 'index' => 1,
    14. 'table' => 'student',
    15. 'schema' => 'db3',
    16. ),
    17. );
    18. }
    19. elseif($condition['name']['value']=='test2')
    20. {
    21. $result=array
    22. (
    23. '0' => Array
    24. (
    25. 'targetName' => 'erp2',
    26. 'dbIndex' => 1,
    27. 'tableIndex' => 1,
    28. 'index' => 2,
    29. 'table' => 'student',
    30. 'schema' => 'db3',
    31. ),
    32. );
    33. }
    34. else{
    35. $result=xxxxxx;//返回全分区
    36. }
    37. die(json_encode($result));
    38. ?>

    每个根据分片条件返回的分区集合必须是全分区的子集