File handling and formats
Match CSV, JSON, Parquet, and other formats to an ingestion task. Read and write files from a Unity Catalog volume.
File formats
| Format | Type | Schema | Notes |
|---|---|---|---|
| Delta | Columnar (Parquet + log) | Enforced | Default for tables. ACID, time travel. |
| Parquet | Columnar, binary | Embedded in file | Compressed, column pruning, predicate pushdown. Base of Delta. |
| ORC | Columnar, binary | Embedded | Hadoop/Hive ecosystem |
| Avro | Row-based, binary | Embedded, supports evolution | Common in Kafka and streaming |
| CSV | Row-based text | None (header optional) | Needs options: header, delimiter (sep), quote, escape, inferSchema. Everything is string unless inferred or given a schema. |
| JSON | Text, semi-structured | Inferred | Nested data. One object per line by default; use multiLine for pretty-printed files. |
| Text | Lines | Single value column | Logs |
| binaryFile | Whole file | path, modificationTime, length, content | Images, PDFs, unstructured data |
| XML | Text | Inferred | Native support with rowTag option |
Paths
- Volume:
/Volumes/catalog/schema/volume/dir/file(recommended). - Cloud URI:
s3://bucket/path,abfss://container@account.dfs.core.windows.net/path,gs://bucket/path, accessed through Unity Catalog external locations. - Workspace files:
/Workspace/Users/you/.... Legacy:dbfs:/.
Files or folders
A path to a file reads that file. A path to a directory reads every file in it, no wildcard needed. Directory reads suit daily drops, partitioned data sets and logs.
Reading with options and explicit schemas
schema = "order_id BIGINT, amount DECIMAL(10,2), order_ts TIMESTAMP" # DDL string
df = (spark.read.format("csv")
.schema(schema) # explicit: faster and safer than inferSchema
.option("header", "true")
.option("sep", ";")
.option("mode", "PERMISSIVE") # PERMISSIVE (default), DROPMALFORMED, FAILFAST
.option("rescuedDataColumn", "_rescued_data")
.load("/Volumes/main/raw/landing/orders/*.csv"))
df_json = spark.read.option("multiLine", True).json("/Volumes/main/raw/landing/api/")
df_bin = spark.read.format("binaryFile").option("pathGlobFilter", "*.pdf").load("/Volumes/main/raw/docs/")
Inferring a schema requires an extra pass over data, so give an explicit schema in production. Malformed CSV rows go to _corrupt_record in PERMISSIVE mode, or to the rescued data column if configured.
Querying files in SQL by format prefix
In SQL, a format prefix followed by a file or directory path queries the files directly:
SELECT * FROM parquet.`/Volumes/main/raw/landing/orders/`;
SELECT * FROM csv.`/Volumes/main/raw/landing/file.csv`; -- no options: prefer read_files for CSV
SELECT * FROM json.`/Volumes/main/raw/landing/events/`;
SELECT * FROM delta.`s3://acme-data/tables/orders`; -- path-based Delta read
The path goes in backticks, not quotes.
File metadata
df.select("*", "_metadata.file_path", "_metadata.file_modification_time")
The hidden _metadata column gives the source file of each row. Add it in bronze for lineage and debugging. (input_file_name() is the older function.)
Writing files
df.write.mode("overwrite").partitionBy("country").parquet("/Volumes/main/exports/out/orders")
df.coalesce(1).write.mode("overwrite").option("header", True).csv("/Volumes/main/exports/out/one_file")
Spark writes a directory of part files, one per partition, plus marker files (_SUCCESS, _committed_*).
INSERT OVERWRITE DIRECTORY
The SQL way to write files. It writes query results out to files; it does not load files into a table.
INSERT OVERWRITE DIRECTORY '/Volumes/main/app/exports/tasks'
USING JSON
SELECT * FROM main.app.tasks; -- also PARQUET, CSV, TEXT, ...
To load files into a table, use COPY INTO, Auto Loader, or read_files.
Preserve a leading zero
A CSV feed contains ZIP codes 02115 and 10001. An inferred numeric schema loses a leading zero. How would you change the read?
Check your reasoning
Supply an explicit string field for ZIP codes. A ZIP code is an identifier. Its leading zero is meaningful and arithmetic on it is unnecessary.
Product reference
File handling and formats quiz
Answer 5 questions. Check each answer for an explanation.