Use AnalyticDB for MySQL Spark to read data from ApsaraDB for MongoDB by submitting a Spark JAR job that connects over a Virtual Private Cloud (VPC).
Prerequisites
Before you begin, ensure that you have:
An AnalyticDB for MySQL cluster running Enterprise Edition, Basic Edition, or Data Lakehouse Edition
A database account for the cluster:
Alibaba Cloud account users: create a privileged account
Resource Access Management (RAM) users: create a privileged account and a standard account and associate the standard account with the RAM user
An ApsaraDB for MongoDB instance in the same region as your cluster, with a database and collection that already contain data. See Getting started
The IP address of the vSwitch associated with the ApsaraDB for MongoDB instance added to the instance whitelist
The ApsaraDB for MongoDB instance added to a security group whose inbound and outbound rules allow traffic on the MongoDB ports. See Add a security group to create one, then add a security group rule to open the required ports
To find the vSwitch IP address, go to the Basic Information page of your ApsaraDB for MongoDB instance to get the vSwitch ID, then look up the IP address in the VPC console.
How it works
The Mongo Spark Connector provides the mongodb format for Spark reads and writes. Connection setup uses two configuration keys:
spark.mongodb.read.connection.uri: specifies the VPC endpoint of the ApsaraDB for MongoDB instance for read operationsspark.mongodb.write.connection.uri: specifies the VPC endpoint of the ApsaraDB for MongoDB instance for write operations
The connector JAR and its dependencies are stored in OSS and referenced in the job configuration, keeping them separate from your application code.
Connect AnalyticDB for MySQL Spark to ApsaraDB for MongoDB
Step 1: Download the required JAR packages
Download the following JAR packages from Maven Central and upload them to an OSS bucket:
| JAR package | Version | Download |
|---|---|---|
mongo-spark-connector_2.12 | 10.1.1 | Download |
mongodb-driver-sync | 4.8.2 | Download |
bson | 4.8.2 | Download |
bson-record-codec | 4.8.2 | Download |
mongodb-driver-core | 4.8.2 | Download |
Step 2: Add the Maven dependency
Add the following dependency to your pom.xml:
<dependency>
<groupId>org.mongodb.spark</groupId>
<artifactId>mongo-spark-connector_2.12</artifactId>
<version>10.1.1</version>
</dependency>Step 3: Write and package the Spark program
Write a Spark program that connects to ApsaraDB for MongoDB, compile it, and package it as a JAR file. The example below reads from a MongoDB collection and prints the results. This topic uses spark-mongodb.jar as the output JAR name.
package com.aliyun.spark
import org.apache.spark.sql.SparkSession
object SparkOnMongoDB {
def main(args: Array[String]): Unit = {
// The VPC endpoint of the ApsaraDB for MongoDB instance.
// Find it on the Database Connection page in the ApsaraDB for MongoDB console.
val connectionUri = args(0)
// The name of the database.
val database = args(1)
// The name of the collection.
val collection = args(2)
val spark = SparkSession.builder()
.appName("MongoSparkConnectorIntro")
.config("spark.mongodb.read.connection.uri", connectionUri)
.config("spark.mongodb.write.connection.uri", connectionUri)
.getOrCreate()
val df = spark.read.format("mongodb")
.option("database", database)
.option("collection", collection)
.load()
df.show()
spark.stop()
}
}For additional configuration options, see Configuration Options in the MongoDB Spark Connector documentation. Code examples are available for writing to MongoDB and reading from MongoDB.
Step 4: Submit the Spark JAR job
Log on to the AnalyticDB for MySQL console. In the upper-left corner, select a region. In the left navigation pane, click Clusters, then click the target cluster ID.
In the left navigation pane, choose Job Development > Spark JAR Development.
In the editor, enter the following job configuration:
ImportantAnalyticDB for MySQL Spark supports both VPC and internet access to ApsaraDB for MongoDB. Use VPC for better security and performance.
Parameter Description argsArguments passed to the JAR program: the MongoDB VPC endpoint URI, database name, and collection name, in that order. Separate multiple arguments with commas. fileOSS path of your compiled application JAR ( spark-mongodb.jar).jarsOSS paths of the connector JAR packages that Spark needs to access MongoDB. nameName of the Spark job. classNameEntry class of the Java or Scala program. Not required for Python applications. spark.adb.eni.enabledSpecifies whether to enable Elastic Network Interface (ENI) access. You must enable ENI access when you use Data Lakehouse Edition Spark to access the MongoDB data source. spark.adb.eni.vswitchIdThe vSwitch ID. Find it on the Basic Information page of your ApsaraDB for MongoDB instance. spark.adb.eni.securityGroupIdID of the security group added to the ApsaraDB for MongoDB instance. If no security group exists, see Add a security group. Other confparametersSpark job configuration parameters, following the same format as Apache Spark. Use key:valueformat, separated by commas. See Spark application configuration parameters.{ "args": [ "mongodb://<username>:<password>@<host1>:<port1>,<host2>:<port2>,...,<hostN>:<portN>/<database_name>", "<database_name>", "<collection_name>" ], "file": "oss://<bucket_name>/spark-mongodb.jar", "jars": [ "oss://<bucket_name>/mongo-spark-connector_2.12-10.1.1.jar", "oss://<bucket_name>/mongodb-driver-sync-4.8.2.jar", "oss://<bucket_name>/bson-4.8.2.jar", "oss://<bucket_name>/bson-record-codec-4.8.2.jar", "oss://<bucket_name>/mongodb-driver-core-4.8.2.jar" ], "name": "MongoSparkConnectorIntro", "className": "com.aliyun.spark.SparkOnMongoDB", "conf": { "spark.driver.resourceSpec": "medium", "spark.executor.instances": 2, "spark.executor.resourceSpec": "medium", "spark.adb.eni.enabled": "true", "spark.adb.eni.vswitchId": "vsw-bp14pj8h0****", "spark.adb.eni.securityGroupId": "sg-bp11m93k021tp****" } }Replace all
<placeholder>values with your actual resource identifiers. The following table describes each parameter:Click Execute Now.
-
{ "args": [ "mongodb://root:ALiyun***@dds-bp13f15dfdeac6141.mongodb.rds.aliyuncs.com:3717,dds-bp13f15dfdeac6142.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-68693516", "test", "mongo" ], "file": "oss://adb-test1/MongDB/spark-mongodb-1.0-SNAPSHOT.jar", "jars": [ "oss://adb-test1/MongDB/mongo-spark-connector_2.12-10.1.1.jar", "oss://adb-test1/MongDB/mongodb-driver-sync-4.8.2.jar", "oss://adb-test1/MongDB/bson-4.8.2.jar", "oss://adb-test1/MongDB/bson-record-codec-4.8.2.jar", "oss://adb-test1/MongDB/mongodb-driver-core-4.8.2.jar" ], "name": "MongoSparkConnectorIntro", "className": "com.aliyun.spark.SparkOnMongoDB", "conf": { "spark.driver.resourceSpec": "medium", "spark.executor.instances": 2, "spark.executor.resourceSpec": "medium", "spark.adb.eni.enabled": "true", "spark.adb.eni.vswitchId": "vsw-bp1skxmyj1lhdx4sq2u0p", "spark.adb.eni.securityGroupId": "sg-bp15it2qvpa017gkun5j" } }
Verify the result
After submitting the job, wait for the application status in Application List to change to Completed. Then click Log in the Actions column to view the data read from the MongoDB collection.