Reading and Writing: Files, Save Modes, JDBC
Databricks Spark Developer Associate · Lesson 6 of 18 · 42:26
YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗
Learning with these videos? Subscribe for new courses and exam-style questions.
Subscribe on YouTube (opens in a new tab)In this lesson
Prepare for the Databricks Certified Associate Developer for Apache Spark exam. In this lesson: the DataFrameReader and DataFrameWriter, CSV, JSON, text, and Parquet options, save modes and partitionBy, and parallel JDBC reads and writes. Includes three original practice questions.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
Three sources every night
Every night, Coralpeak loads three sources: game events as JSON files, partner reports as CSV files, and purchases from a PostgreSQL database. Then it writes a clean table for the analysts, split by day, so that queries on one day stay fast. This lesson covers the reader and the writer, file options, save modes with partitionBy, and JDBC.
Reader and writer
You read data with spark.read, which returns a DataFrame reader: you name a format, add options and, if you know it, a schema, then call load with a path. Shortcuts like json, csv, parquet, orc, or text combine the format and the load in one call. Without a format, Spark uses its default source, which is Parquet in Apache Spark, and Delta on Databricks. Writing mirrors reading: the write property of a DataFrame returns a writer, where you set a format, a mode, and options, then call save with a path, or saveAsTable with a table name.
File options
For CSV files, header is false by default, so the first line is read as data unless you set header to true. inferSchema is false too, so every column is a string, and turning it on costs an extra pass over the data, which a schema that you pass yourself avoids. The mode option decides what happens to malformed rows: permissive, the default, keeps them with nulls, drop malformed removes them, and fail fast stops at the first one. JSON files hold one object per line by default, and multiLine reads files where one object spans several lines. And the text format reads each line as a row, in a single string column named value.
Save modes and partitionBy
The save mode decides what happens when data already exists: error if exists, the default, raises an error, append adds the new rows, overwrite replaces the existing data, and ignore silently does nothing. Save modes use no locking, and they are not atomic, so an overwrite deletes the old data before it writes the new data. partitionBy writes one folder per value of the columns you name, like one folder per event date, so that queries that filter on that column read only the matching folders. Choose low cardinality columns, like a date or a country, since a column with millions of values creates millions of tiny folders. And by default, an overwrite with partitionBy replaces all the partitions, while the dynamic partition overwrite mode replaces only the partitions present in the new data.
JDBC
The jdbc format reads a database table with a url, a dbtable or a query, and a user and a password, which belong in a secret, not in the code. By default, a JDBC read uses a single connection, and so a single partition. To read in parallel, set partitionColumn, lowerBound, upperBound, and numPartitions together, where the column is numeric, a date, or a timestamp. The bounds only decide how the range is split into partitions, not which rows are read, so rows outside the bounds still land in the first or the last partition. numPartitions also caps the number of connections at once, and on writes, batchsize sets the rows per insert, one thousand by default, while truncate makes an overwrite empty the table instead of dropping it.
Recap
Let's recap. spark.read takes a format, options, a schema, and a path, and the writer takes a format, a mode, and options. CSV has no header and no schema inference by default, the mode option handles malformed rows, and multiLine reads JSON objects that span several lines. The default save mode raises an error, the others are append, overwrite, and ignore, and none of them is atomic. partitionBy writes one folder per value of a low cardinality column, and overwrite replaces every partition, unless the dynamic mode is on. And JDBC reads in parallel with partitionColumn, lowerBound, upperBound, and numPartitions, where the bounds set the stride, not a filter.
Quick checks for this lesson
The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.
A developer calls spark.read.load(path) with no format, on Apache Spark with the default settings. Which format does Spark expect?
- JSON, the most common format for events
- CSV, with a header row
- Parquet, the default source
- Delta, the default everywhere
Show the answer
Parquet, the default source
Without a format, Spark uses its default data source, which is Parquet in Apache Spark. “JSON, the most common format for events” is wrong, since JSON must be named, with json or with format. “CSV, with a header row” is wrong, since CSV must be named too. And “Delta, the default everywhere” is wrong, since Delta is the default on Databricks, not in Apache Spark.
Coralpeak writes one day of events again with mode("overwrite") and partitionBy("event_date"), with the default settings. What happens to the folders of the other days?
- They are deleted: overwrite replaces all partitions
- They are kept, since only one day is written
- The write fails, since the folders exist
- Their rows are appended to the new day
Show the answer
They are deleted: overwrite replaces all partitions
In the default static mode, overwrite replaces the whole output, so the other days are deleted, unless the dynamic partition overwrite mode is set. “They are kept, since only one day is written” is what the dynamic mode does, and it is not the default. “The write fails, since the folders exist” is what error if exists does, not overwrite. And “Their rows are appended to the new day” is wrong, since overwrite never appends rows.
A JDBC read uses partitionColumn order_id, lowerBound 1, upperBound 1,000,000, and 8 partitions. The table holds 5 million orders. What happens to the orders above one million?
- They are filtered out of the DataFrame
- The read fails with an error
- They become a ninth partition
- They are read, in the last partition
Show the answer
They are read, in the last partition
The bounds only set the stride of the partitions, so every row is read, and the rows above the upper bound go to the last partition, which grows much bigger than the others. “They are filtered out of the DataFrame” is wrong, since the bounds are never a filter. “The read fails with an error” is wrong, since rows outside the bounds are allowed. And “They become a ninth partition” is wrong, since numPartitions fixes eight partitions, not nine.