Skip to content

Latest commit

ย 

History

History
356 lines (243 loc) ยท 8.68 KB

File metadata and controls

356 lines (243 loc) ยท 8.68 KB

๐Ÿง  Delta Lake โ€” Complete Notes (with Examples)


๐Ÿ“˜ 1. What is Delta Lake?

Delta Lake is an open-source storage layer built on top of your existing data lake (like Azure Data Lake, S3, or GCS). It brings reliability, performance, and ACID transactions to data lakes โ€” turning them into โ€œLakehouses.โ€

๐Ÿ—๏ธ Key Features:

  • โœ… ACID Transactions (Atomicity, Consistency, Isolation, Durability)
  • ๐Ÿ“œ Schema Enforcement & Evolution
  • ๐Ÿ•“ Time Travel
  • ๐Ÿ”„ Upserts (MERGE), Deletes, Updates
  • ๐Ÿงฉ Data Versioning
  • โšก Optimized performance with Z-Ordering
  • โ™ป๏ธ Integrates seamlessly with Spark, Databricks, and cloud storage

๐Ÿ“ 2. Delta Table Storage Structure

When you create a Delta table, it looks like this inside your storage:

/mnt/delta/sales/
โ”œโ”€โ”€ part-00000-xxxx.snappy.parquet
โ”œโ”€โ”€ part-00001-yyyy.snappy.parquet
โ””โ”€โ”€ _delta_log/
    โ”œโ”€โ”€ 00000000000000000000.json
    โ”œโ”€โ”€ 00000000000000000001.json
    โ”œโ”€โ”€ ...
  • The data files are stored in Parquet format.
  • The _delta_log directory contains JSON log files (transaction logs).
  • Each JSON file records every transaction (insert/update/delete/merge).

โš™๏ธ 3. Creating Sample Data

Letโ€™s start with some dummy data ๐Ÿ‘‡

data = [
    (1, "Alice", 1000),
    (2, "Bob", 1500),
    (3, "Charlie", 2000)
]

columns = ["id", "name", "salary"]

df = spark.createDataFrame(data, columns)
df.show()

๐Ÿ“Š Output:

id name salary
1 Alice 1000
2 Bob 1500
3 Charlie 2000

โœ๏ธ 4. Writing Data to Delta Table

df.write.format("delta").mode("overwrite").save("/mnt/delta/employees")

โœ… This command:

  • Writes the DataFrame to a Delta table format.
  • Creates a folder with _delta_log.

๐Ÿ“– 5. Reading Data from Delta Table

delta_df = spark.read.format("delta").load("/mnt/delta/employees")
delta_df.show()

๐Ÿ’ก You can also create a SQL table:

CREATE TABLE employees USING DELTA LOCATION '/mnt/delta/employees';
SELECT * FROM employees;

๐Ÿ” 6. Write Modes in Delta Lake

Mode Description
append Adds new records
overwrite Replaces existing data
ignore Skips write if table exists
errorifexists Fails if table exists
merge Conditional insert/update

Example:

df.write.format("delta").mode("append").save("/mnt/delta/employees")

๐Ÿงฑ 7. Delta Lake vs Parquet

Feature Parquet Delta Lake
ACID Transactions โŒ No โœ… Yes
Time Travel โŒ No โœ… Yes
Schema Enforcement โŒ No โœ… Yes
Upserts & Deletes โŒ No โœ… Yes
Data Versioning โŒ No โœ… Yes
Performance Optimizations โš ๏ธ Limited โœ… ZORDER, OPTIMIZE

๐Ÿ” 8. Time Travel (Versioning)

Delta keeps multiple versions of data using transaction logs.

โณ Query older data

# Using version number
df_v0 = spark.read.format("delta").option("versionAsOf", 0).load("/mnt/delta/employees")

# Using timestamp
df_time = spark.read.format("delta").option("timestampAsOf", "2025-10-05T10:00:00Z").load("/mnt/delta/employees")

SQL version

SELECT * FROM employees VERSION AS OF 0;

๐Ÿงฉ 9. Schema Enforcement & Evolution

Schema Enforcement (Strict Checking)

If you try to write a column that doesnโ€™t exist โ€” Delta will throw an error.

Schema Evolution (Allow Changes)

You can add new columns automatically by enabling mergeSchema:

new_data = [(4, "David", 2500, "IT")]
new_df = spark.createDataFrame(new_data, ["id", "name", "salary", "dept"])

