DataWorks provides the EMR PySpark node, which lets you collaboratively write Python business logic and spark-submit commands in a unified development interface. This node integrates seamlessly with both Alibaba Cloud EMR semi-managed clusters and the fully-managed EMR Serverless Spark.
Prerequisites
Compute resource limitations: The EMR PySpark node supports two types of compute resources:
EMR compute resources (semi-managed clusters): Only DataLake clusters and Custom clusters are supported.
EMR Serverless Spark compute resources (fully-managed clusters).
ImportantIf you need to run an EMR PySpark node on a semi-managed EMR cluster, such as a DataLake cluster or Custom cluster, submit a ticket. In the ticket, specify your cluster type, EMR version, and use case. We will evaluate the feasibility and provide the necessary support.
Resource group limitations: Only Serverless resource groups are supported.
Permission requirements: You must be an Alibaba Cloud account or a RAM user with the Dev or Workspace Administrator role in the workspace. For more information about adding members, see Add workspace members.
Create a node
Log on to the DataWorks console. Switch to the target region, click in the left-side navigation pane, select the desired workspace from the drop-down list, and then click Data Analytics.
On the Data Development (DataStudio) page, create an EMR PySpark node.
Configure the node Path and Name. In this example, the node name is emr_pyspark_test.
Develop the node
The EMR PySpark node uses a dual-pane editor:
The upper pane is the Python code editor, where you write core business logic. It supports referencing uploaded resource files, such as
.pymodules.The lower pane is the Submit command editor, where you write the spark-submit command for the EMR cluster.
This example shows how to use the Monte Carlo method on an EMR PySpark node to estimate the value of Pi (π) in a distributed manner.
Step 1: Upload resources
Upload your custom Python file (in this example, utils.py) to the Resource Management module in DataWorks. After you upload the file, you can reference this resource in your node. This Python file defines the core logic of the Monte Carlo sampling simulation within a single Spark task.
For detailed instructions on uploading resources, see EMR resources and functions. On the Resource Management page in DataStudio, click Create Resource, select
EMR Fileas the resource type, and configure the resource Name.Click Re-Upload to upload the example utils.py file.
Select the Storage path, Connection, and Resource Groups, then click Save.

Step 2: Write the Python code
In the Python code editor, write the following code. This example program estimates the value of Pi (π) by using parallel computing on a cluster. The program divides the computation into subtasks, distributes them to compute nodes, and then collects and aggregates the results to calculate the final estimate for Pi.
##@resource_reference{"utils.py"}
from pyspark.sql import SparkSession
from utils import estimate_pi_in_task
import sys
def main():
# Create a SparkSession
spark = SparkSession.builder \
.appName("EstimatePi") \
.getOrCreate()
sc = spark.sparkContext
# Total number of samples
total_samples = int(sys.argv[1])
num_partitions = ${test1}
# Number of samples per partition
samples_per_partition = total_samples // num_partitions
# Create an RDD where each partition runs estimate_pi_in_task once
rdd = sc.parallelize(range(num_partitions), num_partitions)
# Map each partition to run the sampling task
inside_counts = rdd.map(lambda _: estimate_pi_in_task(samples_per_partition))
# Aggregate the results from all partitions
total_inside = inside_counts.sum()
pi_estimate = 4.0 * total_inside / total_samples
print(f"Total samples: {total_samples}")
print(f"Samples within the circle: {total_inside}")
print(f"Estimated value of Pi: {pi_estimate:.6f}")
spark.stop()
if __name__ == "__main__":
main()You can only submit the entire Python file as a single Spark job. Running selected code snippets is not supported.
The following table describes the parameters used in the script.
Parameter | Type | Value | Description |
| Command-line argument | The value that follows the script name in the | Represents the total number of samples, for example, |
| Scheduling parameter | Dynamically replaced with the actual value by DataWorks at runtime or during scheduling. | Represents the number of partitions, for example, |
Step 3: Write the spark-submit command
In the Submit command editor, write the following code. This command uses the spark-submit tool to package and submit the Python application to an EMR cluster for execution.
The spark-submit command and its parameters differ significantly between EMR compute resources (semi-managed clusters) and EMR Serverless Spark compute resources (fully-managed clusters). The following table compares the command usage for both resource types.
Category | EMR compute resources (semi-managed cluster) | EMR Serverless Spark compute resources (fully-managed cluster) |
Code example | | |
Command integration | Based on the | Based on the Alibaba Cloud EMR Serverless |
Command reference | For more information about supported | For more information about |
Run the node
In the Running Configurations tab, configure the Compute Resource and Resource Group.
Parameter
Description
Compute resource
Select an associated EMR compute resource or EMR Serverless Spark compute resource. If no compute resource is available, you can select Create Compute Resource from the drop-down list.
Resource group
Select a resource group that is bound to the workspace.
Script parameters
You can define variables in the node content by using the
${ParameterName}format. Then, in the Value Used in This Run section, assign a value to each variable in the Script Parameters field. At runtime, the variable is replaced with this value.The parameter value is synchronized with the value set in the scheduling configuration.
NoteThe Value Used in This Run applies only to the current execution and takes precedence over the Parameter Value.
In the toolbar at the top of the node editor, click Run. The system assembles the complete Python script, including referenced resources, submits it to the EMR cluster by using
spark-submit, and then returns the execution logs and results.
Next steps
Configure node scheduling: If you need to run a node periodically, configure its Scheduling Policy in the Scheduling Settings panel on the right.
Publish a node: To run a task in the production environment, click the
icon to publish the node. A node runs on schedule only after it is published to the production environment.Task O&M: After a task is published, you can monitor the status of its periodic runs in the Operation Center. For more information, see Get started with Operation Center.