Mycat sharding rule sharding-by-murmur
The murmur algorithm hash fields and distribute them to different databases. The field types support int and varchar.
1. rule. xml
- <TableRule name = "sharding-by-murmur-userid">
- <Rule>
- <Columns> userid </columns>
- <Algorithm> murmur </algorithm>
- </Rule>
- </TableRule>
- <Function name = "murmur"
- Class = "io. mycat. route. function. partitionbymurhash">
- <Property name = "seed"> 0 </property> <! -- The default value is 0 -->
- <Property name = "count"> 6 </property> <! -- The number of database nodes to be sharded must be specified; otherwise, the database nodes cannot be sharded. -->
- <Property name = "virtualBucketTimes"> 160 </property> <! -- If an actual database node is mapped to so many virtual nodes, the default value is 160 times.
- The number of virtual nodes is 160 times the number of physical nodes -->
- <! -- <Property name = "weightMapFile"> weightMapFile </property> indicates the node weight. If no weight is specified, the default value is 1. Take the properties File
- Enter the value in the format, starting from 0 to count-1, that is, the node index is the key, and the node weight is the value. The ownership value must be a positive integer. Otherwise, replace 1 with -->
- <! -- <Property name = "bucketMapPath">/etc/mycat/bucketMapPath </property>
- Observe the distribution of each physical node and virtual node during the test. If this attribute is specified, the murmur hash value of the virtual node is mapped to the physical node.
- Output to this file by row. No default value exists. If this parameter is not specified, nothing will be output. -->
- </Function>
Name = "count" is set based on the number of sub-databases. I have 6 sub-databases here.
2. schema. xml configuration file
- <? Xml version = "1.0"?>
- <! DOCTYPE mycat: schema SYSTEM "schema. dtd">
- <Mycat: schema xmlns: mycat = "http://io.mycat/">
- <Schema name = "logdb" checkSQLschema = "false" sqlMaxLimit = "100">
- <Table name = "tb_log_t" primaryKey = "ID" autoIncrement = "true" dataNode = "dn1, dn2, dn3, dn4, dn5, dn6 "rule =" mod-long "/>
- <Table name = "tb_user_t" dataNode = "dn1, dn2, dn3, dn4, dn5, dn6" rule = "sharding-by-intfile-provcode"/>
- <Table name = "tb_user_detail_t" dataNode = "dn1, dn2, dn3, dn4, dn5, dn6" rule = "auto-sharding-long-userid"/>
- <Table name = "tb_user_murmur_t" dataNode = "dn1, dn2, dn3, dn4, dn5, dn6" rule = "sharding-by-murmur"/>
- <Table name = "tb_user_murmur_string_t" dataNode = "dn1, dn2, dn3, dn4, dn5, dn6" rule = "sharding-by-murmur-userid"/>
- </Schema>
- <Schema name = "coss03" checkSQLschema = "false" sqlMaxLimit = "100" dataNode = "dnoss03"/>
- <DataNode name = "dn1" dataHost = "localhost1" database = "log01"/>
- <DataNode name = "dn2" dataHost = "localhost1" database = "log02"/>
- <DataNode name = "dn3" dataHost = "localhost1" database = "log03"/>
- <DataNode name = "dn4" dataHost = "localhost1" database = "log04"/>
- <DataNode name = "dn5" dataHost = "localhost1" database = "log05"/>
- <DataNode name = "dn6" dataHost = "localhost1" database = "log06"/>
- <DataNode name = "dnoss03" dataHost = "localhost1" database = "oss03"/>
- <DataHost name = "localhost1" maxCon = "1000" minCon = "10" balance = "0"
- WriteType = "0" dbType = "mysql" dbDriver = "native" switchType = "1" slaveThreshold = "100">
- <Heartbeat> select user ()
- <! -- Can have multi write hosts -->
- <WriteHost host = "hostM1" url = "192.168.56.141: 3306" user = "root"
- Password = "mysql">
- <! -- Can have multi read hosts -->
- <ReadHost host = "hostS2" url = "192.168.56.142: 3306" user = "root" password = "mysql"/>
- </WriteHost>
- </DataHost>
- </Mycat: schema>
3. Create a table
Create table 'tb _ user_murmur_string_t '(
'Userid' varchar (32) not null,
'Name' varchar (64) default null,
'Createtime' datetime DEFAULT CURRENT_TIMESTAMP,
'Moditytime' datetime DEFAULT CURRENT_TIMESTAMP,
Primary key ('userid ')
) ENGINE = InnoDB default charset = utf8;
Write Data
Insert into tb_user_murmur_string_t (userid, name) values ('user002', 'name002 ');
Insert into tb_user_murmur_string_t (userid, name) values ('user003 ', 'name003 ');
Insert into tb_user_murmur_string_t (userid, name) values ('user004 ', 'name004 ');
Insert into tb_user_murmur_string_t (userid, name) values ('user005 ', 'name005 ');
Insert into tb_user_murmur_string_t (userid, name) values ('user006 ', 'name006 ');
Insert into tb_user_murmur_string_t (userid, name) values ('user007 ', 'name007 ');
Insert into tb_user_murmur_string_t (userid, name) values ('user008 ', 'name008 ');
Insert into tb_user_mur_string_t (userid, name) values ('user009', 'name009 ');
Insert into tb_user_murmur_string_t (userid, name) values ('user010 ', 'name010 ');
-- The End --