AnalyticDB for MySQL lets you submit Spark SQL jobs to query data from self-managed MySQL databases, ApsaraDB RDS for MySQL, or PolarDB for MySQL. This topic shows how to read ApsaraDB RDS for MySQL data using two access modes: view mode and catalog mode.
Prerequisites
Before you begin, ensure that you have:
-
An AnalyticDB for MySQL Data Lakehouse Edition cluster
-
The AnalyticDB for MySQL cluster and the ApsaraDB RDS for MySQL instance in the same region
-
A job resource group created for the cluster. See Create a resource group
-
A database account for the AnalyticDB for MySQL cluster:
-
Alibaba Cloud account: a privileged account. See Create a privileged account
-
Resource Access Management (RAM) user: both 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
-
-
The ApsaraDB RDS for MySQL instance added to a security group with inbound and outbound rules that allow traffic on the instance's port. See Configure a security group for an ApsaraDB RDS for MySQL instance and Add a security group rule
Step 1: Prepare data
Create a database and table in the ApsaraDB RDS for MySQL instance, then insert sample rows:
CREATE DATABASE `db`;
CREATE TABLE `db`.`test` (
`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 test VALUES(1,'a','b',5);
INSERT INTO test VALUES(2,'c','d',6);
INSERT INTO test VALUES(3,'e','f',7);
Step 2: (Optional) Upload a CA certificate and the ApsaraDB RDS for MySQL driver
Skip this step if you do not need SSL encryption. Go directly to Step 3: Submit a Spark SQL job.
-
Enable SSL encryption on the ApsaraDB RDS for MySQL instance and download the CA certificate. See Configure the SSL encryption feature.
ImportantYou can encrypt an internal or public connection. This example uses an internal connection. The CA certificate is valid for one year. Generate a new certificate before it expires — an expired certificate cannot establish an SSL connection.
-
Download the JDBC driver that matches your ApsaraDB RDS for MySQL engine version from https://dev.mysql.com/downloads/connector/j/. This example uses
mysql-connector-java-8.0.29.jar. -
Extract the CA certificate package. Upload the JKS file and the driver JAR to Object Storage Service (OSS). See Upload objects.
Step 3: Submit a Spark SQL job
-
Log on to the AnalyticDB for MySQL console. In the upper-left corner, select a region. In the left-side navigation pane, click Clusters. On the Data Lakehouse Edition tab, click the cluster ID.
-
In the left-side navigation pane, choose Job Development > SQL Development.
-
On the SQLConsole tab, select the Spark engine and a job resource group.
-
Enter and run the SQL for your access mode.
The examples below use placeholder values for user and password. Do not hardcode credentials in production jobs. Store them securely and reference them at runtime.