View a markdown version of this page

Migrating Apache Spark - Amazon EMR Migration Guide

Migrating Apache Spark

Apache Spark applications are a common workload on Amazon EMR. Because EMR clusters can be configured to use specific instance types and can easily scale out, Apache Spark is effective for optimizing compute resources for both performance and cost.

Use cases for migrating Apache Spark

There are several use cases to consider. In many cases, the existing environment is one large cluster with a specific number of resources dedicated to Spark jobs. With Amazon EMR, you can continue to use one large shared cluster, use On-Demand Instance clusters to isolate resources on a per-job basis, or use Amazon EMR Serverless to run Spark jobs without managing clusters. The On-Demand approach lets you take advantage of different instance types and ensure Spark fully uses each cluster's resources, but requires more planning and automation around cluster creation.

Shared cluster

Be aware of how many concurrent jobs you expect to run. By default, Amazon EMR configures executors to use the maximum resources on each node through the maximizeResourceAllocation property. On a shared cluster, you will likely need to configure Spark cores, memory, and executors. For more information, see Best practices for successfully managing memory for Apache Spark applications on Amazon EMR on the AWS Big Data Blog. The shared cluster is appropriate for interactive use, such as SageMaker Unified Studio or EMR Studio with JupyterLab notebooks.

Note

The maximizeResourceAllocation setting is deprecated in Amazon EMR 7.x. Instead, use Spark's dynamic resource allocation or configure executor resources explicitly.

When you use a shared cluster, use the dynamic allocation setting in Amazon EMR to automatically calculate the default executor size and return resources to the cluster when they are no longer used. Dynamic allocation is enabled by default on Amazon EMR.

Dedicated clusters per job

Using a separate cluster for each Spark job benefits scheduled jobs. It isolates the job to prevent resource contention, allows optimization for CPU-, GPU-, or memory-intensive workloads, and ensures you pay only for the resources used during the job. The maximizeResourceAllocation setting helps dedicate the entire cluster's resources to the job. For more information, see Using maximizeResourceAllocation in the Amazon EMR Release Guide.

Amazon EMR Serverless

For scheduled or event-driven Spark jobs, Amazon EMR Serverless removes the need to provision, configure, or manage clusters. You submit Spark jobs directly to a serverless application, and Amazon EMR automatically provisions resources, scales them as the job progresses, and releases them when the job completes. This approach is well suited for ETL pipelines, batch processing, and any workload where cluster lifecycle management adds overhead without value.

The following example submits a Spark job to an Amazon EMR Serverless application:

# Submit a Spark job to EMR Serverless aws emr-serverless start-job-run \ --application-id <<application-id>> \ --execution-role-arn arn:aws:iam::123456789:role/EMRServerlessRole \ --job-driver '{ "sparkSubmit": { "entryPoint": "s3://my-bucket/etl/spark-job.py", "entryPointArguments": ["--input", "s3://my-bucket/raw/", "--output", "s3://my-bucket/processed/"], "sparkSubmitParameters": "--conf spark.executor.cores=4 --conf spark.executor.memory=16g --conf spark.dynamicAllocation.enabled=true" } }'

Amazon EMR Spark runtime

The Amazon EMR runtime for Apache Spark is a performance-optimized runtime that is active by default on EMR clusters. It provides 100% API compatibility, so your existing Spark applications run faster without code changes. Based on TPC-DS benchmarks at 3 TB scale, the EMR runtime runs Spark workloads up to 4.5 times faster than open-source Apache Spark 3.5.1. It also provides 2.8 times better price performance on current-generation instance types with data in Amazon S3, which saves compute costs with no application changes.

Optimize cost with Amazon EC2 Spot Instances

A common way to decrease cost is to use Amazon EC2 Spot Instances. Amazon EMR release 5.9.0 and later includes built-in node decommissioning, so Spark gracefully handles node termination during manual resizes or automatic scaling. Starting with Amazon EMR 5.11.0, the spark.decommissioning.timeout.threshold setting improves Spark resiliency on Spot Instances, so use Amazon EMR 5.11.0 or later with Spot Instances.

