飞道的博客

Mongodb实验二——分片集群搭建

497人阅读  评论(0)

 

目录

前言

题目一

1.创建副本集实例(Mongod)

cfg0_0配置 

cfg0_1配置

cfg0_2配置

ch0_0配置

ch0_1配置

ch0_2配置

ch1_0配置

 ch1_1配置

ch1_2配置

2.初始化副本集

 ch0conf.js配置

ch1conf.js配置

 3.创建路由节点(分片管理服务器)Mongos

4.添加分片集合

5.Windows批处理文件编写

题目二

1.分片式数据库创建

 2.确认集合片集

 3.分片集合创建

 4.另外一种方法

题目三

1.分片集状态

 2.分片数据库的统计信息

 3.分片集合数据分布情况

前言

本实验在一台电脑上完成,本人自己方法与标准方法略有差异,大家仁者见仁,同时可能你按照一样的步骤下来,会出现各种各样的错误,但希望大家懂得学会去查看日志,查看代码,懂得如何解决问题,这是一个有趣的过程,共勉。

题目一

根据下面的规划表搭建MongoDB分片集群:

 步骤:

1.创建副本集实例(Mongod)

即两个分片服务器ch0,ch1(以大家名字缩写命名)和一个配置服务器cfg0,每个服务器下有包含三个服务,即ch0(ch0_0,ch0_1,ch0_2),ch1(ch1_0,ch1_1,ch1_2),cfg0(cfg0_0,cfg0_1,cfg0_2),于是我们可以开始配置每个服务的配置文件即ini文件,由于个人强迫症原因,喜欢每个服务器用一个文件夹保存(这里大家可以全部放在一个文件夹下,不用这样的)如下图所示

 在cf0服务器下将每个服务用一个文件夹保存

cfg0_0配置 

在cfg0_0这个服务呀创建一个ini文件,然后编写参数(这里你可以自己手动创建db文件夹和log文件,也可以不用创建,只要你在配置文件中把dbpath和logpath明确就行,因为在Windows执行的时候会自己创建的)

 

bind_ip=127.0.0.1
port=27026

dbpath=D:\MongoDB\ch\cfg0\cfg0_0\db
logpath=D:\MongoDB\ch\cfg0\cfg0_0\cfg0_0.log
logappend=true

replSet=cfg0
configsvr=true

然后其他的服务类似配置,这里不细说了,图片展示一下

cfg0_1配置

 

bind_ip=127.0.0.1
port=27027

dbpath=D:\MongoDB\ch\cfg0\cfg0_1\db
logpath=D:\MongoDB\ch\cfg0\cfg0_1\cfg0_1.log
logappend=true

replSet=cfg0
configsvr=true

cfg0_2配置

bind_ip=127.0.0.1
port=27028

dbpath=D:\MongoDB\ch\cfg0\cfg0_2\db
logpath=D:\MongoDB\ch\cfg0\cfg0_2\cfg0_2.log
logappend=true

replSet=cfg0
configsvr=true

ch0_0配置

 bind_ip=127.0.0.1
port=27020

dbpath=D:\MongoDB\ch\ch0\ch0_0\db
logpath=D:\MongoDB\ch\ch0\ch0_0\ch0_0.log
logappend=true

replSet=ch0
shardsvr=true

ch0_1配置

 bind_ip=127.0.0.1
port=27021

dbpath=D:\MongoDB\ch\ch0\ch0_1\db
logpath=D:\MongoDB\ch\ch0\ch0_1\ch0_1.log
logappend=true

replSet=ch0
shardsvr=true

ch0_2配置

 bind_ip=127.0.0.1
port=27022

dbpath=D:\MongoDB\ch\ch0\ch0_2\db
logpath=D:\MongoDB\ch\ch0\ch0_2\ch0_2.log
logappend=true

replSet=ch0
shardsvr=true

ch1_0配置

bind_ip=127.0.0.1
port=27023

dbpath=D:\MongoDB\ch\ch1\ch1_0\db
logpath=D:\MongoDB\ch\ch1\ch1_0\ch1_0.log
logappend=true

