-
Notifications
You must be signed in to change notification settings - Fork 947
/
import.sh
executable file
·157 lines (138 loc) · 4.81 KB
/
import.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env bash
# Default parameters value
DOCKER=0
SMALL=0
URI=mongodb://localhost
usage() {
echo "Usage: ./import.sh [-h|-help] [-d|--docker] [-s|--small] [-u URI|-u=URI|--uri URI|--uri=URI]"
echo
echo "OPTIONS:"
echo -e " -d, --docker\t starts a MongoDB docker container on port 27017 with --rm option."
echo -e " -s, --small\t only imports the smallest datasets for a fast loading."
echo -e " -u, --uri\t MongoDB Cluster URI. Can contain user:password if --auth is activated."
echo -e "\t\t Compatible with \"mongodb://\" and \"mongodb://srv\" connection strings."
echo -e " -h, --help\t prints the help."
echo
echo "EXAMPLES:"
echo " ./import.sh"
echo " ./import.sh -h"
echo " ./import.sh --help"
echo " ./import.sh --docker"
echo " ./import.sh --small"
echo " ./import.sh -s"
echo " ./import.sh -d"
echo " ./import.sh -u=mongodb+srv://user:[email protected]"
echo " ./import.sh --docker --small --uri mongodb://127.0.0.1"
echo " ./import.sh -d -s --uri mongodb://127.0.0.1"
echo " ./import.sh -d -s --uri=mongodb://127.0.0.1"
echo " ./import.sh -ds --uri mongodb://127.0.0.1"
echo " ./import.sh -ds -u mongodb://127.0.0.1"
echo " ./import.sh -ds -u=mongodb://127.0.0.1"
echo " ./import.sh -dsu mongodb://127.0.0.1"
echo " ./import.sh -dsu=mongodb://127.0.0.1"
echo " ./import.sh -sd --uri mongodb://127.0.0.1"
echo " ./import.sh -sd -u mongodb://127.0.0.1"
echo " ./import.sh -sd -u=mongodb://127.0.0.1"
echo " ./import.sh -sdu mongodb://127.0.0.1"
echo " ./import.sh -sdu=mongodb://127.0.0.1"
exit 1
}
echo_error() {
echo -e "\033[0;31m$1\033[0m"
}
argument_parsing() {
while [[ $# -gt 0 ]]; do
case "$1" in
-h | --help)
usage
exit 0
;;
-d | --docker)
DOCKER=1
;;
-s | --small)
SMALL=1
;;
-ds | -sd)
DOCKER=1
SMALL=1
;;
-u | --uri)
shift
URI="$1"
;;
-dsu | -sdu)
shift
DOCKER=1
SMALL=1
URI="$1"
;;
-u=* | --uri=*)
URI="${1#*=}"
;;
-dsu=* | -sdu=*)
DOCKER=1
SMALL=1
URI="${1#*=}"
;;
*)
echo_error "Unknown option '$1'"
usage
;;
esac
shift
done
}
start_mongodb_docker() {
if [[ ${DOCKER} -eq 1 ]]; then
echo "Starting MongoDB in Docker on port 27017..."
docker run --rm -d -p 27017:27017 --name mongo mongo:latest
sleep 3
echo "Done."
fi
}
import_small_datasets() {
cd datasets
unzip tweets.zip
mongorestore --drop --uri ${URI}
rm -rf dump
wget -qO- http://media.mongodb.org/zips.json | mongoimport --drop -c zips --uri ${URI}/samples
unzip palbum.zip
mongoimport --drop -c images --uri ${URI}/sample_pictures palbum/images.json
mongoimport --drop -c albums --uri ${URI}/sample_pictures palbum/albums.json
rm -rf palbum
mongoimport --drop -c grades --uri ${URI}/sample_school grades.json
mongoimport --drop -c students --uri ${URI}/sample_school students.json
mongoimport --drop -c profiles --uri ${URI}/samples profiles.json
mongoimport --drop -c products --uri ${URI}/samples products.json
mongoimport --drop -c countries-small --uri ${URI}/samples countries-small.json
mongoimport --drop -c countries-big --uri ${URI}/samples countries-big.json
mongoimport --drop -c restaurants --uri ${URI}/samples restaurant.json
mongoimport --drop -c covers --uri ${URI}/sample_library covers.json
mongoimport --drop -c books --uri ${URI}/sample_library books.json
cd ..
}
import_big_datasets() {
if [[ ${SMALL} -eq 0 ]]; then
cd datasets
unzip people-bson.zip -d dump
mongorestore --gzip --drop --uri ${URI}
rm -rf dump
mongoimport --drop -c city_inspections --uri ${URI}/samples city_inspections.json
mongoimport --drop -c companies --uri ${URI}/samples companies.json
wget https://dl.dropbox.com/s/p75zp1karqg6nnn/stocks.zip
unzip stocks.zip
mongorestore --drop --uri ${URI}
rm -rf dump stocks.zip
wget -qO- https://dl.dropbox.com/s/gxbsj271j5pevec/trades.json | mongoimport --drop -c trades --uri ${URI}/samples
wget https://dl.dropbox.com/s/nfnvx6pggmvw5vt/enron.zip
unrar x enron.zip
mongorestore --drop --uri ${URI}
rm -rf dump enron.zip
cd ..
fi
}
argument_parsing $@
start_mongodb_docker
import_small_datasets
import_big_datasets