暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

mongodb生成数据-sharding测试准备

原创 zhou 2024-03-13
80

JS函数准备

注:准备几个js函数,为后续测试mongodb分片生成数据

主要生成个人姓名,性别,年龄,省份,手机号码等字段;

#性别

function getSex() {  
    var sex_array=["男","女"]
    var sex = sex_array[Math.floor(Math.random() *sex_array.length )];
    return sex;
}

#名字

function getName(sex){
    var first = ["赵",  "钱",  "孙",  "李",  "周",  "吴",  "郑",  "王",  "冯",  "陈" ];
    var male_last = [ "宇轩","梓轩","俊宇","梓睿","铭轩","梓豪","睿","伟","杰","军","强","勇","建华" ];
    var female_last = [ "芷晴","可昕","梓晴","秀英 ","桂英","秀兰","玥","悦","可","悠","钰","敏","艳","丽","静","玉兰" ];
    var last=[]
    if (sex=='男')
    {
        last=male_last 
    }else{
       last=female_last;
   };
    var name = first[Math.floor(Math.random()*first.length)];
    name+=last[Math.floor(Math.random()*last.length)];
    return name;
}

#手机号码

function getPhone() {
    const phonePrefixes = ['134', '135', '136', '137', '138', '139', '147', '150', '151', '152', '157', '158', '159', '165', '172', '178', '182', '183', '184', '187', '188', '198'];
    const prefix = phonePrefixes[Math.floor(Math.random() * phonePrefixes.length)];
    const middleDigits = Math.floor(Math.random() * (8999)) + 1000;
    const lastDigits = Math.floor(Math.random() * (8999)) + 1000;
    return `${prefix}${middleDigits}${lastDigits}`;
}

#省份

function getProvinces() {
    const provinces = [
        '广东', '北京', '上海', '江苏', '浙江', '四川',
        '河南', '湖南', '湖北', '江西', '安徽', '浙江',
        '山东', '河北', '山西', '内蒙古', '甘肃', '云南',
        '甘肃', '广西', '西藏', '甘肃', '甘肃', '甘肃'
    ];
    const index = Math.floor(Math.random() * provinces.length);
    return provinces[index];
}

#年龄

function getAge() {  
    var age =Math.floor(Math.random() *99+1 );
    return age;
}

连接mongos造数

连接mongos
mongo --port 27000 --host 127.0.0.1
use admin
db.auth("admin","Admin@01");

20240313150743image.png

执行

use testdb;

生成数据

启用sharding

如:按_id来做分区键

sh.enableSharding("testdb")
sh.shardCollection("testdb.person",{_id:"hashed"})


20240313152311image.png
生成1万条记录

for(var i=0; i<200; i++){ 
    var docs = new Array();
     for(var j=0; j<50; j++) {

                    var sex = getSex() ;
                    var name=getName(sex);                                   
                    var age =getAge(); 
                    var province=getProvinces();
                    var phonenumber=getPhone();
                    var id=ObjectId()
                    docs[j] = {"_id":id,"name":name,"sex":sex,"age":age,"province":province,"phonenumber":phonenumber};                            
 }          
db.person.insertMany(docs);                
}


20240313162257image.png

load scripts

注:保存脚本到文件中,避免每次都需要手动输入

vi /home/mongo/testdb_func.js
function getSex() {  
    var sex_array=["男","女"]
    var sex = sex_array[Math.floor(Math.random() *sex_array.length )];
    return sex;
}
function getName(sex){
    var first = ["赵",  "钱",  "孙",  "李",  "周",  "吴",  "郑",  "王",  "冯",  "陈" ];
    var male_last = [ "宇轩","梓轩","俊宇","梓睿","铭轩","梓豪","睿","伟","杰","军","强","勇","建华" ];
    var female_last = [ "芷晴","可昕","梓晴","秀英 ","桂英","秀兰","玥","悦","可","悠","钰","敏","艳","丽","静","玉兰" ];
    var last=[]
    if (sex=='男')
    {
        last=male_last 
    }else{
       last=female_last;
   };
    var name = first[Math.floor(Math.random()*first.length)];
    name+=last[Math.floor(Math.random()*last.length)];
    return name;
}
function getPhone() {
    const phonePrefixes = ['134', '135', '136', '137', '138', '139', '147', '150', '151', '152', '157', '158', '159', '165', '172', '178', '182', '183', '184', '187', '188', '198'];
    const prefix = phonePrefixes[Math.floor(Math.random() * phonePrefixes.length)];
    const middleDigits = Math.floor(Math.random() * (8999)) + 1000;
    const lastDigits = Math.floor(Math.random() * (8999)) + 1000;
    return `${prefix}${middleDigits}${lastDigits}`;
}
function getProvinces() {
    const provinces = [
        '广东', '北京', '上海', '江苏', '浙江', '四川',
        '河南', '湖南', '湖北', '江西', '安徽', '浙江',
        '山东', '河北', '山西', '内蒙古', '甘肃', '云南',
        '甘肃', '广西', '西藏', '甘肃', '甘肃', '甘肃'
    ];
    const index = Math.floor(Math.random() * provinces.length);
    return provinces[index];
}
function getAge() {  
    var age =Math.floor(Math.random() *99+1 );
    return age;
}

for(var i=0; i<200; i++){ 
    var docs = new Array();
     for(var j=0; j<50; j++) {

                    var sex = getSex() ;
                    var name=getName(sex);                                   
                    var age =getAge(); 
                    var province=getProvinces();
                    var phonenumber=getPhone();
                    var id=ObjectId()
                    docs[j] = {"_id":id,"name":name,"sex":sex,"age":age,"province":province,"phonenumber":phonenumber};                            
 }          
db.person.insertMany(docs);                
}

加载脚本

use testdb
load("/home/mongo/testdb_func.js")


20240313180914image.png

最后修改时间:2024-03-13 18:12:56
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论