replSet=ch1
shardsvr=true

 ch1_1配置

 

bind_ip=127.0.0.1
port=27024

dbpath=D:\MongoDB\ch\ch1\ch1_1\db
logpath=D:\MongoDB\ch\ch1\ch1_1\ch1_1.log
logappend=true

replSet=ch1
shardsvr=true

ch1_2配置

 bind_ip=127.0.0.1
port=27025

dbpath=D:\MongoDB\ch\ch1\ch1_2\db
logpath=D:\MongoDB\ch\ch1\ch1_2\ch1_2.log
logappend=true

replSet=ch1
shardsvr=true

2.初始化副本集

即初始化两个分片服务器(ch0,ch1)和一个配置服务器(cfg0)

这里我分布在ch0,ch1,cfg0下创建了一个js文件(可自己随便在某个位置创建这三个文件)

cfgoconf.js文件配置

 


  
  1. var rsconf={
  2. _id: "cfg0",
  3. "members":[
  4. { _id: 0, host: "127.0.0.1:27026", priority: 2},
  5. { _id: 1, host: "127.0.0.1:27027", priority: 1},
  6. { _id: 2, host: "127.0.0.1:27028", priority: 1}
  7. ]};
  8. rs. initiate(rsconf)

 ch0conf.js配置


  
  1. var rsconf={
  2. _id: "ch0",
  3. "members":[
  4. { _id: 0, host: "127.0.0.1:27020", priority: 2},
  5. { _id: 1, host: "127.0.0.1:27021", priority: 1},
  6. { _id: 2, host: "127.0.0.1:27022", priority: 0, arbiterOnly: true}
  7. ]};
  8. rs. initiate(rsconf)

ch1conf.js配置


  
  1. var rsconf={
  2. _id: "ch1",
  3. "members":[
  4. { "_id": 0, "host": "127.0.0.1:27023", priority: 2},
  5. { "_id": 1, "host": "127.0.0.1:27024", priority: 1},
  6. { "_id": 2, "host": "127.0.0.1:27025", priority: 0, arbiterOnly: true}
  7. ]};
  8. rs. initiate(rsconf)

 3.创建路由节点(分片管理服务器)Mongos

我用了一个文件夹来保存这个服务器,如下图

 然后在该文件夹下编写其配置文件ini

 bind_ip=0.0.0.0
port=27017

logpath=D:\MongoDB\ch\router0\mongos.log
logappend=true

configdb=cfg0/127.0.0.1:27026,127.0.0.1:27027,127.0.0.1:27028

4.添加分片集合

我在router0下创建了一个js文件,其配置参数如下


  
  1. sh. addShard( "ch0/127.0.0.1:27020,127.0.0.1:27021,127.0.0.1:27022");
  2. sh. addShard( "ch1/127.0.0.1:27023,127.0.0.1:27024,127.0.0.1:27025");

 到了这里所有配置的基本配置完成,只需要进行启动就好,为了每次启动便捷,我们这里编写一个windows批处理文件

5.Windows批处理文件编写

 先创建一个txt文件,然后配置好后,把后缀改为bat文件即可

REM 0. 配置环境变量 (根据你的实际环境来配置)
SET MONGOD_CMD=D:\MongoDB\bin\mongod.exe
SET MONGOS_CMD=D:\MongoDB\bin\mongos.exe
SET MONGO_CMD=D:\MongoDB\bin\mongo.exe

REM 1. 创建9台服务器实例(server1~server9)路径//已经手动创建,故这里不再创建
REM //mkdir D:\MongoDB\ch\cfg0,D:\MongoDB\ch\cfg0\cfg0_0,D:\MongoDB\ch\cfg0\cfg0_1........这样下去
REM 2. 编写9台服务器的启动配置文件

REM 手动完成 cfg0_0.ini ~ ch1_3.ini

