Database Data Lakes and Lakehouses
What you will learn
- What a data lake is and how it differs from a warehouse
- Schema on write versus schema on read
- What goes wrong when a lake is not managed (the "data swamp")
- What a lakehouse tries to add
- When each approach fits
1. A folder of raw files
A warehouse (lessons 13.1 to 13.3) holds cleaned, structured tables. But organisations also produce data that is not tidy or not yet understood: log files, exports, JSON from apps, spreadsheets, sensor readings. Cleaning all of it before storing is slow, and you may not know yet what questions will be asked.
A data lake is a place to store such data as it arrives, in its raw form, usually as plain files in folders, because storing files is cheap. Nothing forces a structure on the files when they are written.
We can build a tiny lake with Python. Two sources write into it: sales exports as CSV files, and website events as JSON lines (one JSON object per line).
import csv, json, pathlib, tempfile
lake = pathlib.Path(tempfile.mkdtemp()) / "lake"
(lake / "sales" / "2026-01").mkdir(parents=True)
(lake / "web_events").mkdir()
(lake / "sales" / "2026-01" / "store_dhaka.csv").write_text("order_id,amount\n1,120\n2,450\n", encoding="utf-8")
(lake / "sales" / "2026-01" / "store_ctg.csv").write_text("order_id,amount,coupon\n3,300,EID10\n4,90,\n", encoding="utf-8")
events = [{"user": "u1", "page": "/tea", "ms": 120}, {"user": "u2", "page": "/rice"}, {"user": "u1", "page": "/tea", "ms": 80, "device": {"type": "phone"}}]
(lake / "web_events" / "day1.jsonl").write_text("\n".join(json.dumps(e) for e in events) + "\n", encoding="utf-8")
for path in sorted(lake.rglob("*")):
if path.is_file():
print(path.relative_to(lake).as_posix())
sales/2026-01/store_ctg.csv
sales/2026-01/store_dhaka.csv
web_events/day1.jsonl
Notice what the lake allows (and does not check):
- The two sales files have different columns: Chattogram's file has an extra
couponcolumn. - The events have different fields: one has no
ms, one has a nesteddevice. - Folders (
sales/2026-01/) are the only organisation, and names are a convention people must follow.
A database table would have rejected or reshaped these rows. The lake simply keeps them.
2. Schema on write versus schema on read
| Warehouse (and any normal database) | Data lake | |
|---|---|---|
| When is the structure decided? | before writing (schema on write) | when reading (schema on read) |
| What happens to odd data? | refused or cleaned at load time | stored as it is |
| Cost of storing | higher effort up front | cheap and fast |
| Cost of reading | simple, reliable queries | each reader must interpret the files |
With schema on read, the program that reads decides what the data means. Here it adds up all sales amounts and notes which columns exist:
total, columns = 0, set()
for path in sorted((lake / "sales").rglob("*.csv")):
with open(path, encoding="utf-8", newline="") as f:
reader = csv.DictReader(f)
columns.update(reader.fieldnames)
for row in reader:
total += int(row["amount"])
print("sales columns seen:", sorted(columns), "total:", total)
pages = {}
for line in (lake / "web_events" / "day1.jsonl").read_text(encoding="utf-8").splitlines():
e = json.loads(line)
pages.setdefault(e["page"], []).append(e.get("ms"))
print(pages)
print("device on 3rd event:", json.loads((lake / "web_events" / "day1.jsonl").read_text(encoding="utf-8").splitlines()[2]).get("device", {}).get("type"))
sales columns seen: ['amount', 'coupon', 'order_id'] total: 960
{'/tea': [120, 80], '/rice': [None]}
device on 3rd event: phone
The reader had to know that amount is a whole number, that ms may be missing (e.get("ms") gives None), and that device is optional and nested. Every program reading the lake must repeat this knowledge, and if two programs interpret a field differently, they produce different reports.
Schema on read in SQL. This lesson runs on the sample database you chose in lesson 4.1 (use the Example data picker at the top). SQLite can read the same event lines as a database would: store each line as it arrives, as text, and decide what it means only when you query it. The raw_events table is a small one of its own; Reset data removes it.
CREATE TABLE raw_events (line TEXT);
INSERT INTO raw_events VALUES
('{"user": "u1", "page": "/tea", "ms": 120}'),
('{"user": "u2", "page": "/rice"}'),
('{"user": "u1", "page": "/tea", "ms": 80, "device": {"type": "phone"}}');
SELECT json_extract(line, '$.user') AS user, json_extract(line, '$.page') AS page,
json_extract(line, '$.ms') AS ms, json_extract(line, '$.device.type') AS device
FROM raw_events;
user page ms device
---- ----- ---- ------
u1 /tea 120 NULL
u2 /rice NULL NULL
u1 /tea 80 phone
(3 rows)
SELECT json_extract(line, '$.page') AS page, COUNT(*) AS visits, AVG(json_extract(line, '$.ms')) AS average_ms
FROM raw_events GROUP BY page ORDER BY page;
page visits average_ms
----- ------ ----------
/rice 1 NULL
/tea 2 100.0
(2 rows)
CREATE TABLE raw_events (line TEXT);
INSERT INTO raw_events VALUES
('{"user": "u1", "page": "/tea", "ms": 120}'),
('{"user": "u2", "page": "/rice"}'),
('{"user": "u1", "page": "/tea", "ms": 80, "device": {"type": "phone"}}');
SELECT json_extract(line, '$.user') AS user, json_extract(line, '$.page') AS page,
json_extract(line, '$.ms') AS ms, json_extract(line, '$.device.type') AS device
FROM raw_events;
user page ms device
---- ----- ---- ------
u1 /tea 120 NULL
u2 /rice NULL NULL
u1 /tea 80 phone
(3 rows)
SELECT json_extract(line, '$.page') AS page, COUNT(*) AS visits, AVG(json_extract(line, '$.ms')) AS average_ms
FROM raw_events GROUP BY page ORDER BY page;
page visits average_ms
----- ------ ----------
/rice 1 NULL
/tea 2 100.0
(2 rows)
CREATE TABLE raw_events (line TEXT);
INSERT INTO raw_events VALUES
('{"user": "u1", "page": "/tea", "ms": 120}'),
('{"user": "u2", "page": "/rice"}'),
('{"user": "u1", "page": "/tea", "ms": 80, "device": {"type": "phone"}}');
SELECT json_extract(line, '$.user') AS user, json_extract(line, '$.page') AS page,
json_extract(line, '$.ms') AS ms, json_extract(line, '$.device.type') AS device
FROM raw_events;
user page ms device
---- ----- ---- ------
u1 /tea 120 NULL
u2 /rice NULL NULL
u1 /tea 80 phone
(3 rows)
SELECT json_extract(line, '$.page') AS page, COUNT(*) AS visits, AVG(json_extract(line, '$.ms')) AS average_ms
FROM raw_events GROUP BY page ORDER BY page;
page visits average_ms
----- ------ ----------
/rice 1 NULL
/tea 2 100.0
(2 rows)
The missing ms and device come back as NULL; nothing refused the odd lines when they were stored. The meaning (a whole number of milliseconds, an optional nested device) lives in the query, which is schema on read.
3. The data swamp
A lake without care turns into a data swamp: files nobody can find, understand or trust. Typical causes:
- No catalog: nothing says what each folder holds, who owns it, or how fresh it is.
- No quality checks: wrong or duplicated files pile up (lesson 13.7).
- No naming and folder rules:
final2_new.csvbesidefinal.csv. - No access control: sensitive data sits in shared folders (chapter 8).
- No history of changes: a file is overwritten and the old values are gone.
A well-run lake therefore has rules: consistent folders (often by source and date), a catalog describing each dataset, checks on arrival, and permissions.
4. The lakehouse idea
Many organisations end up with both a lake (cheap, flexible) and a warehouse (reliable, easy to query), copying data between them. A lakehouse is an approach that tries to give database-style features directly on top of lake files, such as:
- table-like structure and schemas for files;
- transactions, so a half-finished write is never visible (chapter 6);
- the ability to run SQL on the files;
- history and the ability to see data as it was earlier.
Several products and file formats implement this idea. Their details change quickly and this lesson does not describe any of them; read the documentation of the one you consider before relying on a claim about it.
5. Lake, warehouse, or both?
| Situation | Suggested approach |
|---|---|
| Clean, well-understood business data and many report users | warehouse |
| Large volumes of logs, JSON or files whose use is not yet known | lake |
| Need cheap storage now, structure later, and reliable reports later | lake, then warehouse (or a lakehouse) |
| Small team, modest data | usually one relational database is enough |
Worked example: a mixed set of sources
A made-up online grocer collects: (a) daily order exports from its shop system, (b) website click logs, (c) delivery drivers' GPS pings, (d) supplier price lists sent as spreadsheets.
- Orders (a) are structured and needed for finance reports: load into the warehouse (lessons 13.2 and 13.4).
- Click logs (b) and GPS pings (c) are huge, loosely structured, and no one knows yet what to ask: store raw in the lake, folder per source per day.
- Supplier lists (d) vary in layout; a person checks them, then they are loaded to the warehouse; the originals stay in the lake as a record.
Later, the team asks "do slow pages lose sales?". They read click logs from the lake, join them to orders in the warehouse and, if the analysis proves useful, build a cleaned table for it.
Common mistakes
- Dumping everything in "just in case", without a catalog or owner. That is how swamps form.
- Assuming files in a lake follow one structure. Check, as the
couponcolumn showed. - Letting each team interpret raw fields differently, which produces conflicting numbers.
- Storing sensitive personal data in a lake without access rules.
- Treating lake or lakehouse as a magic cure. They move work, not remove it.
Key takeaways
- A data lake stores raw files cheaply; its structure is decided when reading (schema on read).
- A warehouse decides structure before writing (schema on write) and gives reliable, simple queries.
- Without catalog, quality checks, naming and permissions, a lake becomes a data swamp.
- A lakehouse aims to add database features (tables, transactions, SQL) on top of lake files.
- Many organisations use a lake for raw data and a warehouse for trusted reports.
Practice
1. What does "schema on read" mean?
Show answer
The structure of the data is decided by the program that reads it, not enforced when the data is written.
2. In the lesson's lake, which file had an extra column, and what was it?
Show answer
store_ctg.csv had a coupon column that the other file lacked.
3. Name three things that help prevent a data swamp.
Show answer
For example: a catalog describing each dataset, quality checks when files arrive, naming and folder rules, and access permissions.
4. Why did the code use e.get("ms") instead of e["ms"]?
Show answer
Some events have no ms field. e["ms"] would raise a KeyError; e.get("ms") returns None instead.
5. Give two features a lakehouse tries to add to a plain lake.
Show answer
For example: table structure with schemas, transactions, SQL queries over the files, and keeping or viewing earlier versions of the data.
6. A school has 2,000 students and keeps grades in one relational database. Does it need a lake? Why?
Show answer
Probably not: the data is small, structured and well understood, so one relational database is simpler and enough.