Commit fa47f6e6 authored by drnull03's avatar drnull03

Finished the porject

parent 7fb829ee
# 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" });
{
"snyk.advanced.autoSelectOrganization": true
}
\ No newline at end of file
<?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>
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
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; }
}
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; }
}
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();
}
}
# Connect to the MONGOS Router port
spring.data.mongodb.uri=mongodb://localhost:27021/videodb
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment