MaxCompute Spark node
Spark on MaxCompute supports running jobs in both local and cluster modes. In DataWorks, you can run Spark on MaxCompute offline jobs in cluster mode to integrate them with other types of nodes for scheduling. This document describes how to configure and schedule Spark on MaxCompute jobs using DataWorks.
Overview
Spark on MaxCompute is a computing service provided by MaxCompute that is compatible with open source Spark. It provides a Spark computing framework based on a unified computing resource and permission system. This allows you to submit and run Spark jobs using familiar development workflows for a wide range of data processing and analysis needs. In DataWorks, you can use the MaxCompute Spark node to schedule and run Spark on MaxCompute jobs and integrate them with other jobs.
Spark on MaxCompute supports development in Java, Scala, and Python, and runs jobs in either local or cluster mode. When you run an offline Spark on MaxCompute job in DataWorks, it executes in cluster mode. For more information about the run modes of Spark on MaxCompute, see Run modes.
Permission requirements
To develop jobs, RAM users must be added to the corresponding workspace and granted either the Development or Workspace Administrator role. The Workspace Administrator role includes extensive permissions and should be granted with caution. For more information about how to add a member to a workspace, see Add workspace members.
If you are using an Alibaba Cloud account, you can skip this step.
Limitations
If you encounter an error when submitting a MaxCompute Spark node that uses Spark 3.x, you must purchase and use a serverless resource group. For more information, see Use a serverless resource group.
Before you begin
The MaxCompute Spark node supports running Spark on MaxCompute offline jobs using Java/Scala and Python. The development steps and configuration UI differ for each language. Choose a language based on your business requirements.
Java/Scala
Before you can run Java or Scala code in a MaxCompute Spark node, you must develop the Spark on MaxCompute job code locally and upload it as a MaxCompute resource to DataWorks. Follow these steps:
-
Set up the development environment.
Prepare a development environment for running Spark on MaxCompute jobs based on your operating system. For more information, see Set up a Linux development environment and Set up a Windows development environment.
-
Develop the Java/Scala code.
Before you run the code in a MaxCompute Spark node, develop your Spark on MaxCompute code locally or in an existing environment. We recommend using the sample project template provided by Spark on MaxCompute.
-
Package the code and upload it to DataWorks.
After you finish development, package your code and upload it to DataWorks as a MaxCompute resource. For more information, see Resource Management.
Python with default environment
You can develop PySpark jobs in DataWorks by writing code directly into a Python resource. You can then submit and run the code using a MaxCompute Spark node. For development examples, see PySpark development examples.
If the default environment does not meet your job's dependency requirements, refer to the Python (using a custom environment) section to prepare a custom Python environment. Alternatively, you can use a PyODPS 2 node or a PyODPS 3 node, which offer better support for Python resources.
Python with custom environment
If the default Python environment does not meet your business requirements, follow these steps to use a custom Python environment to run your Spark on MaxCompute job.
-
Prepare a Python environment locally.
Refer to PySpark Python versions and dependency support to configure the required Python environment.
-
Package the environment and upload it to DataWorks.
Compress the Python environment into a .zip package and upload it to DataWorks as a MaxCompute resource. This package becomes the execution environment for your Spark on MaxCompute job.
Parameters
DataWorks runs Spark on MaxCompute offline jobs in Cluster mode. In Cluster mode, you must specify a custom program entry point main. The corresponding Spark job ends when the main function finishes with a status of Success or Fail. In addition, the configurations in spark-defaults.conf must be added one by one to the MaxCompute Spark node configurations. For example, the number of executors, memory size, and the spark.hadoop.odps.runtime.end.point configuration.
You do not need to upload the spark-defaults.conf file. Instead, you need to add the configurations in the spark-defaults.conf file one by one to the configuration items of a MaxCompute Spark node.
Java/Scala
|
Parameter |
Description |
Spark-submit command |
|
Spark Version |
The Spark version. Valid values: Spark 1.x, Spark 2.x, and Spark 3.x. Note
If you encounter an error when submitting a MaxCompute Spark node that uses the Spark 3.x version, purchase and use a serverless resource group. For more information, see Use a serverless resource group. |
— |
|
Language |
The programming language. Select Java/Scala or Python based on the language used to develop your Spark on MaxCompute job. |
— |
|
Main JAR Resource |
Specifies the main JAR resource file for the job. The resource file must be uploaded to DataWorks and committed. For more information, see Resource Management. |
|
|
Configuration Item |
Specifies the configuration items for submitting the job. Note the following:
|
|
|
Main Class |
Configure the main class name. This parameter is required when the development language is |
|
|
Parameter |
You can add parameters as needed and separate multiple parameters with a space. DataWorks supports scheduling parameters. The format for the Parameter is For information about the supported formats of scheduling parameter values, see Sources and expressions of scheduling parameters. |
|
|
JAR Resources |
This is supported only when the programming language is The resource files must be uploaded to DataWorks and committed. For more information, see Resource Management. |
Resource command:
|
|
File Resources |
Specifies the file resources for the job. |
|
|
Archive Resources |
Specifies the archive resources for the job. Only .zip archives are supported. |
|
Python
|
Parameter |
Description |
Spark-submit command |
|
Spark Version |
The Spark version. Valid values: Spark 1.x, Spark 2.x, and Spark 3.x. Note
If you encounter an error when submitting a MaxCompute Spark node that uses the Spark 3.x version, purchase and use a serverless resource group. For more information, see Use a serverless resource group. |
— |
|
Language |
The programming language. Select Python based on the language used to develop your Spark on MaxCompute job. |
— |
|
Main Python Resource |
Specifies the main Python resource file for the job. The resource file must be uploaded to DataWorks and committed. For more information, see Resource Management. |
|
|
Configuration Item |
Specifies the configuration items for submitting the job. Note the following:
|
|
|
Parameter |
You can add parameters as needed, separated by spaces. DataWorks supports scheduling parameters, which must be in the For information about the supported formats of scheduling parameter values, see Sources and expressions of scheduling parameters. |
|
|
Python Resources |
This can be used only when the development language is The resource files must be uploaded to DataWorks and committed. For more information, see Resource Management. |
|
|
File Resources |
Specifies the file resources for the job. |
|
|
Archive Resources |
Specifies the archive resources for the job. |
|
Procedure
-
Create a resource.
-
Find Resource Management in the left navigation bar of the Data Studio page and click Create. Create a Python resource of the MaxCompute Spark type and name it
spark_is_number.py. For more information, see Resource Management. The code is as follows:# -*- coding: utf-8 -*- import sys from pyspark.sql import SparkSession try: # for python 2 reload(sys) sys.setdefaultencoding('utf8') except: # python 3 not needed pass if __name__ == '__main__': spark = SparkSession.builder\ .appName("spark sql")\ .config("spark.sql.broadcastTimeout", 20 * 60)\ .config("spark.sql.crossJoin.enabled", True)\ .config("odps.exec.dynamic.partition.mode", "nonstrict")\ .config("spark.sql.catalogImplementation", "odps")\ .getOrCreate() def is_number(s): try: float(s) return True except ValueError: pass try: import unicodedata unicodedata.numeric(s) return True except (TypeError, ValueError): pass return False print(is_number('foo')) print(is_number('1')) print(is_number('1.3')) print(is_number('-1.37')) print(is_number('1e3')) -
Save the resource.
-
-
In the created MaxCompute Spark node, configure the node parameters and the scheduling parameters. For more information, see Parameters.
-
To run the job on a schedule, configure its scheduling properties based on your business requirements. For more information, see Node scheduling configuration.
-
After you configure the node job, deploy the node. For more information, see Deploy nodes/workflows.
-
After the job is deployed, you can go to Operation Center to view the run status of the periodic job. For more information, see Get started with Operation Center.
Note-
MaxCompute Spark nodes do not have a run entry point in Data Studio. You must run Spark jobs in Operation Center in the development environment.
-
After the data backfill instance runs successfully, open the tracking URL in the instance run log to view the result.
-
References
-
For more information about developing Spark on MaxCompute jobs for other use cases, see the following topics:
-
Spark FAQ: Find solutions to common Spark execution issues to accelerate troubleshooting. For more information, see Spark FAQ.
-
Spark job diagnosis: MaxCompute provides the Logview tool and the Spark web UI. Use job logs to verify that a job is submitted and running correctly. For more information, see Spark job diagnosis.