PySpark is the Python API for Apache Spark, the distributed computing framework designed for large-scale data processing. With the release of Spark 4.0 — bringing ANSI SQL mode by default, the VARIANT data type, Python UDTFs, Spark Connect, and over 5,100 improvements — PySpark now offers an even richer set of tools for data scientists and engineers processing terabytes of data across clusters. Understanding the lazy evaluation model is critical: transformations build a logical plan, but nothing executes until an action is called, allowing Spark's Catalyst optimizer to generate the most efficient physical execution strategy. This approach makes PySpark both powerful for big data workloads and surprisingly accessible for those familiar with pandas-style operations.
What This Cheat Sheet Covers
This topic spans 31 focused tables and 314 indexed concepts, 135 flashcards. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
A jump-to index of every table row in this cheat sheet.
An interactive map of every table and concept in this topic.
Table 1: SparkSession Initialization
Every PySpark program starts here — the SparkSession is the single entry point that wires your code to the cluster. The builder pattern below lets you name the app, point it at a cluster manager, tune resources like executor memory, and (since Spark Connect) even connect to a remote cluster from a lightweight client.
| Method | Example | Description | |
|---|---|---|---|
spark = SparkSession.builder.appName("MyApp").getOrCreate() | • Entry point for creating a Spark application • initializes or retrieves an existing SparkSession | ||
spark = SparkSession.builder.config("spark.executor.memory", "4g").getOrCreate() | Sets Spark configuration properties like executor memory, parallelism, or shuffle partitions | ||
spark = SparkSession.builder.master("local[*]").getOrCreate() | • Defines cluster manager URL • local[*] uses all available cores on local machine | ||
spark = SparkSession.builder.appName("DataPipeline").getOrCreate() | Sets application name visible in Spark UI for monitoring and debugging | ||
spark = SparkSession.builder.getOrCreate() | Returns existing active session or creates new one if none exists | ||
spark = SparkSession.builder.enableHiveSupport().getOrCreate() | Enables Hive support for reading from and writing to Hive metastore tables | ||
spark = SparkSession.builder.remote("sc://localhost:15002").getOrCreate() | • Connects to a remote Spark cluster via Spark Connect • lightweight client (~1.5 MB via pip install pyspark-connect) | ||
spark = SparkSession.active | Returns the active SparkSession for the current thread without builder | ||
spark2 = spark.newSession() | Creates new session with separate SQLConf and temp views but shared SparkContext | ||
spark.stop() | Terminates SparkSession and releases all cluster resources |