Use AnalyticDB for MySQL Spark to read from ApsaraMQ for Kafka over an elastic network interface (ENI). This topic walks you through setting up the Maven dependencies, writing a Spark Streaming program, and submitting the job with the ENI parameters required for VPC-based Kafka access.
Prerequisites
Before you begin, make sure you have:
An AnalyticDB for MySQL Data Lakehouse Edition cluster with a job resource group. See Create and manage a resource group.
A database account for the AnalyticDB for MySQL cluster:
Alibaba Cloud account: a privileged account. See the "Create a privileged account" section in Create a database account.
RAM user: a privileged account and a standard account, with the standard account associated with the RAM user. See Create a database account and Associate or disassociate a database account with or from a RAM user.
An ApsaraMQ for Kafka instance in the same region as the AnalyticDB for MySQL cluster, with a topic and a consumer group created. See Purchase and deploy an Internet- and VPC-connected instance and Create resources.
Object Storage Service (OSS) activated with a bucket in the same region as the AnalyticDB for MySQL cluster. See Activate OSS and Create buckets.
Gather network information
Collect the following values from your Kafka instance before configuring the Spark job:
Log on to the ApsaraMQ for Kafka console and go to the Instance Details page. Note the vSwitch ID of the Kafka instance.
Log on to the Elastic Compute Service (ECS) console and go to the Security Groups page. Note the ID of the security group associated with the Kafka instance.
In the ApsaraMQ for Kafka console, go to Whitelist Management and confirm that the CIDR block of the vSwitch is added to a whitelist of the Kafka instance.
Connect AnalyticDB for MySQL Spark to ApsaraMQ for Kafka
Step 1: Add dependencies
Add the following dependencies to your pom.xml. The Spark-Kafka connector version must match the Spark runtime version of your cluster.
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-sql-kafka-0-10 -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql-kafka-0-10_2.12</artifactId>
<version>3.2.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-clients -->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.8.1</version>
</dependency>For other versions, see kafka-clients and spark-sql-kafka-0-10 on Maven Repository.
Step 2: Write and package the Spark Streaming program
Write a Spark Streaming program that reads from ApsaraMQ for Kafka. The following sample code reads messages from a Kafka topic and prints them to the console. After packaging, the output JAR is named spark-example.jar.
package com.aliyun.spark.streaming
import org.apache.kafka.clients.consumer.ConsumerRecord
import org.apache.spark.SparkConf
import org.apache.spark.sql.SparkSession
object SparkKafka {
def main(args: Array[String]): Unit = {
if(args.length < 3){
System.err.println(
"""
|args0: groupId
|args1: topicName
|args2: bootstrapServers
|""".stripMargin)
System.exit(1)
}
val groupId = args(0)
val topicName = args(1)
val bootstrapServers = args(2)
val sparkConf: SparkConf = new SparkConf()
.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
.setAppName("SparkKafkaSub")
sparkConf.registerKryoClasses(Array(classOf[ConsumerRecord[_,_]]))
val sparkSession = SparkSession
.builder()
.config(sparkConf)
.getOrCreate()
val df = sparkSession
.readStream
.format("kafka")
// The endpoint of the Kafka instance.
.option("kafka.bootstrap.servers", bootstrapServers)
// The name of the topic to consume.
.option("subscribe", topicName)
// The consumer group ID.
.option("group.id", groupId)
.load()
val query = df.selectExpr("CAST(key AS STRING)", "CAST(value AS STRING)")
.writeStream
.outputMode("append")
.format("console")
.start()
query.awaitTermination()
}
}Step 3: Upload JAR packages to OSS
Upload the following files to your OSS bucket. For upload instructions, see Upload objects.
The downloaded
kafka-clients-2.8.1.jarandspark-sql-kafka-0-10_2.12-3.2.0.jarThe packaged
spark-example.jar
Step 4: Submit the Spark job
Log on to the AnalyticDB for MySQL console. Select a region, then click Clusters in the left-side navigation pane.
On the Data Lakehouse Edition tab, find the cluster and click the cluster ID.
In the left-side navigation pane, choose Job Development > Spark JAR Development.
Select a job resource group and set the job type. This example uses the batch type.
Enter the following configuration in the Spark editor, replacing the placeholder values with your actual vSwitch ID and security group ID:
{ "args": [ "kafka_groupId", "kafka_test", "alikafka-pre-cn-x0r34a20****-1-vpc.alikafka.aliyuncs.com:9092,alikafka-pre-cn-x0r34a20****-2-vpc.alikafka.aliyuncs.com:9092,alikafka-pre-cn-x0r34a20****-3-vpc.alikafka.aliyuncs.com:9092" ], "file": "oss://<bucket_name>/spark-example.jar", "jars": "oss://<bucket_name>/kafka-clients-2.8.1.jar,oss://<bucket_name>/spark-sql-kafka-0-10_2.12-3.2.0.jar", "name": "Kafka Example", "className": "com.aliyun.spark.streaming.SparkKafka", "conf": { "spark.driver.resourceSpec": "small", "spark.executor.instances": 1, "spark.executor.resourceSpec": "small", "spark.adb.eni.enabled": "true", "spark.adb.eni.vswitchId": "vsw-bp17jqw3lrrobn6y****", "spark.adb.eni.securityGroupId": "sg-bp163uxgt4zandx****" } }The following table describes the key parameters. For all supported Spark configuration parameters, see Spark application configuration parameters.
Parameter Description argsThe arguments passed to the Spark job. Separate multiple arguments with commas (,). fileThe path of the main file of the Spark job. The main file can be a JAR package that contains the entry class or an executable file that serves as the entry point for the Python program. NoteThe main file of a Spark job must be stored in OSS.
jarsThe JAR packages that are required for a Spark job. Separate multiple JAR packages with commas (,). nameThe name of the Spark job. classNameThe entry class of the Java or Scala program. This parameter is not required for a Python program. spark.adb.eni.enabledSet to "true"to enable ENI. Required when accessing ApsaraMQ for Kafka from Enterprise Edition, Basic Edition, or Data Lakehouse Edition Spark.spark.adb.eni.vswitchIdThe vSwitch ID of the ApsaraMQ for Kafka instance. Obtained in Gather network information. spark.adb.eni.securityGroupIdThe ID of the security group associated with the ApsaraMQ for Kafka instance. Obtained in Gather network information. confThe configuration parameters that are required for the Spark job, which are similar to those of Apache Spark. The parameters must be in the key:valueformat. Separate multiple parameters with commas (,). For more information about the configuration parameters that are different from those of Apache Spark or the configuration parameters specific to AnalyticDB for MySQL, see Spark application configuration parameters.Click Run Now.