new_df.write.format("delta") \
    .mode("append") \
    .option("mergeSchema", "true") \
    .save("/mnt/delta/employees")

๐Ÿ”„ 10. MERGE Operation (UPSERT)

Used for updating or inserting data in a single transaction.

MERGE INTO employees AS t
USING updates AS s
ON t.id = s.id
WHEN MATCHED THEN
  UPDATE SET t.salary = s.salary
WHEN NOT MATCHED THEN
  INSERT (id, name, salary) VALUES (s.id, s.name, s.salary);

Equivalent PySpark code:

from delta.tables import *

deltaTable = DeltaTable.forPath(spark, "/mnt/delta/employees")
updatesDF = spark.createDataFrame([(2, "Bob", 1800)], ["id", "name", "salary"])

deltaTable.alias("t").merge(
    updatesDF.alias("s"),
    "t.id = s.id"
).whenMatchedUpdateAll() \
 .whenNotMatchedInsertAll() \
 .execute()

๐Ÿงน 11. VACUUM Command

Cleans up old files no longer needed for time travel.

VACUUM employees RETAIN 168 HOURS;

๐Ÿ• Default retention: 7 days (168 hours) To force immediate cleanup (โš ๏ธ risky):

SET spark.databricks.delta.retentionDurationCheck.enabled = false;
VACUUM employees RETAIN 0 HOURS;

โšก 12. OPTIMIZE & ZORDER

Used to compact small files and improve query speed.

OPTIMIZE employees;
OPTIMIZE employees ZORDER BY (dept);

ZORDER sorts data by specific columns for faster queries.


๐Ÿงฐ 13. Change Data Feed (CDF)

Tracks changes (inserts/updates/deletes) between table versions.

Enable it:

ALTER TABLE employees SET TBLPROPERTIES (delta.enableChangeDataFeed = true);

Read changes:

spark.read.format("delta")
    .option("readChangeFeed", "true")
    .option("startingVersion", 1)
    .load("/mnt/delta/employees")

๐Ÿ” 14. Streaming with Delta

Write stream to Delta

df.writeStream.format("delta") \
    .option("checkpointLocation", "/mnt/delta/checkpoints") \
    .start("/mnt/delta/stream_data")

Read stream from Delta

stream_df = spark.readStream.format("delta").load("/mnt/delta/stream_data")

๐Ÿ’พ 15. Managed vs External Delta Tables

Type Storage Location Example
Managed Stored inside Databricks metastore CREATE TABLE employees (id INT) USING DELTA
External Stored in user-defined path CREATE TABLE employees USING DELTA LOCATION '/mnt/delta/employees'

๐Ÿงฑ 16. Converting Parquet to Delta

If you have an existing Parquet table:

CONVERT TO DELTA parquet.`/mnt/delta/parquet_data`

๐Ÿง  17. Checkpoints & Transaction Logs

  • Checkpoints are Parquet summaries of transaction logs.
  • They help Spark quickly rebuild table state during reads.
  • Stored every 10 commits by default.

๐Ÿงฉ 18. Handling Concurrent Writes

Delta uses transaction logs with optimistic concurrency control. If two jobs write simultaneously, one fails to avoid data corruption.


๐Ÿงฎ 19. Restoring a Table

RESTORE TABLE employees TO VERSION AS OF 3;

๐ŸŒ 20. Delta Live Tables (DLT)

DLT is a Databricks-managed pipeline framework built on Delta Lake. It automates:

  • Data ingestion,
  • Quality enforcement (expectations),
  • Pipeline orchestration.

โœ… 21. Common Interview Scenarios

Scenario How to Handle
Duplicate small files Use OPTIMIZE
Deleted wrong data Use TIME TRAVEL or RESTORE
Schema mismatch Enable mergeSchema
Need incremental loads Use MERGE or Change Data Feed
Query slow Use ZORDER and caching

๐Ÿงฉ 22. Summary Table

Feature Command
Create Delta Table df.write.format("delta").save()
Read Delta Table spark.read.format("delta").load()
Time Travel option("versionAsOf", x)
Merge (Upsert) MERGE INTO
Vacuum VACUUM table
Optimize OPTIMIZE table
Z-Order OPTIMIZE table ZORDER BY (col)
Change Data Feed readChangeFeed = true
Restore RESTORE TABLE ...