Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
M
MongoShard
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
diaa.hanna
MongoShard
Commits
fa47f6e6
Commit
fa47f6e6
authored
Apr 21, 2026
by
drnull03
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Finished the porject
parent
7fb829ee
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
220 additions
and
0 deletions
+220
-0
.gitignore
.gitignore
+1
-0
MongoShard.pdf
Report/MongoShard.pdf
+0
-0
commands.sh
commands.sh
+58
-0
settings.json
movie-shard-app/.vscode/settings.json
+3
-0
pom.xml
movie-shard-app/pom.xml
+31
-0
Application.java
movie-shard-app/src/main/java/sy/edu/hiast/Application.java
+21
-0
Movie.java
movie-shard-app/src/main/java/sy/edu/hiast/model/Movie.java
+22
-0
User.java
movie-shard-app/src/main/java/sy/edu/hiast/model/User.java
+19
-0
DataGeneratorService.java
.../main/java/sy/edu/hiast/service/DataGeneratorService.java
+63
-0
application.properties
movie-shard-app/src/main/resources/application.properties
+2
-0
No files found.
.gitignore
View file @
fa47f6e6
**/target/
data/
Report/MongoShard.pdf
0 → 100644
View file @
fa47f6e6
File added
commands.sh
0 → 100644
View file @
fa47f6e6
# 1 turining on the database
sudo
systemctl start mongod
# 2 creating folder for storing information
mkdir
-p
data/config data/s1n1 data/s1n2 data/s2n1 data/s2n2
# Start Config Server (Replica Set):
mongod
--configsvr
--replSet
cfgrs
--port
27016
--dbpath
./data/config
--bind_ip
localhost
# Connect via mongosh and run: rs.initiate({_id: "cfgrs", members: [{_id: 0, host: "localhost:27016"}]})
# Start Shard 1 (Replica Set - 2 members):
mongod
--shardsvr
--replSet
s1rs
--port
27017
--dbpath
./data/s1n1
--bind_ip
localhost
mongod
--shardsvr
--replSet
s1rs
--port
27018
--dbpath
./data/s1n2
--bind_ip
localhost
# Connect to 27017 and run: rs.initiate({_id: "s1rs", members: [{_id: 0, host: "localhost:27017"}, {_id: 1, host: "localhost:27018"}]})
#Start Shard 2 (Replica Set - 2 members):
mongod
--shardsvr
--replSet
s2rs
--port
27019
--dbpath
./data/s2n1
--bind_ip
localhost
mongod
--shardsvr
--replSet
s2rs
--port
27020
--dbpath
./data/s2n2
--bind_ip
localhost
# Connect to 27019 and run: rs.initiate({_id: "s2rs", members: [{_id: 0, host: "localhost:27019"}, {_id: 1, host: "localhost:27020"}]})
#Start Mongos (Router):
mongos
--configdb
cfgrs/localhost:27016
--port
27021
--bind_ip
localhost
#Configure Sharding in MongoDB
#Connect to the router (mongosh --port 27021) and run:
# Add Shards to the cluster
sh.addShard
(
"s1rs/localhost:27017"
)
;
sh.addShard
(
"s2rs/localhost:27019"
)
;
# Enable sharding for the database
sh.enableSharding
(
"videodb"
)
;
# Shard the Movies collection (Range-based on title)
use videodb
;
db.movies.createIndex
({
title: 1
})
;
sh.shardCollection
(
"videodb.movies"
,
{
title: 1
})
;
# Shard the Users collection (Hashed on _id)
sh.shardCollection
(
"videodb.users"
,
{
_id:
"hashed"
})
;
movie-shard-app/.vscode/settings.json
0 → 100644
View file @
fa47f6e6
{
"snyk.advanced.autoSelectOrganization"
:
true
}
\ No newline at end of file
movie-shard-app/pom.xml
0 → 100644
View file @
fa47f6e6
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<parent>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-parent
</artifactId>
<version>
3.2.2
</version>
</parent>
<groupId>
sy.edu.hiast
</groupId>
<artifactId>
movie-shard-app
</artifactId>
<version>
1.0.0
</version>
<dependencies>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-data-mongodb
</artifactId>
</dependency>
<dependency>
<groupId>
org.apache.commons
</groupId>
<artifactId>
commons-lang3
</artifactId>
<version>
3.12.0
</version>
</dependency>
<dependency>
<groupId>
org.projectlombok
</groupId>
<artifactId>
lombok
</artifactId>
<optional>
true
</optional>
</dependency>
</dependencies>
</project>
movie-shard-app/src/main/java/sy/edu/hiast/Application.java
0 → 100644
View file @
fa47f6e6
package
sy
.
edu
.
hiast
;
import
org.springframework.boot.CommandLineRunner
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.context.annotation.Bean
;
import
sy.edu.hiast.service.DataGeneratorService
;
@SpringBootApplication
public
class
Application
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
Application
.
class
,
args
);
}
@Bean
CommandLineRunner
start
(
DataGeneratorService
service
)
{
return
args
->
{
service
.
runGeneration
();
};
}
}
\ No newline at end of file
movie-shard-app/src/main/java/sy/edu/hiast/model/Movie.java
0 → 100644
View file @
fa47f6e6
package
sy
.
edu
.
hiast
.
model
;
import
org.springframework.data.annotation.Id
;
import
org.springframework.data.mongodb.core.mapping.Document
;
import
java.util.List
;
@Document
(
collection
=
"movies"
)
public
class
Movie
{
@Id
private
String
id
;
private
String
title
;
private
List
<
String
>
directors
;
private
float
rating
;
private
List
<
String
>
cast
;
// Standard Getters/Setters or use @Data if using Lombok
public
String
getTitle
()
{
return
title
;
}
public
void
setTitle
(
String
title
)
{
this
.
title
=
title
;
}
public
void
setDirectors
(
List
<
String
>
directors
)
{
this
.
directors
=
directors
;
}
public
void
setRating
(
float
rating
)
{
this
.
rating
=
rating
;
}
public
void
setCast
(
List
<
String
>
cast
)
{
this
.
cast
=
cast
;
}
}
movie-shard-app/src/main/java/sy/edu/hiast/model/User.java
0 → 100644
View file @
fa47f6e6
package
sy
.
edu
.
hiast
.
model
;
import
org.springframework.data.annotation.Id
;
import
org.springframework.data.mongodb.core.mapping.Document
;
import
java.util.List
;
@Document
(
collection
=
"users"
)
public
class
User
{
@Id
private
String
id
;
private
String
user_name
;
private
List
<
String
>
watched_movies
;
private
int
subscription_month
;
// Getters/Setters
public
void
setUser_name
(
String
user_name
)
{
this
.
user_name
=
user_name
;
}
public
void
setWatched_movies
(
List
<
String
>
watched_movies
)
{
this
.
watched_movies
=
watched_movies
;
}
public
void
setSubscription_month
(
int
subscription_month
)
{
this
.
subscription_month
=
subscription_month
;
}
}
movie-shard-app/src/main/java/sy/edu/hiast/service/DataGeneratorService.java
0 → 100644
View file @
fa47f6e6
package
sy
.
edu
.
hiast
.
service
;
import
org.apache.commons.lang3.RandomStringUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.data.mongodb.core.MongoTemplate
;
import
org.springframework.stereotype.Service
;
import
sy.edu.hiast.model.Movie
;
import
sy.edu.hiast.model.User
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.Random
;
@Service
public
class
DataGeneratorService
{
@Autowired
private
MongoTemplate
mongoTemplate
;
private
final
Random
random
=
new
Random
();
public
void
runGeneration
()
{
System
.
out
.
println
(
"Starting data generation..."
);
generateMovies
(
10000
);
generateUsers
(
5000
);
System
.
out
.
println
(
"Generation complete!"
);
}
private
void
generateMovies
(
int
count
)
{
List
<
Movie
>
movies
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
count
;
i
++)
{
Movie
m
=
new
Movie
();
m
.
setTitle
(
generateName
());
m
.
setRating
(
random
.
nextFloat
()
*
10.0f
);
m
.
setDirectors
(
Arrays
.
asList
(
generateName
(),
generateName
()));
movies
.
add
(
m
);
if
(
movies
.
size
()
>=
1000
)
{
// Batch insert for speed
mongoTemplate
.
insertAll
(
movies
);
movies
.
clear
();
}
}
}
private
void
generateUsers
(
int
count
)
{
List
<
User
>
users
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
count
;
i
++)
{
User
u
=
new
User
();
u
.
setUser_name
(
"User_"
+
RandomStringUtils
.
randomAlphanumeric
(
5
));
u
.
setWatched_movies
(
Arrays
.
asList
(
"Inside out"
,
"Aladdin"
));
u
.
setSubscription_month
(
random
.
nextInt
(
12
)
+
1
);
users
.
add
(
u
);
if
(
users
.
size
()
>=
1000
)
{
mongoTemplate
.
insertAll
(
users
);
users
.
clear
();
}
}
}
private
String
generateName
()
{
return
RandomStringUtils
.
randomAlphabetic
(
1
).
toUpperCase
()
+
RandomStringUtils
.
randomAlphabetic
(
7
).
toLowerCase
();
}
}
movie-shard-app/src/main/resources/application.properties
0 → 100644
View file @
fa47f6e6
# Connect to the MONGOS Router port
spring.data.mongodb.uri
=
mongodb://localhost:27021/videodb
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment