This topic describes how to use the serverless Spark engine of Data Lake Analytics (DLA) to access ApsaraDB RDS databases in a virtual private cloud (VPC) over an elastic network interface (ENI).
Prerequisites
The serverless Spark engine is authorized to access your VPC. For more information about how to authorize the serverless Spark engine to access a VPC, see Configure the network of data sources.
Configure a whitelist for an ApsaraDB RDS instance

Write test data to an ApsaraDB RDS database
CREATE TABLE `persons` (
`id` int(11) DEFAULT NULL,
`first_name` varchar(32) DEFAULT NULL,
`laster_name` varchar(32) DEFAULT NULL,
`age` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
;
insert into persons VALUES(1,'a','b',5);
insert into persons VALUES(2,'c','d',6);
insert into persons VALUES(3,'e','f',7);
Write a Spark application to access the ApsaraDB RDS database
package com.aliyun.spark
import org.apache.spark.sql.SparkSession
object SparkRDS {
def main(args: Array[String]): Unit = {
val sparkSession = SparkSession.builder()
.appName("rds test")
.getOrCreate()
val url = args(0)
val dbtable = args(1)
val user = args(2)
val password = args(3)
val jdbcDF = sparkSession.read
.format("jdbc")
.option("url", url)
.option("driver", "com.mysql.jdbc.Driver")
.option("dbtable", dbtable)
.option("user", user)
.option("password", password)
.load()
jdbcDF.show()
}
}
Upload files to OSS
Compile code and package it into a JAR file, and upload the JAR file of the Spark application and MySQL driver dependency to Object Storage Service (OSS). You can click the link to download the MySQL driver dependency.
For more information, see Upload objects.
Submit a job
{
"args": [
"jdbc:mysql://URL of your ApsaraDB RDS database",
"persons",
"spark",
"Password to access your ApsaraDB RDS database"
],
"name": "changqing-dla-test",
"jars": [
"oss://changqing-dla-test/mysql-connector-java.jar"
],
"file": "oss://changqing-dla-test/rds_test.jar",
"className": "com.aliyun.spark.SparkRDS",
"conf": {
"spark.dla.eni.enable": "true",
"spark.dla.eni.vswitch.id": "ID of the vSwitch that you selected",
"spark.dla.eni.security.group.id": "ID of the security group that you selected",
"spark.driver.resourceSpec": "medium",
"spark.executor.instances": 1,
"spark.executor.resourceSpec": "medium"
}
}
