Data Lakehouse Architecture
The data lakehouse combines the flexibility of data lakes with the reliability of data warehouses. Learn the core technologies and patterns that make this possible.
Why Lakehouse?
Traditional architectures forced a choice: data lakes for cheap storage and flexibility, or warehouses for ACID transactions and SQL. The lakehouse eliminates this tradeoff.
Delta Lake Deep Dive
Delta Lake adds ACID transactions, time travel, and schema enforcement to Parquet files.
from delta import DeltaTable
from pyspark.sql import SparkSession
spark = SparkSession.builder \
.appName("DeltaLake") \
.config("spark.sql.extensions", "io.delta.sql.DeltaSparkSessionExtension") \
.config("spark.sql.catalog.spark_catalog",
"org.apache.spark.sql.delta.catalog.DeltaCatalog") \
.getOrCreate()
# Create a Delta table with schema enforcement
data = [
(1, "Alice", 28, 75000.0),
(2, "Bob", 35, 92000.0),
(3, "Charlie", 42, 110000.0)
]
columns = ["id", "name", "age", "salary"]
df = spark.createDataFrame(data, columns)
df.write.format("delta").mode("overwrite").save("/delta/employees")
# Schema evolution - adding a column
new_data = [
(4, "Diana", 31, 88000.0, "Engineering"),
(5, "Eve", 29, 95000.0, "Data Science")
]
new_columns = ["id", "name", "age", "salary", "department"]
new_df = spark.createDataFrame(new_data, new_columns)
new_df.write.format("delta").mode("append") \
.option("mergeSchema", "true") \
.save("/delta/employees")
# Time travel - query historical versions
historical_df = spark.read.format("delta") \
.option("versionAsOf", 0) \
.load("/delta/employees")
# ACID update operation
delta_table = DeltaTable.forPath(spark, "/delta/employees")
delta_table.update(
condition="name = 'Bob'",
set={"salary": "salary * 1.10"}
)
# Delta Lake optimizations
delta_table.optimize().executeCompaction()
delta_table.vacuum(168) # Retain 7 days of history
Apache Iceberg
Iceberg provides hidden partitioning, schema evolution without rewriting data, and snapshot isolation.
# Iceberg with PySpark
spark = SparkSession.builder \
.appName("IcebergExample") \
.config("spark.sql.catalog.spark_catalog",
"org.apache.iceberg.spark.SparkCatalog") \
.config("spark.sql.catalog.local.type", "hadoop") \
.config("spark.sql.catalog.local.warehouse", "warehouse") \
.getOrCreate()
# Create an Iceberg table
spark.sql("""
CREATE TABLE local.db.users (
id BIGINT,
name STRING,
email STRING,
created_at TIMESTAMP
)
USING iceberg
PARTITIONED BY (days(created_at))
""")
# Schema evolution in Iceberg - add, rename, reorder columns
spark.sql("ALTER TABLE local.db.users ADD COLUMN phone STRING")
spark.sql("ALTER TABLE local.db.users RENAME COLUMN name TO full_name")
spark.sql("ALTER TABLE local.db.users ALTER COLUMN email TYPE STRING NOT NULL")
# Hidden partitioning - Iceberg handles partitions automatically
spark.sql("""
INSERT INTO local.db.users VALUES
(1, 'Alice', '[email protected]', '2024-01-15'),
(2, 'Bob', '[email protected]', '2024-03-22')
""")
# Query with partition pruning (automatic)
spark.sql("""
SELECT * FROM local.db.users
WHERE created_at BETWEEN '2024-01-01' AND '2024-12-31'
""")
# Iceberg table maintenance
spark.sql("CALL local.system.rewrite_data_files(table => 'db.users')")
spark.sql("CALL local.system.expire_snapshots(table => 'db.users', retain_max_snapshots => 10)")
Apache Hudi
Hudi excels at incremental processing and upsert-heavy workloads.
from pyspark.sql import SparkSession
spark = SparkSession.builder \
.appName("HudiExample") \
.config("spark.serializer", "org.apache.spark.serializer.KryoSerializer") \
.config("hoodie.datasource.write.recordkey.field", "id") \
.config("hoodie.datasource.write.precombine.field", "updated_at") \
.config("hoodie.table.name", "user_profiles") \
.getOrCreate()
# Write initial data
df.write.format("hudi") \
.option("hoodie.datasource.write.operation", "upsert") \
.option("hoodie.datasource.write.table.type", "COPY_ON_WRITE") \
.mode("overwrite") \
.save("/hudi/user_profiles")
# Incremental read - only new/changed records
hudi_df = spark.read.format("hudi") \
.option("hoodie.datasource.query.type", "incremental") \
.option("hoodie.datasource.read.begin.instanttime", "20240101000000") \
.load("/hudi/user_profiles")
# Merge on read for better write performance
df.write.format("hudi") \
.option("hoodie.datasource.write.table.type", "MERGE_ON_READ") \
.option("hoodie.compact.inline.max.delta.commits", "5") \
.mode("append") \
.save("/hudi/user_profiles")
# Global index for upserts across partitions
df.write.format("hudi") \
.option("hoodie.index.type", "GLOBAL_BLOOM") \
.option("hoodie.bloom.index.filter.type", "DYNAMIC_V0") \
.mode("append") \
.save("/hudi/user_profiles")
Comparing Table Formats
| Feature | Delta Lake | Iceberg | Hudi |
|---|---|---|---|
| ACID Transactions | Yes | Yes | Yes |
| Time Travel | Yes | Yes | Yes |
| Schema Evolution | Yes | Yes (best) | Yes |
| Hidden Partitioning | No | Yes | No |
| Upsert Performance | Good | Good | Best |
| Streaming Support | Good | Good | Best |
| Community | Databricks | Apache | Uber/Apache |
Best Practices
- Start small â Pick one table format and master it before expanding
- Partition wisely â Low-cardinality columns work best for partitioning
- Compact regularly â Small files kill read performance
- Set retention policies â Use vacuum/expire snapshots to manage storage
- Monitor file counts â Track small file accumulation as an operational metric
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type â Always start by categorizing the problem
- Incorrect formula application â Verify the formula applies to your specific case
- Unit errors â Always check units are consistent throughout
- Sign errors â Pay careful attention to positive and negative values
- Rounding too early â Keep full precision until the final answer
- Forgetting constraints â Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic â Build on the foundation laid here
- Next Topic â See how this concept extends to more advanced material
- Practice Problems â Additional exercises with solutions
- Video Explanation â Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions