Sub topic 7
Reading files and tables
This chapter assumes you can attach a notebook to compute, know that spark is already defined, and understand that a DataFrame has a schema. You will read files and tables into DataFrames and avoid the common mistakes.
The reader pattern
Every read starts from spark.read. You choose a format, add options, optionally set a schema, and call load with a path. Nothing is processed yet. Spark only builds a plan, as described in the chapter on lazy evaluation.
raw = (
spark.read
.format("csv")
.option("header", True)
.schema("order_id INT, customer STRING, order_date DATE, amount DOUBLE")
.load("/Volumes/main/default/raw/orders.csv") # replace with a path you can read
)Each part has a job:
formatnames the file type, such ascsv,json,parquetordelta.optionchanges reader behavior. For CSV,headertells Spark that the first line holds column names.schemagives column names and types in a DDL string.loadtakes the location. On Unity Catalog workspaces, files usually live in a volume, with a path like/Volumes/<catalog>/<schema>/<volume>/<file>.
Why set the schema yourself
If you leave out .schema(...) and set inferSchema to true, Spark makes an extra pass over the data to guess types. That costs time on large files. The guess can also change when the data changes. A column that held only whole numbers last week may be read as a string this week if one bad value appears.
An explicit schema avoids both problems. Types stay the same on every run.
Reading tables
Tables registered in Unity Catalog need no path or format. Use the three-level name catalog.schema.table.
orders = spark.read.table("main.default.customer_summary")spark.table("main.default.customer_summary") does the same thing. The table already stores its schema, so you do not declare one.
Choosing a source
| Source | Schema comes from | Typical use |
|---|---|---|
| CSV | You (or costly inference) | Raw exports from other systems |
| JSON | Inference or you | Event logs and API dumps |
| Parquet | Stored in the file | Columnar data from other tools |
| Delta table | Stored in the table | Data you manage in Databricks |
Formats that store their own schema, such as Parquet and Delta, are easier and safer to read than CSV.
Common errors
- Path not found. Check the path spelling and confirm you have access to the volume or table.
- All columns are strings with names like
_c0. You forgotheaderfor a CSV file. - Unexpected nulls. The declared type does not match the data. Compare the schema with a sample of the file.
- Slow first read.
inferSchematriggers an extra scan. Declare the schema instead.
Exercise
A CSV file has a header row. You read it with a schema but without .option("header", True). What happens to the header line, and how do you fix it?
Show answer
Spark treats the header line as a data row. With numeric column types, its text values will not parse, so the fields typically become null. Add .option("header", True) so the first line is used as column names and skipped as data.