Arman Pouyaei Start a conversation
Lesson 21  ·  Oct 30, 2026
Data Engineering Fundamentals

Read it in, write it out: read/write pipelines

Every pipeline so far has ended in show(). Real pipelines end in write(). The full shape is: read raw data in, transform it, write the clean result out — and the next job reads that clean output as its own source. Four lines do the whole thing.

The code

from pyspark.sql import SparkSession

spark = SparkSession.builder.appName("storm-pipeline").getOrCreate()

# Read: raw storm readings land as CSV in the lake.
raw = spark.read.csv("lake/raw/storm_readings.csv", header=True, inferSchema=True)

# Transform: drop rows with no temperature reading.
clean = raw.filter(raw.temp_c.isNotNull())

# Write: Parquet, partitioned by region — one folder per region.
clean.write.mode("overwrite").partitionBy("region").parquet("lake/clean/storm_readings/")

# Read it back: Parquet stores the schema, so no header/infer options needed.
back = spark.read.parquet("lake/clean/storm_readings/")
back.show()

spark.read is the reader: .csv(), .json(), .parquet(), with options like header=True and inferSchema=True. CSV files carry no schema of their own, so you have to tell Spark how to read them; Parquet carries its schema with it, so spark.read.parquet(...) needs no options at all (and Parquet is Spark's default format). df.write is the writer: .parquet(), .csv(), .json(), plus .mode() for what happens if the destination already exists — overwrite replaces it, append adds to it, errorIfExists (the default) refuses, ignore skips silently. And .partitionBy("region") writes one subfolder per region — the partitioning idea from Lesson 15, built into the write.

In practice

One more thing that matters: write is an action. Unlike filter or join, which only build a plan, writing actually executes the whole chain — it is the show() of production pipelines. That is why pipelines end in write, not show: the output has to land somewhere the next step can use.

In your world: nightly storm-sensor CSVs land in a raw zone, a job cleans them and writes partitioned Parquet to a clean zone, and downstream jobs — or DuckDB — read that clean Parquet as their source. Yesterday's join + aggregate fits in the middle: read raw, join, aggregate, write summary. Same four-step skeleton every time, whatever the transform in the middle is.

Python bridge: this is pandas read_csv and to_parquet — df = pd.read_csv("storm_readings.csv"), then df.to_parquet("clean/"). One difference to expect: pandas writes a single file, Spark writes a folder of part files (one per partition), plus a _SUCCESS marker. That is normal — readers treat the folder as one table.

Further reading

PySpark and Parquet: Elegant Python DataFrames and SQL — CodeSolid (a beginner-friendly walkthrough of loading Parquet with spark.read and saving it back with df.write, including why a write produces a folder, not a file)

Try it

Practice question: starting from the clean Parquet written above, write code that reads lake/clean/storm_readings/ back in, keeps only rows where region is "Northeast", and writes the result as Parquet to lake/clean/storm_readings_northeast/ with mode overwrite.

Show answer
back = spark.read.parquet("lake/clean/storm_readings/")
northeast = back.filter(back.region == "Northeast")
northeast.write.mode("overwrite").parquet("lake/clean/storm_readings_northeast/")

Result: a new folder in the lake, holding only Northeast rows:

lake/clean/storm_readings_northeast/
  part-00000-....parquet
  _SUCCESS

No region= subfolders this time: we filtered down to a single region and did not call partitionBy, so Spark just writes part files. The _SUCCESS file is Spark's way of saying the write finished cleanly.

← All posts