REM 3. 启动配置集、分片集的副本集实例
REM 配置集 cfg0
start "cfg0_0:27026" %MONGOD_CMD% --config=D:\MongoDB\ch\cfg0\cfg0_0\cfg0_0.ini
start "cfg0_1:27027" %MONGOD_CMD% --config=D:\MongoDB\ch\cfg0\cfg0_1\cfg0_1.ini
start "cfg0_2:27028" %MONGOD_CMD% --config=D:\MongoDB\ch\cfg0\cfg0_2\cfg0_2.ini
REM 分片集1 rs0
start "ch0_0:27020" %MONGOD_CMD% --config=D:\MongoDB\ch\ch0\ch0_0\ch0_0.ini
start "ch0_1:27021" %MONGOD_CMD% --config=D:\MongoDB\ch\ch0\ch0_1\ch0_1.ini
start "ch0_2:27022" %MONGOD_CMD% --config=D:\MongoDB\ch\ch0\ch0_2\ch0_2.ini
REM 分片集2 rs1
start "ch1_0:27023" %MONGOD_CMD% --config=D:\MongoDB\ch\ch1\ch1_0\ch1_0.ini
start "ch1_1:27024" %MONGOD_CMD% --config=D:\MongoDB\ch\ch1\ch1_1\ch1_1.ini
start "ch1_2:27025" %MONGOD_CMD% --config=D:\MongoDB\ch\ch1\ch1_2\ch1_2.ini

REM 4. 编写副本集初始化脚本for分片集和配置集
REM 手动完成 rs0conf.js, rs1conf.js, cfg0conf.js

REM 等待5秒确保所有服务器都启动了
TIMEOUT /T 5

REM 5. 初始化配置集、分片集的副本集实例
start "连接到分片1" %MONGO_CMD% --port=27020 --shell D:\MongoDB\ch\ch0\ch0conf.js
start "连接到分片2" %MONGO_CMD% --port=27023 --shell D:\MongoDB\ch\ch1\ch1conf.js
start "连接到配置集" %MONGO_CMD% --port=27026 --shell D:\MongoDB\ch\cfg0\cfg0conf.js

REM 等待11秒确保副本集完成选举
TIMEOUT /T 11

REM 6. 启动路由节点/分片服务器 mongos
start "router0:27017" %MONGOS_CMD% --config=D:\MongoDB\ch\router0\ruoter0.ini

REM 等待2秒确保路由节点完成启动
TIMEOUT /T 10

REM 7. 添加分片到分片集群
REM 手动编写脚本
REM // 添加rs0
REM sh.addShard("ch0/127.0.0.1:27020,127.0.0.1:27021,127.0.0.1:27022");
REM // 添加rs1
REM sh.addShard("ch1/127.0.0.1:27023,127.0.0.1:27024,127.0.0.1:27025");
start "连接到分片集群" %MONGO_CMD% --shell D:\MongoDB\ch\router0\addShards.js

这个有个小注意的地方,如果是你自己创建的txt文本后,请注意一下编码,如果运行后出现乱码问题,将编码改为ANSI。

5.启动服务

出现这四个页面则说明你成功了,恭喜哦

题目二

