Use AnalyticDB for MySQL Spark to read and write data stored in Tablestore.
Prerequisites
Before you begin, ensure that you have:
An AnalyticDB for MySQL Data Lakehouse Edition clusterData Lakehouse Edition
A job resource group created for the cluster. See Create and manage a resource group
A database account configured for your identity type:
Alibaba Cloud account: a privileged account. See the "Create a privileged account" section of Create a database account
Resource Access Management (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
Authorization for AnalyticDB for MySQL to assume the AliyunADBSparkProcessingDataRole role. See Perform authorization
A Tablestore instance in the same region as the AnalyticDB for MySQL cluster
Access Tablestore from a Spark JAR job
Step 1: Download the required JAR packages
Download the following JAR packages:
Step 2: Add the Maven dependency
Add the following dependency to your pom.xml:
<dependency>
<groupId>com.aliyun.emr</groupId>
<artifactId>emr-tablestore</artifactId>
<version>2.2.0</version>
<scope>provided</scope>
</dependency>Step 3: Write and package the Spark program
Write a Spark program that reads from and writes to Tablestore, then package it as a JAR file. The following example reads all rows from a Tablestore table and writes them back. The output JAR in this example is named spark-tablestore.jar.
All examples use the tablestore format with the options map to configure the endpoint, credentials, table name, and data catalog (column schema).
package com.aliyun.spark
import com.aliyun.openservices.tablestore.hadoop.TableStore
import org.apache.spark.sql.SparkSession
import scala.collection.mutable
object SparkOnTablestore {
def main(args: Array[String]): Unit = {
// VPC endpoint of the Tablestore instance
val endpoint = args(0)
// Name of the Tablestore table
val tableName = args(1)
// Name of the Tablestore instance
val instanceName = args(2)
// AccessKey ID of the RAM user — read from environment variables
val accessKeyId = sys.env.get("ACCESS_KEY_ID")
// AccessKey secret of the RAM user — read from environment variables
val accessKeySecret = sys.env.get("ACCESS_KEY_SECRET")
// Data catalog: maps Spark SQL columns to Tablestore column names and types
val dataCatalog =
"""
|{"columns":{
| "PkString": {"type":"string"},
| "PkInt": {"type":"long"},
| "col1": {"type":"string"},
| "col2": {"type":"long"},
| "col3": {"type":"binary"},
| "timestamp": {"type":"long"},
| "col5": {"type":"double"},
| "col6": {"type":"boolean"}
| }
|}
""".stripMargin
val options = new mutable.HashMap[String, String]()
options.put("endpoint", endpoint)
options.put("access.key.id", accessKeyId.get)
options.put("access.key.secret", accessKeySecret.get)
options.put("table.name", tableName)
options.put("instance.name", instanceName)
options.put("catalog", dataCatalog)
val spark = SparkSession.builder().getOrCreate()
// Read data from Tablestore
val df = spark.read.format("tablestore").options(options).load()
df.show()
// Write data back to Tablestore
df.write.format("tablestore").options(options).save()
TableStore.shutdown()
spark.stop()
}
}Step 4: Upload JAR packages to OSS
Upload the JAR packages from Steps 1 and 3 to Object Storage Service (OSS). See Simple upload.
Step 5: Open Spark JAR development in the console
Log on to the AnalyticDB for MySQL console. In the upper-left corner, select a region.
In the left-side navigation pane, click ClustersData Lakehouse Edition. On the Data Lakehouse Edition tab, find the cluster and click its ID.
In the left-side navigation pane, choose Job Development > Spark JAR Development.
Step 6: Configure and run the Spark job
Select a job resource group and set the job type to Batch.
Enter the following configuration in the Spark editor. Replace the placeholders with your actual values.
{ "args": [ "https://i01164l****.<region_id>.vpc.tablestore.aliyuncs.com", "spark_test", "i01164l****" ], "file": "oss://<bucket_name>/spark-tablestore.jar", "jars": [ "oss://<bucket_name>/emr-tablestore-2.3.0-SNAPSHOT.jar", "oss://<bucket_name>/tablestore-5.10.3-jar-with-dependencies.jar" ], "name": "spark-on-tablestore", "className": "com.aliyun.spark.SparkOnTablestore", "conf": { "spark.driver.resourceSpec": "medium", "spark.executor.instances": 2, "spark.executor.resourceSpec": "medium", "spark.kubernetes.driverEnv.ACCESS_KEY_ID": "LTAI****************", "spark.kubernetes.driverEnv.ACCESS_KEY_SECRET": "yourAccessKeySecret", "spark.hadoopRDD.ignoreEmptySplits": false } }The following table describes the key parameters.
Parameter Description Example argsArguments passed to the JAR program, in the order expected by main(args): Tablestore VPC endpoint, table name, and instance name"https://i01164l****.<region_id>.vpc.tablestore.aliyuncs.com"fileOSS path of the application JAR ( spark-tablestore.jar)"oss://my-bucket/spark-tablestore.jar"jarsOSS paths of the dependency JARs required by the Spark job "oss://my-bucket/emr-tablestore-2.3.0-SNAPSHOT.jar"nameDisplay name of the Spark job "spark-on-tablestore"classNameEntry class of the Scala or Java program. Not required for Python applications "com.aliyun.spark.SparkOnTablestore"spark.kubernetes.driverEnv.ACCESS_KEY_IDAccessKey ID of the RAM user used to access Tablestore, injected as an environment variable into the Spark driver "LTAI5tXxx"spark.kubernetes.driverEnv.ACCESS_KEY_SECRETAccessKey secret of the RAM user used to access Tablestore, injected as an environment variable into the Spark driver "yourAccessKeySecret"spark.hadoopRDD.ignoreEmptySplitsWhether to skip empty partitions. Must be set to `false` when accessing Tablestore. falseconfSpark configuration in key:valueformat, compatible with Apache Spark parameters. Separate multiple parameters with commas (,). See Spark application configuration parameters— Click Run Now.
Verify the result
After submitting the job, go to the Applications tab and wait for the Spark application status to change to Completed. Then click Logs in the Actions column to view the Tablestore table data printed by df.show().
What's next
Spark application configuration parameters — full reference for Spark job
confoptionsCreate and manage a resource group — manage job resource groups for your cluster