When you use Spot Instances, use Instance Fleets with multiple instance types across multiple Availability Zones to maximize Spot capacity. Configure the Spot allocation strategy as capacity-optimized to reduce interruptions.

Use Instance Fleets

Instance fleets let you specify target capacity based on a set of units, such as cores, memory, or an arbitrary reference. This is useful when your Spark job requires a known amount of resources and you want to mix capacity across EC2 instance type and Availability Zone. You select a VPC network and EC2 subnets, and the feature searches Availability Zones until it finds the desired capacity. Instance fleets support Managed Scaling, which continuously evaluates utilization and adds or removes instances. For dynamic workloads that require fine-grained autoscaling control, use Instance Groups with custom scaling policies. For more information, see Cluster Configuration Guidelines and Best Practices in the Amazon EMR Management Guide.

Apache Spark file write performance

In Amazon EMR 5.14.0, the default FileOutputCommitter algorithm was updated to version 2 instead of version 1, which reduces renames and improves performance. We recommend that Spark applications migrating to Amazon EMR use the most recent EMR version.

Starting with Amazon EMR 5.20.0, the EMRFS S3-optimized committer is enabled by default. This is an alternative output committer optimized for writing Parquet files to Amazon S3 when using EMRFS. It improves performance and reliability by avoiding costly rename operations.

For Amazon EMR 5.19.0, the EMRFS S3-optimized committer is available but not enabled by default. To enable it, set spark.sql.parquet.fs.optimized.committer.optimization-enabled to true through the spark-defaults classification at cluster creation, or at runtime with --conf.

Note

Starting with Amazon EMR 7.10, EMR S3A replaces EMRFS as the default Amazon S3 connector.

AWS Glue Data Catalog

For Amazon EMR 5.8.0 and later, Spark supports using the AWS Glue Data Catalog as the metastore for Spark SQL. If you plan to migrate to Spark on Amazon EMR, first determine whether you can migrate your existing metastore to AWS Glue. Benefits include a managed metadata catalog that integrates with Apache Trino and Apache Hive on Amazon EMR, Amazon Athena, Amazon Redshift Spectrum, Amazon SageMaker, and Amazon Data Firehose.

With AWS Glue crawlers, the Data Catalog can generate schema and partitions and provide fine-grained access control to databases and tables. It also supports Apache Iceberg, Apache Hudi, and Delta Lake table formats natively, which makes it a central metadata store for multi-engine lakehouse architectures.

Amazon EMR agents for Spark

Amazon EMR provides two conversational AI agents, the Spark Upgrade Agent and the Spark Troubleshooting Agent, that accelerate Spark version upgrades and simplify failure diagnosis. The Upgrade Agent converts complex upgrades, such as Spark 2.4 to 3.5 or 4.x, from months to weeks through automated code analysis and validation. The Troubleshooting Agent reduces debugging time from hours to minutes by analyzing logs, metrics, and configurations. Both agents are available at no additional cost with Amazon EMR. For full details, including architecture, setup, and migration workflow integration, see Spark Agents.

Other ways of troubleshooting Apache Spark jobs on an Amazon EMR cluster

To debug common issues, view the history and log files:

  • Spark web UIs – Access the Spark HistoryServer UI at port 18080 of the EMR cluster's primary node. For Amazon EMR Serverless, the Spark History Server is available directly on the EMR Studio console without SSH or port forwarding. For more information, see Accessing the Spark Web UI.

  • YARN applications, including Spark jobs – Access the Application history tab in the Amazon EMR console. Application history is retained for up to 30 days, including task and stage completion details. For more information, see View Application History.

  • Log files – By default, EMR clusters launched through the console automatically archive log files to Amazon S3. You can find raw container logs in Amazon S3 and view them while the cluster is active and after termination. For Amazon EMR Serverless, logs can be directed to both Amazon S3 and Amazon CloudWatch Logs. For more information, see View Log Files.