beginner
Running Your First Spark Application
10 min readLast updated: 2026-07-08
Overview
Now that your local environment is configured, let's write, build, and execute your first Apache Spark application using Python, Scala, and SQL.
What You Will Learn
In this lesson, you will learn:
- DataFrame Lifecycle: Creating a DataFrame from raw inputs and running transformations.
- Python, Scala, and SQL: Writing the same query across all supported APIs.
- Spark Submit: Deploying and launching your application locally.
Detailed Concept Explanation
Building the Data Pipeline
Every Spark application follows the same three basic steps:
- Initialize: Create a
SparkSession(the entry point to Spark). - Transform: Define operations on your data (like filtering, sorting, or aggregating).
- Action: Trigger execution and display or write the results (e.g. using
.show()).
Code Examples
Input Dataset Preview
Before executing the code examples below, let's preview the input dataset schema and rows:
| product | category | price |
|---|---|---|
| Laptop | Electronics | 1200 |
| Phone | Electronics | 800 |
| Book | Office | 20 |
Python (PySpark) Implementation
Save the code below as app.py:
python
from pyspark.sql import SparkSession
from pyspark.sql.functions import col
# 1. Initialize SparkSession
spark = SparkSession.builder.appName("FirstApp").getOrCreate()
# 2. Load mock dataset
data = [("Laptop", "Electronics", 1200),
("Phone", "Electronics", 800),
("Book", "Office", 20)]
df = spark.createDataFrame(data, ["product", "category", "price"])
# 3. Filter for products costing more than 500
expensive_products = df.filter(col("price") > 500).select("product", "price")
# 4. Trigger action to show results
expensive_products.show()
# 5. Stop session
spark.stop()
Expected Output
Spark executes the query in memory and prints:
text
+-------+-----+
|product|price|
+-------+-----+
| Laptop| 1200|
| Phone| 800|
+-------+-----+
Execution Plan Diagram (Python & Scala)
Execution Plan Diagram
SparkSession.builder
createDataFrame
filter(price > 500)
select(product
price)
show()
Scala Implementation
Here is the same query written in Scala:
scala
import org.apache.spark.sql.SparkSession
val spark = SparkSession.builder().appName("FirstAppScala").getOrCreate()
import spark.implicits._
val df = Seq(
("Laptop", "Electronics", 1200),
("Phone", "Electronics", 800),
("Book", "Office", 20)
).toDF("product", "category", "price")
val expensive = df.filter($"price" > 500).select("product", "price")
expensive.show()
spark.stop()
Expected Output
Scala executes the operations on the JVM and yields:
text
+-------+-----+
|product|price|
+-------+-----+
| Laptop| 1200|
| Phone| 800|
+-------+-----+
SQL Implementation
Spark SQL allows you to write standard SQL queries on your DataFrames:
sql
-- Register table
-- df.createOrReplaceTempView("products")
SELECT product, price
FROM products
WHERE price > 500;
Expected Output
Executing this SQL query returns:
| product | price |
|---|---|
| Laptop | 1200 |
| Phone | 800 |
Common Mistakes
- Forgetting to Stop SparkSession: Keeping sessions open prevents executors from shutting down, locking cluster memory.
Best Practices
- Submit locally: Test your application on your computer using local execution before submitting it to YARN or Kubernetes:
spark-submit --master local[*] app.py
Interview Perspective
What are the essential steps in every Spark application's developer pipeline?
Every Spark app follows a clear sequence:
- Initialization: Instantiating a
SparkSessionto establish connections. - Data Ingestion: Loading data from files (CSV, Parquet) or tables into a DataFrame.
- Transformations: Defining logical operations like filters or selects (which run lazily).
- Action: Triggering physical processing via actions like
show()orwrite(). - Clean up: Invoking
spark.stop()to free up cluster resources.