Sub topic 9
Delta Lake: reliable tables on open storage
This chapter assumes you know basic SQL, can read short Python, and understand the difference between a data lake and a data warehouse. It also assumes you know what cloud object storage is. You should have a workspace with compute attached, as covered in earlier chapters.
What Delta Lake adds
Delta Lake is an open table format. It stores table data as Parquet files in object storage and adds a transaction log next to them. The log is a folder named _delta_log that records every change as an ordered series of JSON commits.
A plain folder of Parquet files has no way to make a write atomic. A job that fails halfway leaves partial files that readers can see. With a transaction log, a change only exists once its commit is written. Readers see the table as of the last complete commit.
This gives a lakehouse table the properties of a warehouse table:
- ACID transactions, so concurrent writers and readers do not corrupt each other.
- Schema enforcement, so a write with the wrong columns fails instead of silently adding bad data.
- Versioned history, so you can inspect or read older states of the table.
On Databricks, tables you create are Delta tables by default.
Create and query a table
This creates a schema and a managed table in Unity Catalog. It assumes you can create objects in the main catalog.
CREATE SCHEMA IF NOT EXISTS main.tutorial;
CREATE TABLE IF NOT EXISTS main.tutorial.orders (
order_id BIGINT,
customer_id BIGINT,
amount DECIMAL(10, 2),
order_ts TIMESTAMP
);
INSERT INTO main.tutorial.orders VALUES
(1, 101, 25.00, current_timestamp()),
(2, 102, 40.50, current_timestamp()),
(3, 101, 15.25, current_timestamp());No USING DELTA clause is needed. In a notebook, read the same table by its three-level name:
from pyspark.sql import functions as F
df = spark.table("main.tutorial.orders")
totals = (
df.groupBy("customer_id")
.agg(F.sum("amount").alias("total_amount"))
)
display(totals)Look at the history
Each committed change creates a new table version. Two commands expose this:
DESCRIBE HISTORY main.tutorial.orders;
SELECT * FROM main.tutorial.orders VERSION AS OF 0;DESCRIBE HISTORY lists versions with the operation, user, and timestamp. VERSION AS OF reads an earlier version. The INSERT above is one commit, so the table has a version 0 for the table creation and a later version for the insert. Querying version 0 returns no rows.
Common errors
- Schema mismatch. Appending a DataFrame with extra or differently typed columns fails. This is enforcement working as intended. Change the schema deliberately instead of loosening the write.
- Editing files directly. Adding or deleting Parquet files in the table folder bypasses the log. Always change the table through SQL or the DataFrame API.
- Missing permissions. Querying a table requires
SELECTon the table andUSEprivileges on its catalog and schema. A later chapter covers this.
Exercise
A job writes new rows to a Delta table and crashes before it finishes. What do readers of the table see, and why?
Show answer
They see the table as it was before the job started. Readers use the transaction log, and the log only lists files from completed commits. Files written by the failed job are not referenced by any commit, so they are ignored.
Exercise
Which command would you run to find out who changed a table and when, and which would you use to read the table as it was at the first version?
Show answer
Use DESCRIBE HISTORY main.tutorial.orders to see the operations, users, and timestamps. Use SELECT * FROM main.tutorial.orders VERSION AS OF 0 to read the first version.