Let’s Get Certified
Module 14 / 21 · Ingestion

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

FormatTypeSchemaNotes
DeltaColumnar (Parquet + log)EnforcedDefault for tables. ACID, time travel.
ParquetColumnar, binaryEmbedded in fileCompressed, column pruning, predicate pushdown. Base of Delta.
ORCColumnar, binaryEmbeddedHadoop/Hive ecosystem
AvroRow-based, binaryEmbedded, supports evolutionCommon in Kafka and streaming
CSVRow-based textNone (header optional)Needs options: header, delimiter (sep), quote, escape, inferSchema. Everything is string unless inferred or given a schema.
JSONText, semi-structuredInferredNested data. One object per line by default; use multiLine for pretty-printed files.
TextLinesSingle value columnLogs
binaryFileWhole filepath, modificationTime, length, contentImages, PDFs, unstructured data
XMLTextInferredNative 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

Data format options in the Databricks documentation

Knowledge check

File handling and formats quiz

Answer 5 questions. Check each answer for an explanation.