在分布式集群中创建分片集合: 

 GenerateStudents.js文件如下


  
  1. //生成随机手机号
  2. function getRandPhone( ){
  3. var heads = [ "134", "138", "139", "150", "151", "152", "157", "158", "159", "170", "189"];
  4. var phone = heads[ Math. floor( Math. random()*heads. length)];
  5. var numbers = [ 1, 2, 3, 4, 5, 6, 7, 8, 9];
  6. for( var i= 0; i< 8; i++){
  7. phone+=numbers[ Math. floor( Math. random()*numbers. length)];
  8. }
  9. return phone;
  10. }
  11. //生成车牌号
  12. function getRandPlate( ){
  13. //地区,用于生成车牌号
  14. // var loc = ["川","渝","贵","陕","京","沪","粤","津","赣","湘","鄂"];
  15. var loc = [ "川", "渝"];
  16. //字母,用于生成车牌号
  17. var chars = [ "A", "B", "C", "D"];
  18. //数字,用于生成车牌号
  19. var numbers = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, "J", "K", "L", "M"];
  20. var plate = "";
  21. plate += loc[ Math. floor( Math. random()*loc. length)]; //第一位地区简称
  22. plate += chars[ Math. floor( Math. random()*chars. length)]; //第二位字母简称
  23. //4位数字
  24. for( var i= 0; i< 4; i++){
  25. plate+=numbers[ Math. floor( Math. random()*numbers. length)];
  26. }
  27. return plate;
  28. }
  29. // print(getRandPlate())
  30. //生成名字
  31. function getRandName( ){
  32. var familyNames = [ "赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈",
  33. "褚", "卫", "蒋", "沈", "韩", "杨", "朱", "秦", "尤", "许",
  34. "何", "吕", "施", "张", "孔", "曹", "严", "华", "金", "魏",
  35. "陶", "姜", "戚", "谢", "邹", "喻", "柏", "水", "窦", "章",
  36. "云", "苏", "潘", "葛", "奚", "范", "彭", "郎", "鲁", "韦",
  37. "昌", "马", "苗", "凤", "花", "方", "俞", "任", "袁", "柳",
  38. "刘", "鲍", "史", "唐", "费", "廉", "岑", "薛", "雷", "贺",
  39. "倪", "汤", "滕", "殷", "罗", "毕", "郝", "邬", "安", "常",
  40. "乐", "于", "时", "傅", "皮", "卞", "齐", "董", "伍", "余",
  41. "元", "卜", "顾", "孟", "平", "黄", "和", "谭", "萧", "尹"
  42. ];
  43. var givenNames = [ "子璇", "淼", "国栋", "夫子", "瑞堂", "甜", "敏", "尚", "国贤", "贺祥", "晨涛",
  44. "昊轩", "易轩", "益辰", "益帆", "益冉", "瑾春", "瑾昆", "春齐", "杨", "文昊",
  45. "东东", "雄霖", "浩晨", "熙涵", "溶溶", "冰枫", "欣欣", "宜豪", "欣慧", "建政",
  46. "美欣", "淑慧", "文轩", "文杰", "欣源", "忠林", "榕润", "欣汝", "慧嘉", "新建",
  47. "建林", "亦菲", "林", "冰洁", "佳欣", "涵涵", "禹辰", "淳美", "泽惠", "伟洋",
  48. "涵越", "润丽", "翔", "淑华", "晶莹", "凌晶", "苒溪", "雨涵", "嘉怡", "佳毅",
  49. "子辰", "佳琪", "紫轩", "瑞辰", "昕蕊", "萌", "明远", "欣宜", "泽远", "欣怡",
  50. "佳怡", "佳惠", "晨茜", "晨璐", "运昊", "汝鑫", "淑君", "晶滢", "润莎", "榕汕",
  51. "佳钰", "佳玉", "晓庆", "一鸣", "语晨", "添池", "添昊", "雨泽", "雅晗", "雅涵",
  52. "清妍", "诗悦", "嘉乐", "晨涵", "天赫", "玥傲", "佳昊", "天昊", "萌萌", "若萌",
  53. "泽民", "国强", "胜利", "小凡", "碧瑶", "书书", "京雨", "卫东", "小佳", "长江",
  54. "文虹", "义凡", "广晨", "宸滔", "嘉岐", "雅珺", "睿明", "皓轩", "程天", "子酝",
  55. "艾康", "如羽", "冠玉", "子歉", "永昊", "龙华", "兆颜", "奇文", "月昕", "裕锦",
  56. "昂佳", "昊浩", "宇韬", "睿焓", "永译", "鸿彬", "颢霖", "益彬", "虹昊", "飞悦",
  57. "睿珏", "?宵童", "睿鸿", "容冰", "逸濠", "楷岩", "弘义", "海萦", "昊孺", "梓铭",
  58. "生钊", "蓝玺", "晨辕", "宇菡", "砚海", "文揩", "韬瑞", "彦红", "奕韦", "清予",
  59. "宁翼", "冬睿", "锦昌", "烨宁", "昌权", "国研", "德运", "孝清", "佳阳", "凯玮",
  60. "正真", "民云", "昕冶", "力威", "帅欣", "知淳", "烨飞", "兴远", "子墨", "澄欣",
  61. "烨煊", "悦勤", "晨津", "博宏", "育萌", "羽炫", "绍钧", "睿昌", "泓千", "颢炜",
  62. "虹金", "筠航", "元甲", "星明", "景涛", "铭虹", "德本", "向辉", "基翔", "家易",
  63. "欣鹏", "羽荃", "泽容", "弘亮", "尚廷", "轩梓", "甫津", "彬楷", "寅飞", "愉君",
  64. "阳平", "誉杰", "钦昭", "蕴藉", "羽程", "宏海", "涵畅", "光浩", "令沂", "浩浩",
  65. "睿锦", "易泽", "俊康", "家文", "晨元", "语洋", "裕宏", "梓榛", "阳嘉", "恒展",
  66. "雨远", "哲伊", "逸江", "丰源", "学东", "奇岩", "浩财", "和蔼", "红言", "瑞赫",
  67. "森圆", "欣赢", "梓鸿", "博明", "铭育", "颢硕", "宇烯", "宇如", "淳炎", "源承"
  68. ];
  69. var name = familyNames[ Math. floor( Math. random()*familyNames. length)];
  70. name+=givenNames[ Math. floor( Math. random()*givenNames. length)];
  71. return name;
  72. }
  73. // print(getRandName())
  74. // 生成随机专业
  75. function getRandMajor( ) {
  76. return [ "大数据", "应用数学", "应用统计"][ Math. floor( Math. random()* 3)]
  77. }
  78. // print(getRandMajor())
  79. // 随机取一门课程
  80. function getRandCourse( ) {
  81. var courses = [ "大数据概论", "大数据开发语言", "Hadoop大数据技术",
  82. "分布式数据库原理与应用", "网络操作系统", "结构化数据存储与应用",
  83. "数据导入与预处理应用", "数据可视化技术", "数据仓库与挖掘技术",
  84. "时间序列分析实践", "大数据分析与内存计算"
  85. ];
  86. var credits = [ 2, 4, 4,
  87. 4, 2, 2,
  88. 4, 2, 4,
  89. 4, 4
  90. ];
  91. var idx = Math. floor( Math. random()*courses. length);
  92. var course = [courses[idx],credits[idx]];
  93. return course;
  94. }
  95. // 生成学生数据
  96. function generateStudents( snum=100, sgrade=2020, overwrite=true, sharding=true) {
  97. db = db. getSiblingDB( "cqust");
  98. // use cqust;
  99. if (sharding) {
  100. sh. enableSharding( "cqust");
  101. }
  102. if (overwrite) {
  103. //db.getCollection("students").drop();
  104. // db.students.drop() 删除students集合
  105. if (sharding){
  106. sh. shardCollection( "cqust.students",{ "sno": "hashed"});
  107. }
  108. }
  109. for( var i= 0; i<snum/ 100; i++){
  110. var StudentsDoc = new Array();
  111. for( var j= 0; j< 100; j++){
  112. var sid = sgrade* 1e6+i* 100+j;
  113. var sname = getRandName();
  114. var gender = Math. floor( Math. random()* 2);
  115. var phone = getRandPhone();
  116. var plate = getRandPlate();
  117. var major = getRandMajor();
  118. var height = Math. floor( Math. random()* 30+gender* 5+ 150);
  119. var weight = Math. floor( Math. random()* 30+gender* 20+ 40);
  120. var sclass = Math. floor( Math. random()* 4+ 1);
  121. var courses = new Array();
  122. for( var k= 0; k< Math. floor( Math. random()* 5);k++){
  123. var course_credit = getRandCourse();
  124. courses[k] = { "course":course_credit[ 0], "credit":course_credit[ 1], "score": Math. floor( Math. random()* 50)+ 50};
  125. }
  126. StudentsDoc[j] = {
  127. "sno":sid,
  128. "name":sname,
  129. "gender":gender,
  130. // "height":height,
  131. // "weight":weight,
  132. "body":{
  133. "height":height,
  134. "weight":weight
  135. },
  136. "list":[ Math. ceil( Math. random()* 6), Math. ceil( Math. random()* 6), Math. ceil( Math. random()* 6)],
  137. "phone":phone,
  138. "role": "student",
  139. "major": major,
  140. "grade": sgrade,
  141. "class": sclass,
  142. "courses":courses
  143. };
  144. }
  145. db. getCollection( "students"). insertMany( StudentsDoc);
  146. // db.students.insertMany(StudentsDoc)
  147. }
  148. }
  149. // 主程序从这里开始
  150. generateStudents( 1000, 2018, true, false);
  151. generateStudents( 1000, 2019, false, false);
  152. generateStudents( 1000, 2020, false, false);
  153. generateStudents( 1000, 2021, false, false);

 首先咱们先load一下js文件,先创建个数据库,到入点数据进去这个load里面是跟这个js文件的路径哈,我因为这个文件就保存再这个目录下才没跟哈。

 然后我们浅浅的看一下数据

 长酱紫,咱们正式开始

