EMR Serverless Spark provides a built-in MaxCompute data source connector based on Spark DataSource V2. You can connect to MaxCompute by adding the required configurations during development.
Background information
MaxCompute (formerly known as ODPS) is a fast, fully managed, exabyte-scale data warehouse solution for storing and computing batch structured data. It provides analytics and modeling services. For more information about MaxCompute, see What is MaxCompute?.
Prerequisites
-
You have created a workspace in EMR Serverless Spark.
-
You have created a MaxCompute project and enabled Open Storage for it.
The examples in this topic use the pay-as-you-go billing method for Open Storage.
Limits
-
The following engine versions are supported:
-
esr-4.x: esr-4.6.0 and later
-
esr-3.x: esr-3.5.0 and later
-
esr-2.x: esr-2.9.0 and later
-
-
You must enable the Open Storage feature for MaxCompute. For more information, see Open Storage.
-
The MaxCompute endpoint you use must support the Storage API. If not, switch to a supported endpoint. For more information, see Data transmission resources.
Notes
When you use Open Storage (pay-as-you-go), data transfers that exceed 1 TB are billed based on the logical data size.
Procedure
Step 1: Create a MaxCompute session
Create an SQL session or a notebook session to connect to MaxCompute. For more information about sessions, see Session management.
SQL session
-
Go to the Sessions page.
-
Log on to the EMR console.
-
In the left-side navigation pane, choose EMR Serverless > Spark.
-
On the Spark page, click the name of the target workspace.
-
On the EMR Serverless Spark page, click Sessions in the left-side navigation pane.
-
-
On the SQL Session tab, click Create SQL Session.
-
On the Create SQL Session page, configure the following parameters and click Create.
Parameter
Description
Name
Enter a custom name for the SQL session. For example,
mc_sql_compute.Spark Configuration
Enter the Spark configuration parameters to connect to MaxCompute.
ImportantIf you need to access a MaxCompute project that uses the three-level model, you must also set the
spark.sql.catalog.odps.enableNamespaceSchemaparameter totruein the Spark configuration. For more information about parameters, see Spark Connector. For more information about schemas, see Schema operations.spark.sql.catalog.odps org.apache.spark.sql.execution.datasources.v2.odps.OdpsTableCatalog spark.sql.extensions org.apache.spark.sql.execution.datasources.v2.odps.extension.OdpsExtensions spark.sql.sources.partitionOverwriteMode dynamic spark.hadoop.odps.tunnel.quota.name pay-as-you-go spark.hadoop.odps.project.name <project_name> spark.hadoop.odps.end.point https://service.cn-hangzhou-vpc.maxcompute.aliyun-inc.com/api spark.hadoop.odps.access.id <accessId> spark.hadoop.odps.access.key <accessKey>Replace the following placeholders with your actual values:
-
<project_name>: the name of your MaxCompute project. -
https://service.cn-hangzhou-vpc.maxcompute.aliyun-inc.com/api: the endpoint of your MaxCompute service. For more information, see Endpoints. -
<accessId>: the AccessKey ID of the Alibaba Cloud account used to access MaxCompute. -
<accessKey>: the AccessKey Secret of the Alibaba Cloud account used to access MaxCompute.
-
Notebook session
-
Go to the Notebook Sessions page.
-
Log on to the EMR console.
-
In the navigation pane on the left, choose .
-
On the Spark page, click the name of the target workspace.
-
On the EMR Serverless Spark page, choose Sessions in the navigation pane on the left.
-
Click the Notebook Session tab.
-
-
Click Create Notebook Session.
-
On the Create Notebook Session page, configure the following parameters and click Create.
Parameter
Description
Name
Enter a custom name for the notebook session. For example,
mc_notebook_compute.Spark Configuration
Enter the Spark configuration parameters to connect to MaxCompute.
ImportantIf you need to access a MaxCompute project that uses the three-level model, you must also set the
spark.sql.catalog.odps.enableNamespaceSchemaparameter totruein the Spark configuration. For more information about parameters, see Spark Connector. For more information about schemas, see Schema operations.spark.sql.catalog.odps org.apache.spark.sql.execution.datasources.v2.odps.OdpsTableCatalog spark.sql.extensions org.apache.spark.sql.execution.datasources.v2.odps.extension.OdpsExtensions spark.sql.sources.partitionOverwriteMode dynamic spark.hadoop.odps.tunnel.quota.name pay-as-you-go spark.hadoop.odps.project.name <project_name> spark.hadoop.odps.end.point https://service.cn-hangzhou-vpc.maxcompute.aliyun-inc.com/api spark.hadoop.odps.access.id <accessId> spark.hadoop.odps.access.key <accessKey>Replace the following placeholders with your actual values:
-
<project_name>: the name of your MaxCompute project. -
https://service.cn-hangzhou-vpc.maxcompute.aliyun-inc.com/api: the endpoint of your MaxCompute service. For more information, see Endpoints. -
<accessId>: the AccessKey ID of the Alibaba Cloud account used to access MaxCompute. -
<accessKey>: the AccessKey Secret of the Alibaba Cloud account used to access MaxCompute.
-
Step 2: Query and write MaxCompute data
Spark SQL
-
In the left-side navigation pane of the EMR Serverless Spark page, click Development.
-
On the Development tab, click the
icon. -
Create a Spark SQL job.
-
In the dialog box that appears, enter a name such as
mc_load_task, select SparkSQL as the type, and then click OK. -
Copy the following code into the new Spark SQL tab (
mc_load_task).CREATE TABLE odps.default.mc_table (name STRING, num BIGINT); INSERT INTO odps.default.mc_table (name, num) VALUES ('Alice', 100),('Bob', 200); SELECT * FROM odps.default.mc_table; -
Select a database from the Database drop-down list. Then, from the Compute drop-down list, select the SQL session that you created in Step 1: Create a MaxCompute session (
mc_sql_compute). -
Click Run to execute the Spark SQL job.
After the query is successfully executed, the query result is displayed on the Execution Results tab.
Result #3 shows a table with two columns, name and num, containing the inserted values for Alice (100) and Bob (200).
-
-
View the created table in the MaxCompute console.
-
Log on to the MaxCompute console and select a region in the upper-left corner.
-
On the Projects page, find the project that you created and click Manage in the Actions column.
-
Click the Tables tab.
You can see the new table named
mc_tablein the MaxCompute console.
-
Notebook
-
In the left-side navigation pane of the EMR Serverless Spark page, click Development.
-
On the Development tab, click the
icon. -
Create a notebook.
-
In the dialog box that appears, enter a name such as
mc_load_task. For the type, select Interactive Development > Notebook, and then click OK. -
From the session drop-down list, select the running notebook session that you created in Step 1: Create a MaxCompute session (
mc_notebook_compute). -
Write and run the code.
-
In a Python cell, enter the following command to create a table.
spark.sql(""" CREATE TABLE odps.default.mc_table (name STRING, num BIGINT); """) -
In a new Python cell, enter the following command to insert data.
spark.sql("INSERT INTO odps.default.mc_table (name, num) VALUES ('Alice', 100),('Bob', 200);") -
In a new Python cell, enter the following command to query data.
spark.sql("SELECT * FROM odps.default.mc_table;").show()After the query is successfully executed, the query result is displayed in the Execution Results.
+-----+---+ | name|num| +-----+---+ |Alice|100| | Bob|200| +-----+---+
-
-
-
View the created table in the MaxCompute console.
-
Log on to the MaxCompute console and select a region in the upper-left corner.
-
On the Projects page, find the project that you created and click Manage in the Actions column.
-
Click the Tables tab.
You can see the new table named
mc_tablein the MaxCompute console.
-
FAQ
Related topics
The examples in this topic use Spark SQL and notebooks. For other methods of reading from and writing to MaxCompute, see Develop a batch or streaming task.