Commit 0d3be7b4 authored by mohammad.salama's avatar mohammad.salama

All Done

parent 5ef39394
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="MongoDB-Reader" />
</profile>
</annotationProcessing>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17 (2)" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="9ca737e0-d8bf-4a68-bfb8-89308a7b8ac5" name="Changes" comment="" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="ProjectId" id="2fBLnNuNH4twkdBUWpeq0CmKFn3" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"last_opened_file_path": "D:/HIAST/FIY/SS/Advanced Distributed Systems/Lab/5/MongoDB-Reader/src/main/resources"
}
}]]></component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="D:\HIAST\FIY\SS\Advanced Distributed Systems\Lab\5\MongoDB-Reader\src\main\resources" />
</key>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="9ca737e0-d8bf-4a68-bfb8-89308a7b8ac5" name="Changes" comment="" />
<created>1713269694077</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1713269694077</updated>
</task>
<servers />
</component>
</project>
\ 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>MongoDB-Reader</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>5.0.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.6</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package org.example;
import com.mongodb.ClientSessionOptions;
import com.mongodb.ReadPreference;
import com.mongodb.WriteConcern;
import com.mongodb.client.*;
import com.mongodb.connection.ClusterDescription;
import org.bson.Document;
import org.bson.conversions.Bson;
import java.util.List;
import static com.mongodb.client.model.Filters.eq;
public class Main
{
///use mongos
private static final String Mongo_URL="mongodb://192.168.184.11:27117"; /*+
"192.168.184.11:27027," +
"192.168.184.11:27037" +
"/?replicaSet=rs1";*/
private static final String DB_Name = "sample_mflix";
private static final Double Min_gpa = 90.0;
public static void main(String[] args)
{
/*String cousre = args[0];
String student = args[1];*/
String cousre = "Math";
String student = "Abd";
double grade = 200;
MongoDatabase DB = connectToMongoDB();
//enroll(DB , cousre , student , 15 , grade);
//enable Sharding
DB.runCommand(new Document("enable sharding" , DB_Name));
//create collection
DB.createCollection("movies");
//shard a collection
DB.runCommand(new Document("shardCollection", DB_Name+"."+"movies")
.append("key", new Document("_id", "hashed")));
}
private static void enroll(MongoDatabase DB , String course , String student , int age , double gpa)
{
/*if (!isValid(DB , course))
{
System.out.println("Not Valid Course");
return;
}*/
MongoCollection<Document> collection = DB.getCollection(course)
.withWriteConcern(WriteConcern.MAJORITY)
.withReadPreference(ReadPreference.primaryPreferred());
if (collection.find(eq("name" , student)).first() != null)
{
System.out.println("Student : "+student + " already exists");
return;
}
if (gpa < Min_gpa)
{
System.out.println("Please Improve Grade");
return;
}
collection.insertOne(new Document("name" , student).append("age",age).append("gpa",gpa));
System.out.println("Success!!");
for (Document doc : collection.find())
{
System.out.println(doc);
}
}
private static boolean isValid(MongoDatabase DB , String course)
{
for (String co : DB.listCollectionNames())
{
if (co.equals(course)) return true;
}
return false;
}
private static MongoDatabase connectToMongoDB()
{
MongoClient mongoClient = MongoClients.create(Mongo_URL);
return mongoClient.getDatabase(DB_Name);
}
}
\ No newline at end of file
# Root logger option
log4j.rootLogger=ERROR, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss} %-5p %c{1}:%L - %m%n
# Root logger option
log4j.rootLogger=ERROR, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss} %-5p %c{1}:%L - %m%n
No preview for this file type
File added
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