DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Designing AI for Long-Running Tasks: Looking Beyond More Context
  • How Long-Running ECS Tasks Survive Rolling Deployments Without Duplicating Output
  • MCP for Enterprise Tasks: Making the Rare Frequent Enough to Master
  • Idempotent Output Keying for Long-Running Tasks During Rolling Deployments

Trending

  • Kafka With Spring Boot: Introduction
  • Going All In On the JVM 25 Virtual Threads
  • Moving 900+ GB of Prometheus Without a Cutover Weekend
  • Why Spark Executors Leave Cores Idle on YARN: A Packing Model for Executor Sizing

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

How Spark Internally Executes a Program

In this article, I will try to explain how Spark works internally and what the components of execution are: jobs, tasks, and stages.

By 
Shubham Agarwal
·
Apr. 25, 18 · Analysis
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
65.5K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Hello, everyone! In my previous article, I explained the difference between RDD, DF, and DS. You can find this article here.

In this article, I will try to explain how Spark works internally and what the components of execution are: jobs, tasks, and stages.

As we all know, Spark gives us two operations for performing any problem.

When we do a transformation on any RDD, it gives us a new RDD. But it does not start the execution of those transformations. The execution is performed only when an action is performed on the new RDD and gives us a final result.

So once you perform any action on an RDD, Spark context gives your program to the driver.

The driver creates the DAG (directed acyclic graph) or execution plan (job) for your program. Once the DAG is created, the driver divides this DAG into a number of stages. These stages are then divided into smaller tasks and all the tasks are given to the executors for execution.

The Spark driver is responsible for converting a user program into units of physical execution called tasks. At a high level, all Spark programs follow the same structure. They create RDDs from some input, derive new RDDs from those using transformations, and perform actions to collect or save data. A Spark program implicitly creates a logical directed acyclic graph (DAG) of operations.

When the driver runs, it converts this logical graph into a physical execution plan.

So, let's take an example of word count for better understanding:

val rdd = sc.textFile("address of your file")

rdd.flatMap(_.split(" ")).map(x=>(x,1)).reduceByKey(_ + _).collect

Here you can see that collect is an action that will collect all data and give a final result. As explained above, when I perform the collect action, the Spark driver creates a DAG.

In the image above, you can see that one job is created and executed successfully. Now, let's have a look at DAG and its stages.

Here, you can see that Spark created the DAG for the program written above and divided the DAG into two stages.

In this DAG, you can see a clear picture of the program. First, the text file is read. Then, the transformations like map and flatMap are applied. Finally, reduceBykey is executed.

But why did Spark divided this program into two stages? Why not more than two or less than two? Basically, it depends on shuffling, i.e. whenever you perform any transformation where Spark needs to shuffle the data by communicating to the other partitions, it creates other stages for such transformations. And the transformation does not require the shuffling of your data; it creates a single stage for it.

Now, let's have a look at how many tasks have been created by Spark:

As I mentioned earlier, the Spark driver divides DAG stages into tasks. Here, you can see that each stage is divided into two tasks.

But why did Spark divide only two tasks for each stage? It depends on your number of partitions.

In this program, we have only two partitions, so each stage is divided into two tasks. And a single task runs on a single partition. The number of tasks for a job is:

( no of your stages * no of your partitions )

Now, I think you may have a clear picture of how Spark works internally.

Task (computing)

Published at DZone with permission of Shubham Agarwal. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Designing AI for Long-Running Tasks: Looking Beyond More Context
  • How Long-Running ECS Tasks Survive Rolling Deployments Without Duplicating Output
  • MCP for Enterprise Tasks: Making the Rare Frequent Enough to Master
  • Idempotent Output Keying for Long-Running Tasks During Rolling Deployments

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook