Skip to content

Instantly share code, notes, and snippets.

@RobertWSaunders
Created October 5, 2018 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobertWSaunders/ca15140b95090e3721f6906588dacbc2 to your computer and use it in GitHub Desktop.
Save RobertWSaunders/ca15140b95090e3721f6906588dacbc2 to your computer and use it in GitHub Desktop.
#!bin/bash
# Assignment #1 NoSQL Databases Part 6
# Class: CMPE 432 Advanced Databases
# Authors: Robert Saunders, Chris Jones
# Student Numebers: 10194030, 10177155
# Script to create a MongoDB replica set with two MongoDB instances.
# This script will also create a database and import the movie and ratings data provided in dataset 2.
# This script requires mongo to be installed and mongod to be installed within your PATH.
# For the sake of the assignment we are running this on our local machine which ensures same machine specs.
# Please accept the echo commands as comments to describe our code.
echo "Killing all mongod instances!"
killall mongod
echo "Waiting 5 seconds for mongod instances to shut down!"
sleep 5
# NOTE: For the next three steps the path to the data files is dependant on where you store your mongo data locally.
echo "Removing existing data files for Mongo instances!"
rm -rf ~/mongo-data/cmpe432-data/replicaset-data
echo "Making new directory for data files of replica set instances!"
mkdir -p ~/mongo-data/cmpe432-data/replicaset-data/rs0 ~/mongo-data/cmpe432-data/replicaset-data/rs1 ~/mongo-data/cmpe432-data/replicaset-data/rs2
echo "Starting up instances of the replica set!"
mongod --replSet cmpe432ReplicaSet --logpath "r0.log" --dbpath ~/mongo-data/cmpe432-data/replicaset-data/rs0 --port 37017 --fork
mongod --replSet cmpe432ReplicaSet --logpath "r1.log" --dbpath ~/mongo-data/cmpe432-data/replicaset-data/rs1 --port 37018 --fork
mongod --replSet cmpe432ReplicaSet --logpath "r2.log" --dbpath ~/mongo-data/cmpe432-data/replicaset-data/rs2 --port 37019 --fork
echo "Wating 5 seconds to ensure instances fully boot up!"
sleep 5
echo "Connecting to one of the instances and initiating the replica set!"
mongo --port 37017 << 'EOF'
config = { _id: "cmpe432ReplicaSet", members:[
{ _id : 0, host : "localhost:37017" },
{ _id : 1, host : "localhost:37018" },
{ _id : 2, host : "localhost:37019" }
]};
rs.initiate(config);
EOF
echo "Waiting 20 seconds to ensure the replica set its members are fully setup and configured!"
sleep 20
echo "Replica set successfully setup!"
echo "Dropping cmpe432-database database from replica set incase this script has already been run before!"
mongo mongodb://localhost:37017,locahost:37018,locahost:37019/cmpe432-database?replicaSet=cmpe432ReplicaSet --eval "db.dropDatabase();"
# NOTE: You can comment out the below two steps if you dataset is already comma seperated.
# NOTE: This relies on you having a dataset2 directory with the data files whereever you are excuting this script.
echo "Converting ratings data to tab seperated for mongoimport tool!"
sed -i -e 's/::/ /g' dataset2/ratings.txt
echo "Converting movies data to tab seperated for mongoimport tool!"
sed -i -e 's/::/ /g' dataset2/movies.txt
# NOTE: You can comment out the below two steps if your dataset if already imported into the database.
echo "Importing the ratings data into a ratings collection in a new database called cmpe432-database!"
mongoimport --uri mongodb://localhost:37017,locahost:37018,locahost:37019/cmpe432-database?replicaSet=cmpe432ReplicaSet --collection "ratings" --columnsHaveTypes --fields "userId.int64(), movieId.int64(), rating.double(), timestamp.string()" --type tsv --file dataset2/ratings.txt
echo "Importing the movies data into a movies collection into the new database called cmpe432-database!"
mongoimport --uri mongodb://localhost:37017,locahost:37018,locahost:37019/cmpe432-database?replicaSet=cmpe432ReplicaSet --collection "movies" --columnsHaveTypes --fields "movieId.int64(), title.string(), genres.string()" --type tsv --file dataset2/movies.txt
echo "Replica set fully configured and all data has been imported!"
echo "Connecting to mongo shell of replica set!"
mongo mongodb://localhost:37017,locahost:37018,locahost:37019/cmpe432-database?replicaSet=cmpe432ReplicaSet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment