目录
前言
本实验在一台电脑上完成,本人自己方法与标准方法略有差异,大家仁者见仁,同时可能你按照一样的步骤下来,会出现各种各样的错误,但希望大家懂得学会去查看日志,查看代码,懂得如何解决问题,这是一个有趣的过程,共勉。
题目一
根据下面的规划表搭建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=27026dbpath=D:\MongoDB\ch\cfg0\cfg0_0\db
logpath=D:\MongoDB\ch\cfg0\cfg0_0\cfg0_0.log
logappend=truereplSet=cfg0
configsvr=true
然后其他的服务类似配置,这里不细说了,图片展示一下
cfg0_1配置
bind_ip=127.0.0.1
port=27027dbpath=D:\MongoDB\ch\cfg0\cfg0_1\db
logpath=D:\MongoDB\ch\cfg0\cfg0_1\cfg0_1.log
logappend=truereplSet=cfg0
configsvr=true
cfg0_2配置
bind_ip=127.0.0.1
port=27028dbpath=D:\MongoDB\ch\cfg0\cfg0_2\db
logpath=D:\MongoDB\ch\cfg0\cfg0_2\cfg0_2.log
logappend=truereplSet=cfg0
configsvr=true
ch0_0配置
bind_ip=127.0.0.1
port=27020dbpath=D:\MongoDB\ch\ch0\ch0_0\db
logpath=D:\MongoDB\ch\ch0\ch0_0\ch0_0.log
logappend=truereplSet=ch0
shardsvr=true
ch0_1配置
bind_ip=127.0.0.1
port=27021dbpath=D:\MongoDB\ch\ch0\ch0_1\db
logpath=D:\MongoDB\ch\ch0\ch0_1\ch0_1.log
logappend=truereplSet=ch0
shardsvr=true
ch0_2配置
bind_ip=127.0.0.1
port=27022dbpath=D:\MongoDB\ch\ch0\ch0_2\db
logpath=D:\MongoDB\ch\ch0\ch0_2\ch0_2.log
logappend=truereplSet=ch0
shardsvr=true
ch1_0配置
bind_ip=127.0.0.1
port=27023dbpath=D:\MongoDB\ch\ch1\ch1_0\db
logpath=D:\MongoDB\ch\ch1\ch1_0\ch1_0.log
logappend=truereplSet=ch1
shardsvr=true
ch1_1配置
bind_ip=127.0.0.1
port=27024dbpath=D:\MongoDB\ch\ch1\ch1_1\db
logpath=D:\MongoDB\ch\ch1\ch1_1\ch1_1.log
logappend=truereplSet=ch1
shardsvr=true
ch1_2配置
bind_ip=127.0.0.1
port=27025dbpath=D:\MongoDB\ch\ch1\ch1_2\db
logpath=D:\MongoDB\ch\ch1\ch1_2\ch1_2.log
logappend=truereplSet=ch1
shardsvr=true
2.初始化副本集
即初始化两个分片服务器(ch0,ch1)和一个配置服务器(cfg0)
这里我分布在ch0,ch1,cfg0下创建了一个js文件(可自己随便在某个位置创建这三个文件)
cfgoconf.js文件配置
-
var rsconf={
-
_id:
"cfg0",
-
"members":[
-
{
_id:
0,
host:
"127.0.0.1:27026",
priority:
2},
-
{
_id:
1,
host:
"127.0.0.1:27027",
priority:
1},
-
{
_id:
2,
host:
"127.0.0.1:27028",
priority:
1}
-
]};
-
-
rs.
initiate(rsconf)
ch0conf.js配置
-
var rsconf={
-
_id:
"ch0",
-
"members":[
-
{
_id:
0,
host:
"127.0.0.1:27020",
priority:
2},
-
{
_id:
1,
host:
"127.0.0.1:27021",
priority:
1},
-
{
_id:
2,
host:
"127.0.0.1:27022",
priority:
0,
arbiterOnly:
true}
-
]};
-
-
rs.
initiate(rsconf)
ch1conf.js配置
-
var rsconf={
-
_id:
"ch1",
-
"members":[
-
{
"_id":
0,
"host":
"127.0.0.1:27023",
priority:
2},
-
{
"_id":
1,
"host":
"127.0.0.1:27024",
priority:
1},
-
{
"_id":
2,
"host":
"127.0.0.1:27025",
priority:
0,
arbiterOnly:
true}
-
]};
-
-
rs.
initiate(rsconf)
3.创建路由节点(分片管理服务器)Mongos
我用了一个文件夹来保存这个服务器,如下图
然后在该文件夹下编写其配置文件ini
bind_ip=0.0.0.0
port=27017logpath=D:\MongoDB\ch\router0\mongos.log
logappend=trueconfigdb=cfg0/127.0.0.1:27026,127.0.0.1:27027,127.0.0.1:27028
4.添加分片集合
我在router0下创建了一个js文件,其配置参数如下
-
sh.
addShard(
"ch0/127.0.0.1:27020,127.0.0.1:27021,127.0.0.1:27022");
-
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.exeREM 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.iniREM 4. 编写副本集初始化脚本for分片集和配置集
REM 手动完成 rs0conf.js, rs1conf.js, cfg0conf.jsREM 等待5秒确保所有服务器都启动了
TIMEOUT /T 5REM 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.jsREM 等待11秒确保副本集完成选举
TIMEOUT /T 11REM 6. 启动路由节点/分片服务器 mongos
start "router0:27017" %MONGOS_CMD% --config=D:\MongoDB\ch\router0\ruoter0.iniREM 等待2秒确保路由节点完成启动
TIMEOUT /T 10REM 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文件如下
-
//生成随机手机号
-
function
getRandPhone(
){
-
var heads = [
"134",
"138",
"139",
"150",
"151",
"152",
"157",
"158",
"159",
"170",
"189"];
-
var phone = heads[
Math.
floor(
Math.
random()*heads.
length)];
-
var numbers = [
1,
2,
3,
4,
5,
6,
7,
8,
9];
-
for(
var i=
0; i<
8; i++){
-
phone+=numbers[
Math.
floor(
Math.
random()*numbers.
length)];
-
}
-
return phone;
-
}
-
-
//生成车牌号
-
function
getRandPlate(
){
-
//地区,用于生成车牌号
-
// var loc = ["川","渝","贵","陕","京","沪","粤","津","赣","湘","鄂"];
-
var loc = [
"川",
"渝"];
-
//字母,用于生成车牌号
-
var chars = [
"A",
"B",
"C",
"D"];
-
//数字,用于生成车牌号
-
var numbers = [
1,
2,
3,
4,
5,
6,
7,
8,
9,
1,
2,
3,
4,
5,
6,
7,
8,
9,
"J",
"K",
"L",
"M"];
-
var plate =
"";
-
plate += loc[
Math.
floor(
Math.
random()*loc.
length)];
//第一位地区简称
-
plate += chars[
Math.
floor(
Math.
random()*chars.
length)];
//第二位字母简称
-
//4位数字
-
for(
var i=
0; i<
4; i++){
-
plate+=numbers[
Math.
floor(
Math.
random()*numbers.
length)];
-
}
-
return plate;
-
}
-
// print(getRandPlate())
-
-
//生成名字
-
function
getRandName(
){
-
var familyNames = [
"赵",
"钱",
"孙",
"李",
"周",
"吴",
"郑",
"王",
"冯",
"陈",
-
"褚",
"卫",
"蒋",
"沈",
"韩",
"杨",
"朱",
"秦",
"尤",
"许",
-
"何",
"吕",
"施",
"张",
"孔",
"曹",
"严",
"华",
"金",
"魏",
-
"陶",
"姜",
"戚",
"谢",
"邹",
"喻",
"柏",
"水",
"窦",
"章",
-
"云",
"苏",
"潘",
"葛",
"奚",
"范",
"彭",
"郎",
"鲁",
"韦",
-
"昌",
"马",
"苗",
"凤",
"花",
"方",
"俞",
"任",
"袁",
"柳",
-
"刘",
"鲍",
"史",
"唐",
"费",
"廉",
"岑",
"薛",
"雷",
"贺",
-
"倪",
"汤",
"滕",
"殷",
"罗",
"毕",
"郝",
"邬",
"安",
"常",
-
"乐",
"于",
"时",
"傅",
"皮",
"卞",
"齐",
"董",
"伍",
"余",
-
"元",
"卜",
"顾",
"孟",
"平",
"黄",
"和",
"谭",
"萧",
"尹"
-
];
-
var givenNames = [
"子璇",
"淼",
"国栋",
"夫子",
"瑞堂",
"甜",
"敏",
"尚",
"国贤",
"贺祥",
"晨涛",
-
"昊轩",
"易轩",
"益辰",
"益帆",
"益冉",
"瑾春",
"瑾昆",
"春齐",
"杨",
"文昊",
-
"东东",
"雄霖",
"浩晨",
"熙涵",
"溶溶",
"冰枫",
"欣欣",
"宜豪",
"欣慧",
"建政",
-
"美欣",
"淑慧",
"文轩",
"文杰",
"欣源",
"忠林",
"榕润",
"欣汝",
"慧嘉",
"新建",
-
"建林",
"亦菲",
"林",
"冰洁",
"佳欣",
"涵涵",
"禹辰",
"淳美",
"泽惠",
"伟洋",
-
"涵越",
"润丽",
"翔",
"淑华",
"晶莹",
"凌晶",
"苒溪",
"雨涵",
"嘉怡",
"佳毅",
-
"子辰",
"佳琪",
"紫轩",
"瑞辰",
"昕蕊",
"萌",
"明远",
"欣宜",
"泽远",
"欣怡",
-
"佳怡",
"佳惠",
"晨茜",
"晨璐",
"运昊",
"汝鑫",
"淑君",
"晶滢",
"润莎",
"榕汕",
-
"佳钰",
"佳玉",
"晓庆",
"一鸣",
"语晨",
"添池",
"添昊",
"雨泽",
"雅晗",
"雅涵",
-
"清妍",
"诗悦",
"嘉乐",
"晨涵",
"天赫",
"玥傲",
"佳昊",
"天昊",
"萌萌",
"若萌",
-
"泽民",
"国强",
"胜利",
"小凡",
"碧瑶",
"书书",
"京雨",
"卫东",
"小佳",
"长江",
-
"文虹",
"义凡",
"广晨",
"宸滔",
"嘉岐",
"雅珺",
"睿明",
"皓轩",
"程天",
"子酝",
-
"艾康",
"如羽",
"冠玉",
"子歉",
"永昊",
"龙华",
"兆颜",
"奇文",
"月昕",
"裕锦",
-
"昂佳",
"昊浩",
"宇韬",
"睿焓",
"永译",
"鸿彬",
"颢霖",
"益彬",
"虹昊",
"飞悦",
-
"睿珏",
"?宵童",
"睿鸿",
"容冰",
"逸濠",
"楷岩",
"弘义",
"海萦",
"昊孺",
"梓铭",
-
"生钊",
"蓝玺",
"晨辕",
"宇菡",
"砚海",
"文揩",
"韬瑞",
"彦红",
"奕韦",
"清予",
-
"宁翼",
"冬睿",
"锦昌",
"烨宁",
"昌权",
"国研",
"德运",
"孝清",
"佳阳",
"凯玮",
-
"正真",
"民云",
"昕冶",
"力威",
"帅欣",
"知淳",
"烨飞",
"兴远",
"子墨",
"澄欣",
-
"烨煊",
"悦勤",
"晨津",
"博宏",
"育萌",
"羽炫",
"绍钧",
"睿昌",
"泓千",
"颢炜",
-
"虹金",
"筠航",
"元甲",
"星明",
"景涛",
"铭虹",
"德本",
"向辉",
"基翔",
"家易",
-
"欣鹏",
"羽荃",
"泽容",
"弘亮",
"尚廷",
"轩梓",
"甫津",
"彬楷",
"寅飞",
"愉君",
-
"阳平",
"誉杰",
"钦昭",
"蕴藉",
"羽程",
"宏海",
"涵畅",
"光浩",
"令沂",
"浩浩",
-
"睿锦",
"易泽",
"俊康",
"家文",
"晨元",
"语洋",
"裕宏",
"梓榛",
"阳嘉",
"恒展",
-
"雨远",
"哲伊",
"逸江",
"丰源",
"学东",
"奇岩",
"浩财",
"和蔼",
"红言",
"瑞赫",
-
"森圆",
"欣赢",
"梓鸿",
"博明",
"铭育",
"颢硕",
"宇烯",
"宇如",
"淳炎",
"源承"
-
];
-
var name = familyNames[
Math.
floor(
Math.
random()*familyNames.
length)];
-
name+=givenNames[
Math.
floor(
Math.
random()*givenNames.
length)];
-
return name;
-
}
-
// print(getRandName())
-
-
// 生成随机专业
-
function
getRandMajor(
) {
-
return [
"大数据",
"应用数学",
"应用统计"][
Math.
floor(
Math.
random()*
3)]
-
-
}
-
// print(getRandMajor())
-
-
// 随机取一门课程
-
function
getRandCourse(
) {
-
var courses = [
"大数据概论",
"大数据开发语言",
"Hadoop大数据技术",
-
"分布式数据库原理与应用",
"网络操作系统",
"结构化数据存储与应用",
-
"数据导入与预处理应用",
"数据可视化技术",
"数据仓库与挖掘技术",
-
"时间序列分析实践",
"大数据分析与内存计算"
-
];
-
var credits = [
2,
4,
4,
-
4,
2,
2,
-
4,
2,
4,
-
4,
4
-
];
-
var idx =
Math.
floor(
Math.
random()*courses.
length);
-
var course = [courses[idx],credits[idx]];
-
return course;
-
}
-
-
// 生成学生数据
-
function
generateStudents(
snum=100, sgrade=2020, overwrite=true, sharding=true) {
-
db = db.
getSiblingDB(
"cqust");
-
// use cqust;
-
if (sharding) {
-
sh.
enableSharding(
"cqust");
-
}
-
-
if (overwrite) {
-
//db.getCollection("students").drop();
-
// db.students.drop() 删除students集合
-
if (sharding){
-
sh.
shardCollection(
"cqust.students",{
"sno":
"hashed"});
-
}
-
}
-
-
for(
var i=
0; i<snum/
100; i++){
-
var
StudentsDoc =
new
Array();
-
for(
var j=
0; j<
100; j++){
-
var sid = sgrade*
1e6+i*
100+j;
-
var sname =
getRandName();
-
var gender =
Math.
floor(
Math.
random()*
2);
-
var phone =
getRandPhone();
-
var plate =
getRandPlate();
-
var major =
getRandMajor();
-
var height =
Math.
floor(
Math.
random()*
30+gender*
5+
150);
-
var weight =
Math.
floor(
Math.
random()*
30+gender*
20+
40);
-
var sclass =
Math.
floor(
Math.
random()*
4+
1);
-
var courses =
new
Array();
-
for(
var k=
0; k<
Math.
floor(
Math.
random()*
5);k++){
-
var course_credit =
getRandCourse();
-
courses[k] = {
"course":course_credit[
0],
"credit":course_credit[
1],
"score":
Math.
floor(
Math.
random()*
50)+
50};
-
}
-
-
StudentsDoc[j] = {
-
"sno":sid,
-
"name":sname,
-
"gender":gender,
-
// "height":height,
-
// "weight":weight,
-
"body":{
-
"height":height,
-
"weight":weight
-
},
-
"list":[
Math.
ceil(
Math.
random()*
6),
Math.
ceil(
Math.
random()*
6),
Math.
ceil(
Math.
random()*
6)],
-
"phone":phone,
-
"role":
"student",
-
"major": major,
-
"grade": sgrade,
-
"class": sclass,
-
"courses":courses
-
};
-
}
-
db.
getCollection(
"students").
insertMany(
StudentsDoc);
-
// db.students.insertMany(StudentsDoc)
-
}
-
}
-
-
// 主程序从这里开始
-
generateStudents(
1000,
2018,
true,
false);
-
generateStudents(
1000,
2019,
false,
false);
-
generateStudents(
1000,
2020,
false,
false);
-
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代码
-
//生成随机手机号
-
function
getRandPhone(
){
-
var heads = [
"134",
"138",
"139",
"150",
"151",
"152",
"157",
"158",
"159",
"170",
"189"];
-
var phone = heads[
Math.
floor(
Math.
random()*heads.
length)];
-
var numbers = [
1,
2,
3,
4,
5,
6,
7,
8,
9];
-
for(
var i=
0; i<
8; i++){
-
phone+=numbers[
Math.
floor(
Math.
random()*numbers.
length)];
-
}
-
return phone;
-
}
-
-
//生成车牌号
-
function
getRandPlate(
){
-
//地区,用于生成车牌号
-
// var loc = ["川","渝","贵","陕","京","沪","粤","津","赣","湘","鄂"];
-
var loc = [
"川",
"渝"];
-
//字母,用于生成车牌号
-
var chars = [
"A",
"B",
"C",
"D"];
-
//数字,用于生成车牌号
-
var numbers = [
1,
2,
3,
4,
5,
6,
7,
8,
9,
1,
2,
3,
4,
5,
6,
7,
8,
9,
"J",
"K",
"L",
"M"];
-
var plate =
"";
-
plate += loc[
Math.
floor(
Math.
random()*loc.
length)];
//第一位地区简称
-
plate += chars[
Math.
floor(
Math.
random()*chars.
length)];
//第二位字母简称
-
//4位数字
-
for(
var i=
0; i<
4; i++){
-
plate+=numbers[
Math.
floor(
Math.
random()*numbers.
length)];
-
}
-
return plate;
-
}
-
// print(getRandPlate())
-
-
//生成名字
-
function
getRandName(
){
-
var familyNames = [
"赵",
"钱",
"孙",
"李",
"周",
"吴",
"郑",
"王",
"冯",
"陈",
-
"褚",
"卫",
"蒋",
"沈",
"韩",
"杨",
"朱",
"秦",
"尤",
"许",
-
"何",
"吕",
"施",
"张",
"孔",
"曹",
"严",
"华",
"金",
"魏",
-
"陶",
"姜",
"戚",
"谢",
"邹",
"喻",
"柏",
"水",
"窦",
"章",
-
"云",
"苏",
"潘",
"葛",
"奚",
"范",
"彭",
"郎",
"鲁",
"韦",
-
"昌",
"马",
"苗",
"凤",
"花",
"方",
"俞",
"任",
"袁",
"柳",
-
"刘",
"鲍",
"史",
"唐",
"费",
"廉",
"岑",
"薛",
"雷",
"贺",
-
"倪",
"汤",
"滕",
"殷",
"罗",
"毕",
"郝",
"邬",
"安",
"常",
-
"乐",
"于",
"时",
"傅",
"皮",
"卞",
"齐",
"董",
"伍",
"余",
-
"元",
"卜",
"顾",
"孟",
"平",
"黄",
"和",
"谭",
"萧",
"尹"
-
];
-
var givenNames = [
"子璇",
"淼",
"国栋",
"夫子",
"瑞堂",
"甜",
"敏",
"尚",
"国贤",
"贺祥",
"晨涛",
-
"昊轩",
"易轩",
"益辰",
"益帆",
"益冉",
"瑾春",
"瑾昆",
"春齐",
"杨",
"文昊",
-
"东东",
"雄霖",
"浩晨",
"熙涵",
"溶溶",
"冰枫",
"欣欣",
"宜豪",
"欣慧",
"建政",
-
"美欣",
"淑慧",
"文轩",
"文杰",
"欣源",
"忠林",
"榕润",
"欣汝",
"慧嘉",
"新建",
-
"建林",
"亦菲",
"林",
"冰洁",
"佳欣",
"涵涵",
"禹辰",
"淳美",
"泽惠",
"伟洋",
-
"涵越",
"润丽",
"翔",
"淑华",
"晶莹",
"凌晶",
"苒溪",
"雨涵",
"嘉怡",
"佳毅",
-
"子辰",
"佳琪",
"紫轩",
"瑞辰",
"昕蕊",
"萌",
"明远",
"欣宜",
"泽远",
"欣怡",
-
"佳怡",
"佳惠",
"晨茜",
"晨璐",
"运昊",
"汝鑫",
"淑君",
"晶滢",
"润莎",
"榕汕",
-
"佳钰",
"佳玉",
"晓庆",
"一鸣",
"语晨",
"添池",
"添昊",
"雨泽",
"雅晗",
"雅涵",
-
"清妍",
"诗悦",
"嘉乐",
"晨涵",
"天赫",
"玥傲",
"佳昊",
"天昊",
"萌萌",
"若萌",
-
"泽民",
"国强",
"胜利",
"小凡",
"碧瑶",
"书书",
"京雨",
"卫东",
"小佳",
"长江",
-
"文虹",
"义凡",
"广晨",
"宸滔",
"嘉岐",
"雅珺",
"睿明",
"皓轩",
"程天",
"子酝",
-
"艾康",
"如羽",
"冠玉",
"子歉",
"永昊",
"龙华",
"兆颜",
"奇文",
"月昕",
"裕锦",
-
"昂佳",
"昊浩",
"宇韬",
"睿焓",
"永译",
"鸿彬",
"颢霖",
"益彬",
"虹昊",
"飞悦",
-
"睿珏",
"?宵童",
"睿鸿",
"容冰",
"逸濠",
"楷岩",
"弘义",
"海萦",
"昊孺",
"梓铭",
-
"生钊",
"蓝玺",
"晨辕",
"宇菡",
"砚海",
"文揩",
"韬瑞",
"彦红",
"奕韦",
"清予",
-
"宁翼",
"冬睿",
"锦昌",
"烨宁",
"昌权",
"国研",
"德运",
"孝清",
"佳阳",
"凯玮",
-
"正真",
"民云",
"昕冶",
"力威",
"帅欣",
"知淳",
"烨飞",
"兴远",
"子墨",
"澄欣",
-
"烨煊",
"悦勤",
"晨津",
"博宏",
"育萌",
"羽炫",
"绍钧",
"睿昌",
"泓千",
"颢炜",
-
"虹金",
"筠航",
"元甲",
"星明",
"景涛",
"铭虹",
"德本",
"向辉",
"基翔",
"家易",
-
"欣鹏",
"羽荃",
"泽容",
"弘亮",
"尚廷",
"轩梓",
"甫津",
"彬楷",
"寅飞",
"愉君",
-
"阳平",
"誉杰",
"钦昭",
"蕴藉",
"羽程",
"宏海",
"涵畅",
"光浩",
"令沂",
"浩浩",
-
"睿锦",
"易泽",
"俊康",
"家文",
"晨元",
"语洋",
"裕宏",
"梓榛",
"阳嘉",
"恒展",
-
"雨远",
"哲伊",
"逸江",
"丰源",
"学东",
"奇岩",
"浩财",
"和蔼",
"红言",
"瑞赫",
-
"森圆",
"欣赢",
"梓鸿",
"博明",
"铭育",
"颢硕",
"宇烯",
"宇如",
"淳炎",
"源承"
-
];
-
var name = familyNames[
Math.
floor(
Math.
random()*familyNames.
length)];
-
name+=givenNames[
Math.
floor(
Math.
random()*givenNames.
length)];
-
return name;
-
}
-
// print(getRandName())
-
-
// 生成随机专业
-
function
getRandMajor(
) {
-
return [
"大数据",
"应用数学",
"应用统计"][
Math.
floor(
Math.
random()*
3)]
-
-
}
-
// print(getRandMajor())
-
-
// 随机取一门课程
-
function
getRandCourse(
) {
-
var courses = [
"大数据概论",
"大数据开发语言",
"Hadoop大数据技术",
-
"分布式数据库原理与应用",
"网络操作系统",
"结构化数据存储与应用",
-
"数据导入与预处理应用",
"数据可视化技术",
"数据仓库与挖掘技术",
-
"时间序列分析实践",
"大数据分析与内存计算"
-
];
-
var credits = [
2,
4,
4,
-
4,
2,
2,
-
4,
2,
4,
-
4,
4
-
];
-
var idx =
Math.
floor(
Math.
random()*courses.
length);
-
var course = [courses[idx],credits[idx]];
-
return course;
-
}
-
-
// 生成学生数据
-
function
generateStudents(
snum=100, sgrade=2020, sharding=true) {
-
db = db.
getSiblingDB(
"cqust");
-
// use cqust;
-
if (sharding) {
-
sh.
enableSharding(
"cqust");
-
db.
students.
createIndex({
sno:
"hashed"})
-
sh.
shardCollection(
"cqust.students",{
"sno":
"hashed"});
-
}
-
-
for(
var i=
0; i<snum/
100; i++){
-
var
StudentsDoc =
new
Array();
-
for(
var j=
0; j<
100; j++){
-
var sid = sgrade*
1e6+i*
100+j;
-
var sname =
getRandName();
-
var gender =
Math.
floor(
Math.
random()*
2);
-
var phone =
getRandPhone();
-
var plate =
getRandPlate();
-
var major =
getRandMajor();
-
var height =
Math.
floor(
Math.
random()*
30+gender*
5+
150);
-
var weight =
Math.
floor(
Math.
random()*
30+gender*
20+
40);
-
var sclass =
Math.
floor(
Math.
random()*
4+
1);
-
var courses =
new
Array();
-
for(
var k=
0; k<
Math.
floor(
Math.
random()*
5);k++){
-
var course_credit =
getRandCourse();
-
courses[k] = {
"course":course_credit[
0],
"credit":course_credit[
1],
"score":
Math.
floor(
Math.
random()*
50)+
50};
-
}
-
-
StudentsDoc[j] = {
-
"sno":sid,
-
"name":sname,
-
"gender":gender,
-
// "height":height,
-
// "weight":weight,
-
"body":{
-
"height":height,
-
"weight":weight
-
},
-
"list":[
Math.
ceil(
Math.
random()*
6),
Math.
ceil(
Math.
random()*
6),
Math.
ceil(
Math.
random()*
6)],
-
"phone":phone,
-
"role":
"student",
-
"major": major,
-
"grade": sgrade,
-
"class": sclass,
-
"courses":courses
-
};
-
}
-
db.
getCollection(
"students").
insertMany(
StudentsDoc);
-
// db.students.insertMany(StudentsDoc)
-
}
-
}
-
-
// 主程序从这里开始
-
generateStudents(
10000,
2018,
true);
-
generateStudents(
10000,
2019,
true);
-
generateStudents(
10000,
2020,
true);
-
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