Module 6: Spark DataFrames and Pipelines
Overview
Most day-to-day Spark work uses the structured APIs: DataFrames and Spark SQL. This module explains why they replaced RDDs for data science, then works through data wrangling on a household survey dataset: creating DataFrames, defining schemas, working with columns and expressions, filtering, grouping and aggregating, user-defined functions, and moving between Spark, SQL and pandas.
Estimated time: about 4 hours (1 hour lecture, 3 hours hands-on)
Learning objectives
By the end of this module, you should be able to:
- Compare RDDs with the structured APIs, and choose between RDDs, DataFrames and Spark SQL for a given task.
- Create Spark DataFrames from files and Python objects, and define schemas with Spark’s data types.
- Select, add, rename and filter columns using expressions and Spark’s built-in functions.
- Aggregate data with
groupBy, and query the same data with Spark SQL. - Write user-defined functions, and convert between Spark DataFrames, pandas DataFrames and RDDs.
Before class
Make sure PySpark works on your machine: follow Python environment setup and run the check-setup notebook.
Prerequisites: Module 5, especially transformations, actions and lazy evaluation. Experience with pandas or SQL helps.
The exercises use the household dataset; see data/README.md for how to get it.
Lecture slides
Hands-on
Spark notebooks fail with UnsupportedClassVersionError or Java gateway process exited when Java is missing or too old. PySpark 4 needs Java 17 or later. See Python environment setup → Troubleshooting.
Description of the Household Dataset
The census-style household dataset used in this module’s exercises.
Spark Structured APIs
Create DataFrames, define schemas, and compare loading with and without a schema.
Working with Spark DataFrames
Schemas, manipulating columns, filtering, grouping, user-defined functions, and converting to pandas and RDDs.
Exercises
To be added.
Further reading
- Spark SQL, DataFrames and Datasets guide
- PySpark SQL API reference, including data types and built-in functions
- RDD programming guide, for when you need to drop down to RDDs
- Damji et al., Learning Spark, 2nd edition (O’Reilly), chapters 3–5
- Pandas API on Spark: pandas syntax running on Spark