1.分片式数据库创建

输入命令:

sh.enableSharding("cqust")

出现ok:1你就没问题如果是ok:0你就有问题了,你就完了

 2.确认集合片集

输入命令:

db.students.createIndex({sno:"hashed"})

就是给集合创建哈希索引,这里我们以学号sno为索引创建,出现ok:1就没问题,不然你就完了

 3.分片集合创建

输入命令:

sh.shardCollection("cqust.students",{sno:"hashed"})

同理,出现ok:1成功,不然你就完了

最后呢我们可以输入 sh.status() 命令看一下分片集状态,看看是否真的配好了,看不懂的参数自己百度(像酱紫基本就没问题了)

 4.另外一种方法

另外一种方法,即编写js脚本文件

这里浅浅给大家分析一下GenerateStudents.js文件,

 而你在后面调用的时候 

然后我对代码进行小小的修改

 修改后的js代码


  
  1. //生成随机手机号
  2. function getRandPhone( ){
  3. var heads = [ "134", "138", "139", "150", "151", "152", "157", "158", "159", "170", "189"];
  4. var phone = heads[ Math. floor( Math. random()*heads. length)];
  5. var numbers = [ 1, 2, 3, 4, 5, 6, 7, 8, 9];
  6. for( var i= 0; i< 8; i++){
  7. phone+=numbers[ Math. floor( Math. random()*numbers. length)];
  8. }
  9. return phone;
  10. }
  11. //生成车牌号
  12. function getRandPlate( ){
  13. //地区,用于生成车牌号
  14. // var loc = ["川","渝","贵","陕","京","沪","粤","津","赣","湘","鄂"];
  15. var loc = [ "川", "渝"];
  16. //字母,用于生成车牌号
  17. var chars = [ "A", "B", "C", "D"];
  18. //数字,用于生成车牌号
  19. var numbers = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, "J", "K", "L", "M"];
  20. var plate = "";
  21. plate += loc[ Math. floor( Math. random()*loc. length)]; //第一位地区简称
  22. plate += chars[ Math. floor( Math. random()*chars. length)]; //第二位字母简称
  23. //4位数字
  24. for( var i= 0; i< 4; i++){
  25. plate+=numbers[ Math. floor( Math. random()*numbers. length)];
  26. }
  27. return plate;
  28. }
  29. // print(getRandPlate())
  30. //生成名字
  31. function getRandName( ){
  32. var familyNames = [ "赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈",
  33. "褚", "卫", "蒋", "沈", "韩", "杨", "朱", "秦", "尤", "许",
  34. "何", "吕", "施", "张", "孔", "曹", "严", "华", "金", "魏",
  35. "陶", "姜", "戚", "谢", "邹", "喻", "柏", "水", "窦", "章",
  36. "云", "苏", "潘", "葛", "奚", "范", "彭", "郎", "鲁", "韦",
  37. "昌", "马", "苗", "凤", "花", "方", "俞", "任", "袁", "柳",
  38. "刘", "鲍", "史", "唐", "费", "廉", "岑", "薛", "雷", "贺",
  39. "倪", "汤", "滕", "殷", "罗", "毕", "郝", "邬", "安", "常",
  40. "乐", "于", "时", "傅", "皮", "卞", "齐", "董", "伍", "余",
  41. "元", "卜", "顾", "孟", "平", "黄", "和", "谭", "萧", "尹"
  42. ];
  43. var givenNames = [ "子璇", "淼", "国栋", "夫子", "瑞堂", "甜", "敏", "尚", "国贤", "贺祥", "晨涛",
  44. "昊轩", "易轩", "益辰", "益帆", "益冉", "瑾春", "瑾昆", "春齐", "杨", "文昊",
  45. "东东", "雄霖", "浩晨", "熙涵", "溶溶", "冰枫", "欣欣", "宜豪", "欣慧", "建政",
  46. "美欣", "淑慧", "文轩", "文杰", "欣源", "忠林", "榕润", "欣汝", "慧嘉", "新建",
  47. "建林", "亦菲", "林", "冰洁", "佳欣", "涵涵", "禹辰", "淳美", "泽惠", "伟洋",
  48. "涵越", "润丽", "翔", "淑华", "晶莹", "凌晶", "苒溪", "雨涵", "嘉怡", "佳毅",
  49. "子辰", "佳琪", "紫轩", "瑞辰", "昕蕊", "萌", "明远", "欣宜", "泽远", "欣怡",
  50. "佳怡", "佳惠", "晨茜", "晨璐", "运昊", "汝鑫", "淑君", "晶滢", "润莎", "榕汕",
  51. "佳钰", "佳玉", "晓庆", "一鸣", "语晨", "添池", "添昊", "雨泽", "雅晗", "雅涵",
  52. "清妍", "诗悦", "嘉乐", "晨涵", "天赫", "玥傲", "佳昊", "天昊", "萌萌", "若萌",
  53. "泽民", "国强", "胜利", "小凡", "碧瑶", "书书", "京雨", "卫东", "小佳", "长江",
  54. "文虹", "义凡", "广晨", "宸滔", "嘉岐", "雅珺", "睿明", "皓轩", "程天", "子酝",
  55. "艾康", "如羽", "冠玉", "子歉", "永昊", "龙华", "兆颜", "奇文", "月昕", "裕锦",
  56. "昂佳", "昊浩", "宇韬", "睿焓", "永译", "鸿彬", "颢霖", "益彬", "虹昊", "飞悦",
  57. "睿珏", "?宵童", "睿鸿", "容冰", "逸濠", "楷岩", "弘义", "海萦", "昊孺", "梓铭",
  58. "生钊", "蓝玺", "晨辕", "宇菡", "砚海", "文揩", "韬瑞", "彦红", "奕韦", "清予",
  59. "宁翼", "冬睿", "锦昌", "烨宁", "昌权", "国研", "德运", "孝清", "佳阳", "凯玮",
  60. "正真", "民云", "昕冶", "力威", "帅欣", "知淳", "烨飞", "兴远", "子墨", "澄欣",
  61. "烨煊", "悦勤", "晨津", "博宏", "育萌", "羽炫", "绍钧", "睿昌", "泓千", "颢炜",
  62. "虹金", "筠航", "元甲", "星明", "景涛", "铭虹", "德本", "向辉", "基翔", "家易",
  63. "欣鹏", "羽荃", "泽容", "弘亮", "尚廷", "轩梓", "甫津", "彬楷", "寅飞", "愉君",
  64. "阳平", "誉杰", "钦昭", "蕴藉", "羽程", "宏海", "涵畅", "光浩", "令沂", "浩浩",
  65. "睿锦", "易泽", "俊康", "家文", "晨元", "语洋", "裕宏", "梓榛", "阳嘉", "恒展",
  66. "雨远", "哲伊", "逸江", "丰源", "学东", "奇岩", "浩财", "和蔼", "红言", "瑞赫",
  67. "森圆", "欣赢", "梓鸿", "博明", "铭育", "颢硕", "宇烯", "宇如", "淳炎", "源承"
  68. ];
  69. var name = familyNames[ Math. floor( Math. random()*familyNames. length)];
  70. name+=givenNames[ Math. floor( Math. random()*givenNames. length)];
  71. return name;
  72. }
  73. // print(getRandName())
  74. // 生成随机专业
  75. function getRandMajor( ) {
  76. return [ "大数据", "应用数学", "应用统计"][ Math. floor( Math. random()* 3)]
  77. }
  78. // print(getRandMajor())
  79. // 随机取一门课程
  80. function getRandCourse( ) {
  81. var courses = [ "大数据概论", "大数据开发语言", "Hadoop大数据技术",
  82. "分布式数据库原理与应用", "网络操作系统", "结构化数据存储与应用",
  83. "数据导入与预处理应用", "数据可视化技术", "数据仓库与挖掘技术",
  84. "时间序列分析实践", "大数据分析与内存计算"
  85. ];
  86. var credits = [ 2, 4, 4,
  87. 4, 2, 2,
  88. 4, 2, 4,
  89. 4, 4
  90. ];
  91. var idx = Math. floor( Math. random()*courses. length);
  92. var course = [courses[idx],credits[idx]];
  93. return course;
  94. }
  95. // 生成学生数据
  96. function generateStudents( snum=100, sgrade=2020, sharding=true) {
  97. db = db. getSiblingDB( "cqust");
  98. // use cqust;
  99. if (sharding) {
  100. sh. enableSharding( "cqust");
  101. db. students. createIndex({ sno: "hashed"})
  102. sh. shardCollection( "cqust.students",{ "sno": "hashed"});
  103. }
  104. for( var i= 0; i<snum/ 100; i++){
  105. var StudentsDoc = new Array();
  106. for( var j= 0; j< 100; j++){
  107. var sid = sgrade* 1e6+i* 100+j;
  108. var sname = getRandName();
  109. var gender = Math. floor( Math. random()* 2);
  110. var phone = getRandPhone();
  111. var plate = getRandPlate();
  112. var major = getRandMajor();
  113. var height = Math. floor( Math. random()* 30+gender* 5+ 150);
  114. var weight = Math. floor( Math. random()* 30+gender* 20+ 40);
  115. var sclass = Math. floor( Math. random()* 4+ 1);
  116. var courses = new Array();
  117. for( var k= 0; k< Math. floor( Math. random()* 5);k++){
  118. var course_credit = getRandCourse();
  119. courses[k] = { "course":course_credit[ 0], "credit":course_credit[ 1], "score": Math. floor( Math. random()* 50)+ 50};
  120. }
  121. StudentsDoc[j] = {
  122. "sno":sid,
  123. "name":sname,
  124. "gender":gender,
  125. // "height":height,
  126. // "weight":weight,
  127. "body":{
  128. "height":height,
  129. "weight":weight
  130. },
  131. "list":[ Math. ceil( Math. random()* 6), Math. ceil( Math. random()* 6), Math. ceil( Math. random()* 6)],
  132. "phone":phone,
  133. "role": "student",
  134. "major": major,
  135. "grade": sgrade,
  136. "class": sclass,
  137. "courses":courses
  138. };
  139. }
  140. db. getCollection( "students"). insertMany( StudentsDoc);
  141. // db.students.insertMany(StudentsDoc)
  142. }
  143. }
  144. // 主程序从这里开始
  145. generateStudents( 10000, 2018, true);
  146. generateStudents( 10000, 2019, true);
  147. generateStudents( 10000, 2020, true);
  148. generateStudents( 10000, 2021, true);

 运行一下

 没问题欸嘿

题目三

运行"GenerateStudents.js"Mongoshell脚本,将数据导入分片集合中,并查看

(1)分片集状态

(2)分片数据库的统计信息

(3)分片集合数据分布情况

步骤:

其实我们在第二部已经导入数据了,就其实咱们直接查看就行,如果你的数据没有分片,可能数据量太少,你再导两次(别导多了),然后导完等会,他自己会分片过去

1.分片集状态

 输入命令:

sh.status()

 2.分片数据库的统计信息

输入命令:

db.stats()

你看见ch1和ch0服务器上都有数据了,那么你的数据就已经分片了

 3.分片集合数据分布情况

输入命令:

 db.students.getShardDistribution()

本次实验就酱紫了,点个赞吧


转载:https://blog.csdn.net/qq_55977554/article/details/128036228
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场