MaxCompute Spark node

Updated at:

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.

Note

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:

  1. 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.

  2. 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.

  3. 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.

Note

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.

  1. Prepare a Python environment locally.

    Refer to PySpark Python versions and dependency support to configure the required Python environment.

  2. 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.

Note

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.

app jar or Python file

Configuration Item

Specifies the configuration items for submitting the job. Note the following:

  • You do not need to configure spark.hadoop.odps.access.id, spark.hadoop.odps.access.key, and spark.hadoop.odps.end.point because they default to the values of the MaxCompute project. If you have special requirements, you can explicitly configure them to override the default values.

  • You do not need to upload the spark-defaults.conf file. Instead, you need to add the configurations from spark-defaults.conf one by one to the MaxCompute Spark node configurations, such as the number of Executors, memory size, and the spark.hadoop.odps.runtime.end.point configuration.

--conf PROP=VALUE

Main Class

Configure the main class name. This parameter is required when the development language is Java/Scala.

--class CLASS_NAME

Parameter

You can add parameters as needed and separate multiple parameters with a space. DataWorks supports scheduling parameters. The format for the Parameter is ${variable_name}. After the configuration is complete, you must assign a value to the variable in the Scheduling Parameters section of the Scheduling Settings pane in the right navigation bar.

For information about the supported formats of scheduling parameter values, see Sources and expressions of scheduling parameters.

[app arguments]

JAR Resources

This is supported only when the programming language is Java/Scala.

The resource files must be uploaded to DataWorks and committed. For more information, see Resource Management.

Resource command:

--jars JARS

File Resources

Specifies the file resources for the job.

--files FILES

Archive Resources

Specifies the archive resources for the job. Only .zip archives are supported.

--archives ARCHIVES

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.

app jar or Python file

Configuration Item

Specifies the configuration items for submitting the job. Note the following:

  • You do not need to configure spark.hadoop.odps.access.id, spark.hadoop.odps.access.key, and spark.hadoop.odps.end.point because they default to the values of the MaxCompute project (but can be explicitly configured for special reasons to override the default values).

  • You do not need to upload the spark-defaults.conf file. Instead, you need to add each configuration from spark-defaults.conf to the configuration settings for the MaxCompute Spark node, such as the number of Executors, memory size, and the spark.hadoop.odps.runtime.end.point configuration.

--conf PROP=VALUE

Parameter

You can add parameters as needed, separated by spaces. DataWorks supports scheduling parameters, which must be in the ${variable_name} format in the Parameter field. After you configure the parameters, go to the Scheduling Settings pane in the right navigation bar and assign a value to each variable in the Scheduling Parameters section.

For information about the supported formats of scheduling parameter values, see Sources and expressions of scheduling parameters.

[app arguments]

Python Resources

This can be used only when the development language is Python.

The resource files must be uploaded to DataWorks and committed. For more information, see Resource Management.

--py-files PY_FILES

File Resources

Specifies the file resources for the job.

--files FILES

Archive Resources

Specifies the archive resources for the job.

--archives ARCHIVES

Procedure

  1. Create a resource.

    1. 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'))
    2. Save the resource.

  2. In the created MaxCompute Spark node, configure the node parameters and the scheduling parameters. For more information, see Parameters.

  3. To run the job on a schedule, configure its scheduling properties based on your business requirements. For more information, see Node scheduling configuration.

  4. After you configure the node job, deploy the node. For more information, see Deploy nodes/workflows.

  5. 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