Sub topic 9
Reading volume files with Spark and SQL
This chapter assumes you can open a Databricks notebook, write basic Python and SQL, and know that a volume file has a path of the form /Volumes/<catalog>/<schema>/<volume>/<file>. You also need READ VOLUME on the volume, plus USE CATALOG and USE SCHEMA on its parents.
One path, several readers
A volume path is a normal file path. Every tool that runs on Databricks compute can take it as is. You don't mount anything or add a dbfs: prefix.
The three readers in this chapter are:
- Spark DataFrame readers, which read in parallel across the cluster.
- Python's built-in
open(), which reads on the driver only. - SQL, through
LISTand theread_filestable-valued function.
Reading with Spark and Python
The next example reads one CSV file with Spark, then reads its first line with plain Python.
path = "/Volumes/main/default/landing/sales.csv"
df = spark.read.csv(path, header=True, inferSchema=True)
df.show(5)
with open(path) as f:
print(f.readline())header=True uses the first row as column names. inferSchema=True makes Spark scan the data to guess column types. That costs an extra pass over the file, so for large files you should pass an explicit schema.
Use Spark for data you want to process at scale. Use open() for small files such as JSON config, text templates, or a model file that a library loads by path. Code in open() runs on the driver node, so it doesn't scale with the cluster.
Reading with SQL
SQL can list a volume and query files in it without creating a table first.
LIST '/Volumes/main/default/landing/';
SELECT * FROM read_files(
'/Volumes/main/default/landing/sales.csv',
format => 'csv',
header => true
);LIST returns one row for each file, with its path, name, size, and modification time. read_files is a table-valued function. This is a function that you use in a FROM clause, and it returns rows. It supports CSV, JSON, Parquet, text, and other formats. It infers a schema by default.
You can point read_files at a folder, or use a glob pattern such as '/Volumes/main/default/landing/*.csv', to read many files as one result.
Which reader to choose
| Task | Reader |
|---|---|
| Large or many files, transformations | `spark.read` |
| Small file loaded by a library | `open()` |
| Quick look or SQL-only workflow | `read_files` |
| See which files exist | `LIST` or `dbutils.fs.ls` |
Common mistakes
- Missing privilege. A permission error means you lack
READ VOLUMEon the volume, orUSE CATALOGorUSE SCHEMAon a parent. Ask the owner to grant the missing privilege. - Wrong path. The path has exactly three names after
/Volumes: catalog, schema, volume. Extra or missing parts give a "path does not exist" error. - Adding
dbfs:. In notebook code, use/Volumes/...directly. Thedbfs:/Volumes/...form is for the CLI, as a later chapter shows. - Unsupported compute. Volumes need compute that supports Unity Catalog. Check the cluster's access mode if paths fail on a cluster that otherwise works.
Exercise
You need to load a 2 GB folder of CSV files from a volume, then join it with a table. Which reader fits best, and why would open() be a poor choice?
Show answer
Use spark.read.csv on the folder path, or read_files in SQL. Both read in parallel across the cluster and return a DataFrame or result you can join. open() runs only on the driver, reads one file at a time as raw text, and doesn't produce a distributed dataset.
Exercise
A query on /Volumes/main/landing/sales.csv fails with "path does not exist", but the file is in the volume main.default.landing. What is wrong?
Show answer
The path is missing the schema name. The correct path is /Volumes/main/default/landing/sales.csv, which follows the catalog, schema, volume order.