> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

<!--
Tab options on this page. Append to the .md URL to filter:
  ?tabs=<id,...>   select specific tabs (e.g. ?tabs=nodejs,shell)
  ?allTabs=true    include every tab
  (no param)       default: one tab per tabset

Available tabs:
  drivers: java-sync, python, scala
-->

# Getting Started with the Spark Connector

## Prerequisites

- Basic working knowledge of MongoDB and Apache Spark. Refer to the [MongoDB documentation](https://www.mongodb.com/docs/manual/) and [Spark documentation](https://spark.apache.org/docs/latest/) for more details.

- MongoDB version 4.2 or later

- Spark version 4.0 or later

- Java 8 or later

## Getting Started

### Java (Sync)

**Important:**

In version 10.0.0 and later of the Connector, use the format `mongodb` to read from and write to MongoDB:

`df = spark.read.format("mongodb").load()`

### Dependency Management

Provide the Spark Core, Spark SQL, and MongoDB Spark Connector dependencies to your dependency management tool.

The following example shows how to include these dependencies in a Maven `pom.xml` file:

```xml
<dependencies>
  <dependency>
    <groupId>org.mongodb.spark</groupId>
    <artifactId>mongo-spark-connector_2.13</artifactId>
    <version>11.1.0</version>
  </dependency>
  <dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-core_2.13</artifactId>
    <version>4.0.0</version>
  </dependency>
  <dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-sql_2.13</artifactId>
    <version>4.0.0</version>
  </dependency>
</dependencies>
```

### Configuration

When specifying the Connector configuration via `SparkSession`, you must prefix the settings appropriately. For details and other available MongoDB Spark Connector options, see the [Configuring Spark](https://www.mongodb.com/docs/spark-connector/current/configuration.md) guide.

```java
package com.mongodb.spark_examples;

import org.apache.spark.sql.SparkSession;

public final class GettingStarted {

  public static void main(final String[] args) throws InterruptedException {
    /* Create the SparkSession.
     * If config arguments are passed from the command line using --conf,
     * parse args for the values to set.
     */
    SparkSession spark = SparkSession.builder()
      .master("local")
      .appName("MongoSparkConnectorIntro")
      .config("spark.mongodb.read.connection.uri", "mongodb://127.0.0.1/test.myCollection")
      .config("spark.mongodb.write.connection.uri", "mongodb://127.0.0.1/test.myCollection")
      .getOrCreate();

    // Application logic

  }
}
```

- The `spark.mongodb.read.connection.uri` specifies the MongoDB server  address(`127.0.0.1`), the database to connect (`test`), and the collection (`myCollection`) from which to read data, and the read preference.

- The `spark.mongodb.write.connection.uri` specifies the MongoDB server address(`127.0.0.1`), the database to connect (`test`), and the collection (`myCollection`) to which to write data.

You can use a `SparkSession` object to write data to MongoDB, read data from MongoDB, create Datasets, and perform SQL operations.

## Integrations

The following sections describe some popular third-party platforms that you can integrate Spark and the MongoDB Spark Connector with.

### Amazon EMR

Amazon EMR is a managed cluster platform that you can use to run big data frameworks like Spark. To install Spark on an EMR cluster, see [Getting Started with Amazon EMR](https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-gs.html) in the AWS documentation.

### Databricks

Databricks is an analytics platform for building, deploying, and sharing enterprise-level data. To integrate the MongoDB Spark Connector with Databricks, see [MongoDB](https://docs.databricks.com/aws/en/connect/external-systems/mongodb) in the Databricks documentation.

### Docker

Docker is an open-source platform that helps developers build, share, and run applications in containers.

- To start Spark in a Docker container, see [Apache Spark](https://hub.docker.com/r/apache/spark#!) in the Docker documentation and follow the steps provided.

- To learn how to deploy Atlas on Docker, see [Create a Local Atlas Deployment with Docker.](https://www.mongodb.com/docs/atlas/cli/current/atlas-cli-deploy-docker/)

### Kubernetes

Kubernetes is an open-source platform for automating containerization management. To run Spark on Kubernetes, see [Running Spark on Kubernetes](https://archive.apache.org/dist/spark/docs/4.0.0/running-on-kubernetes.html) in the Spark documentation.

## Tutorials

- [Write to MongoDB in Batch Mode](https://www.mongodb.com/docs/spark-connector/current/batch-mode/batch-write.md#std-label-batch-write-to-mongodb)

- [Read from MongoDB in Batch Mode](https://www.mongodb.com/docs/spark-connector/current/batch-mode/batch-read.md#std-label-batch-read-from-mongodb)

- [Write to MongoDB in Streaming Mode](https://www.mongodb.com/docs/spark-connector/current/streaming-mode/streaming-write.md#std-label-streaming-write-to-mongodb)

- [Read from MongoDB in Streaming Mode](https://www.mongodb.com/docs/spark-connector/current/streaming-mode/streaming-read.md#std-label-streaming-read-from-mongodb)
