Running PySpark locally & in notebooks

Apache Spark · Spark Fundamentals

Theory

You can learn and test Spark on a laptop. Install Java 17 or 21 and then pip install pyspark, which bundles the Spark engine itself, so no separate download or cluster is needed. In your script or Jupyter notebook you create a SparkSession with master('local[*]'), which runs the driver and executor threads inside one local process and uses all CPU cores (local[2] would use two).

The same code later runs on a cluster: you only change the master or let spark-submit, Databricks, EMR or Kubernetes provide it. Useful local settings are a small spark.sql.shuffle.partitions (the default 200 is meant for clusters) and the Spark UI at http://localhost:4040 to watch jobs. For example, Priya runs her orders notebook in Jupyter with local[*] on a 1 GB sample, then submits the same file with spark-submit --master yarn on the full data.

Why we use it

A local setup gives fast feedback for learning, unit tests and debugging without paying for a cluster. Because the API is identical, code you test locally behaves the same on a real cluster.

When to use it

Use local mode for learning, development, CI tests and small samples. Use a real cluster (or Databricks, EMR, Dataproc, Kubernetes) when data is large or jobs must run on a schedule. In notebooks, create the session once and reuse it.

How it works

  1. Install Java 17 or 21
  2. pip install pyspark (and jupyter if needed)
  3. Create SparkSession with master('local[*]')
  4. Develop and test on a small sample
  5. Ready for full data? Yes: spark-submit to YARN, Kubernetes or a managed platform. No: Keep iterating locally.
  6. Call spark.stop() when finished

Syntax

pip install pyspark            # needs Java 17 or 21 on PATH / JAVA_HOME
pyspark                        # interactive shell with 'spark' ready
spark-submit --master local[4] job.py
SparkSession.builder.master("local[*]").appName("dev").getOrCreate()

Example

from pyspark.sql import SparkSession, functions as F

spark = (SparkSession.builder.master("local[1]")
         .config("spark.ui.enabled", "false")
         .getOrCreate())

orders = spark.createDataFrame([
    (101, 1, "North", "Laptop", 1, 55000.0, "2026-01-05"),
    (102, 2, "West",  "Phone",  2, 30000.0, "2026-01-06"),
    (103, 1, "North", "Mouse",  3,  1500.0, "2026-01-06"),
    (104, 3, "South", "Laptop", 1, 52000.0, "2026-01-07"),
    (105, 4, "West",  "Tablet", 1, 25000.0, "2026-01-08"),
    (106, 2, "East",  "Phone",  1, 15000.0, "2026-01-08"),
    (107, 5, "North", "Tablet", 2, 48000.0, "2026-01-09"),
    (108, 3, "South", "Mouse",  4,  2000.0, "2026-01-10"),
], "order_id INT, customer_id INT, region STRING, product STRING, "
   "qty INT, amount DOUBLE, order_date STRING"
).withColumn("order_date", F.to_date("order_date"))

# On a laptop you would usually write .master("local[*]") to use all cores.
spark.conf.set("spark.sql.shuffle.partitions", "4")   # small for local work

print("master:", spark.sparkContext.master)
print("default parallelism:", spark.sparkContext.defaultParallelism)
print("shuffle partitions:", spark.conf.get("spark.sql.shuffle.partitions"))

orders.groupBy("region").count().orderBy("region").show()
spark.stop()   # free resources when the script ends

# Output:
# master: local[1]
# default parallelism: 1
# shuffle partitions: 4
# +------+-----+
# |region|count|
# +------+-----+
# |  East|    1|
# | North|    3|
# | South|    2|
# |  West|    2|
# +------+-----+

Key points

Apache Spark topics

Spark Fundamentals

Spark Architecture

RDDs

DataFrames

Aggregations & Joins

Spark SQL

Window Functions

Cleaning & Complex Types

UDFs

Performance Tuning

Structured Streaming

MLlib & Ecosystem

Interview Prep

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master Apache Spark

PySpark and Spark SQL from the ground up: DataFrames, joins, window functions, partitions and performance tuning, Structured Streaming and more. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice PySpark Write real PySpark — DataFrames, joins, window functions and Spark SQL — in a live editor with hidden tests and instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Spark  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

{{ sec.h }}

{{ sec.body }}

{{ sec.body }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
On screen — where to click

{{ rich.ui.title }}

{{ u.text }}
  1. {{ st.n }}{{ st.text }}
{{ rich.ui.title }}
{{ u.text }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
Want to run this yourself? Try the same API on Practice PySpark Online right in your browser, or run it locally after pip install pyspark (needs Java 17+), or paste it into a Databricks Free Edition notebook.
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic