This repository contains a Docker Compose configuration to quickly set up a MongoDB instance and mongo-express, a web-based MongoDB admin interface.
Make sure you have Docker and Docker Compose installed on your system. If not, you can install them from the official Docker website:
To use this setup, follow these steps:
-
Clone this repository to your local machine:
git clone https://github.com/Ylenia1211/BDM24_spark_example.git
-
Navigate to the cloned repository directory:
cd your-repository -
Start the services using Docker Compose:
docker-compose up
The services will now start in detached mode, and you can access mongo-express at http://localhost:8081 in your web browser. Use root as the username and example as the password to log in.
The docker-compose.yml file defines two services:
-
mongo: MongoDB instance with the specified root credentials.
-
mongo-express: Web-based MongoDB admin interface connected to the MongoDB instance.
- Username: root
- Password: example
- mongo-express: Accessible at
http://localhost:8081. You can change the host port in thedocker-compose.ymlfile if needed.
- This setup is intended for development and testing purposes. Make sure to secure your MongoDB instance appropriately before deploying it in a production environment.
- Ensure that the provided credentials are strong and not used in production environments without proper security measures.
This Java program demonstrates basic CRUD (Create, Read, Update, Delete) operations using the MongoDB Java driver.
Make sure you have the MongoDB Java driver added to your project dependencies.
-
Make sure your MongoDB instance is running and accessible.
-
Customize the connection string (
uri) according to your MongoDB setup.
Link to SparkMongoDBExample.java

Ensure you have Apache Spark installed and configured on your system. Also, make sure you have the MongoDB Java driver and the Spark MongoDB Connector added to your project dependencies.
-
Import necessary libraries:
import org.apache.spark.api.java.JavaRDD; import org.apache.spark.api.java.JavaSparkContext; import org.apache.spark.sql.SparkSession; import org.bson.Document; import com.mongodb.spark.MongoSpark;
-
Define the main class
SparkMongoDBExampleand itsmainmethod. -
Inside the
mainmethod, create a Spark session:SparkSession spark = SparkSession.builder() .appName("MongoDBSparkConnectorExample") .master("local[*]") // Set the master URL .config("spark.mongodb.input.uri","mongodb://root:example@localhost:27017/db_spark.collection1?authSource=admin") .config("spark.mongodb.output.uri", "mongodb://root:example@localhost:27017/db_spark.collection1?authSource=admin") .config("spark.jars.packages", "org.mongodb.spark:mongo-spark-connector_2.12:10.0.0") .getOrCreate();
- Customize the connection URI according to your MongoDB setup.
-
Create a Spark context:
JavaSparkContext jsc = new JavaSparkContext(spark.sparkContext());
-
Read data from MongoDB collection:
JavaRDD<Document> rdd = MongoSpark.load(jsc);
-
Perform operations on the data, for example, displaying the first 10 documents and counting the total number of documents:
rdd.take(10).forEach(System.out::println); long count = rdd.count(); System.out.println("Number of documents in the collection: " + count);
-
Close the Spark session and context:
jsc.close(); spark.stop();
- Ensure your MongoDB instance is running and accessible.
- Customize the connection URI (
spark.mongodb.input.uriandspark.mongodb.output.uri) according to your MongoDB setup. - Replace
db_sparkwith the name of your database andcollection1with the name of your collection. - This example assumes a local Spark setup (
master("local[*]")). Adjust the master URL accordingly for a cluster setup.