CREATE TABLE `xxxxxxxx` (`crttm` int(11) NOT NULL,`srvid` int(11) NOT NULL,`evtid` int(11) NOT NULL,`aid` int(11) NOT NULL,`rid` int(11) NOT NULL,`itmid` int(11) NOT NULL,`itmnum` int(11) NOT NULL,`gdtype` int(11) NOT NULL,`gdnum` int(11) NOT NULL,`islmt` int(11) NOT NULL,KEY `crttm` (`crttm`),KEY `itemid` (`itmid`),KEY `srvid` (`srvid`),KEY `gdtype` (`gdtype`)) ENGINE=myisam DEFAULT CHARSET=utf8PARTITION BY RANGE (crttm)(PARTITION p201303 VALUES LESS THAN (unix_timestamp('2014-04-01')),PARTITION p201304 VALUES LESS THAN (unix_timestamp('2014-05-01')),PARTITION p201305 VALUES LESS THAN (unix_timestamp('2014-06-01')),PARTITION p201306 VALUES LESS THAN (unix_timestamp('2014-07-01')),PARTITION p201307 VALUES LESS THAN (unix_timestamp('2014-08-01')),PARTITION p201308 VALUES LESS THAN (unix_timestamp('2014-09-01')),PARTITION p201309 VALUES LESS THAN (unix_timestamp('2014-10-01')),PARTITION p201310 VALUES LESS THAN (unix_timestamp('2014-11-01')),PARTITION p201311 VALUES LESS THAN (unix_timestamp('2014-12-01')),PARTITION p201312 VALUES LESS THAN (unix_timestamp('2015-01-01')),PARTITION p201401 VALUES LESS THAN (unix_timestamp('2015-02-01')));
if userId % 3 == 0:#insert data in user_table (user_table_0 databaseip: 127.0.0.1)elif userId % 3 == 1:#insert data in user_table (user_table_1 databaseip: 127.0.0.2)else:#insert data in user_table (user_table_2 databaseip: 127.0.0.3)
// 加载模块,返回HashRing的构造函数var HashRing = require('hashring');
//实例化HashRing,这个例子中,我们把各个服务器均匀的添加了,没有设置权重// 设置了最大的缓冲区 10000var ring = new HashRing(['127.0.0.1','127.0.0.2','127.0.0.3','127.0.0.4'], 'md5', {'max cache size': 10000});
//我们获取这个字符串的服务器ipvar server = ring.get('foo bar banana'); // returns 127.0.0.xconsole.log(server)
// 如果你想把数据冗余的存储在多个服务器上ring.range('foo bar banana', 2).forEach(function forEach(server) {console.log(server); // do stuff with your server});
// 对环上移除或新增加一台服务器ring.add('127.0.0.7').remove('127.0.0.1');
var server = ring.get('foo bar banana'); // returns 127.0.0.xconsole.log(server)
var HashRing = require('hashring');var ring = new HashRing(['127.0.0.1','127.0.0.2','127.0.0.3',], 'md5', {'max cache size': 10000});
var record = {'127.0.0.1':0,'127.0.0.2':0,'127.0.0.3':0};var userMap = {}
for(var i=1; i<=30000; i++){var userIdStr = i.toString();var server = ring.get(userIdStr);userMap[userIdStr] = server;record[server]++;}
console.log(record);
{ '127.0.0.1': 9162, '127.0.0.2': 9824, '127.0.0.3': 11014 }
var HashRing = require('hashring');var ring = new HashRing(['127.0.0.1','127.0.0.2','127.0.0.3',], 'md5', {'max cache size': 10000});
var record = {'127.0.0.1':0,'127.0.0.2':0,'127.0.0.3':0};var userMap = {}for(var i=1; i<=50000; i++){var userIdStr = i.toString();var server = ring.get(userIdStr);userMap[userIdStr] = server;record[server]++;}
console.log(record);
//新增加2个sharding节点var record2 = {'127.0.0.1':0,'127.0.0.2':0,'127.0.0.3':0,'127.0.0.4':0,'127.0.0.5':0,};ring.add('127.0.0.4').add('127.0.0.5')
var moveStep = 0;for(var i=1; i<=50000; i++){var userIdStr = i.toString();var server = ring.get(userIdStr);//当用户的存储server改变,则计算移动if(userMap[userIdStr] && userMap[userIdStr] != server){userMap[userIdStr] = server;moveStep++;}record2[server]++;}console.log(record2);console.log('move step:'+moveStep);
{ '127.0.0.1': 15238, '127.0.0.2': 16448, '127.0.0.3': 18314 }
{ '127.0.0.1': 8869,'127.0.0.2': 9972,'127.0.0.3': 10326,'127.0.0.4': 10064,'127.0.0.5': 10769 }
move step:20833
20833 = 10064 + 10769