VOL. 01
← The learning path
FUNDAMENTALS / LESSON 03

RDDs, DataFrames & Spark SQL

Choose the right abstraction for a data engineering pipeline.

THE IDEA TO KEEP

For structured ETL, start with DataFrames or SQL: the schema and expressions give Spark information it can optimize.

A schema is part of the contract

A DataFrame is organized into named, typed columns. Define the expected schema when ingestion needs to be predictable. Check nullability, malformed records, and type conversions explicitly instead of assuming that successful reading means clean data.

Where RDDs fit

RDDs expose lower-level distributed collections. They are useful for custom processing that does not fit structured expressions. Python DataFrames do not provide the Scala/Java typed Dataset API. In an interview, separate the concepts from language-specific API names.

SQL or DataFrame?

These are two ways of expressing structured computation through Spark SQL. Choose the form your team can maintain, then inspect the plan. Equivalent logic may produce equivalent plans; switching syntax alone is not a performance strategy.

Try it on a small dataset

Run this in a PySpark notebook with an existing spark session. Create a local session if needed. Examples are illustrative; this website does not execute Spark.

PYTHON / PYSPARK
orders = spark.createDataFrame(
    [("Pune", 500), ("Delhi", 700), ("Pune", 300)],
    "city string, amount long"
)
orders.createOrReplaceTempView("orders")
spark.sql("""
    SELECT city, SUM(amount) AS revenue
    FROM orders GROUP BY city ORDER BY city
""").show()
Expected result

Delhi: 700 · Pune: 800

Result reasoned through on the small input; not runtime-tested against a Spark cluster.

Watch the assumption

A DataFrame has no guaranteed row order unless you request ordering. Do not rely on the display order of a small test.

EXPLAIN IT BACK

Is Spark SQL slower than the DataFrame API?

Say your answer out loud before opening the notes.

Reveal the reasoning

Not inherently. Both use Spark SQL’s engine. Compare the optimized and physical plans for equivalent expressions, plus runtime metrics and UDF use.

Sources & verification

Core claims checked against Apache Spark 3.5.7 documentation and source references on 2026-09-13. Explanations, scenarios, and diagrams are original teaching material. Other releases and managed runtimes can differ.

Find a concept