bike-lakehouse - HOW TO RUN
===========================

Requirements
------------
- Python 3.10 or newer
- Java 17 or 21 (free Temurin build: https://adoptium.net). JAVA_HOME must point to it.
- About 4 GB of free memory and 1 GB of disk space.
- Internet on the first run: Spark downloads the Delta Lake jars from Maven Central once, then caches them.

1. Setup - macOS / Linux / WSL
------------------------------
    cd bike-lakehouse
    java -version                      # must say 17 or 21
    python3 -m venv .venv
    source .venv/bin/activate
    pip install -r requirements.txt    # delta-spark 4.4.1 + pyspark 4.2.0
    python -c "from lake import get_spark; print(get_spark().version)"

2. Setup - Windows
------------------
Easiest: install WSL (Windows Subsystem for Linux, "wsl --install" in an admin PowerShell),
then follow the Linux steps inside WSL.

Native Windows also works, but Spark needs the Hadoop helper files to write to disk:
  a) Download winutils.exe and hadoop.dll built for Hadoop 3.x and put them in C:\hadoop\bin
  b) Set environment variables: HADOOP_HOME=C:\hadoop and add C:\hadoop\bin to PATH
  c) Install Temurin Java 21 and tick "Set JAVA_HOME variable" in the installer
  d) Open a NEW terminal (PowerShell):
       cd bike-lakehouse
       python -m venv .venv
       .venv\Scripts\activate
       pip install -r requirements.txt
       python -c "from lake import get_spark; print(get_spark().version)"
  Use "python" instead of "python3" in all commands below.

3. Follow the course order
--------------------------
    python gen_data.py                 # day 1 landing files
    python 01_first_table.py
    python 02_acid.py
    python 03_schema.py
    python 04_time_travel.py
    python 08_sql.py
    python bronze.py                   # run it twice: the 2nd run loads 0 rows
    python peek.py bronze stations
    python 13_live_stream.py           # optional live streaming demo
    python stations.py
    python rides.py
    python peek.py silver rides_quarantine
    python members.py
    python gen_data.py --day 2         # day 2 arrives
    python bronze.py
    python stations.py                 # 2 changed, 1 new (SCD type 2)
    python members.py
    python rides.py
    python 05_change_feed.py
    python gold.py
    python 10_reports.py
    python 14_export.py
    pip install "deltalake[pyarrow]" pandas
    python 15_no_spark.py              # reads a gold table with no Spark and no Java
    python 06_constraints.py
    python quality.py
    pip install pytest
    python -m pytest -q
    python maintain.py
    python 07_vacuum.py                # demo only: breaks time travel on the playground table

4. The whole pipeline in one command
------------------------------------
Start over and run everything end to end:
    rm -rf lakehouse landing           # Windows PowerShell: Remove-Item -Recurse -Force lakehouse, landing
    python gen_data.py
    python run_pipeline.py
    python gen_data.py --day 2
    python run_pipeline.py
    python run_pipeline.py             # rerun: 0 new rows, same result (idempotent)
    python 09_audit.py
    python 11_late_rides.py            # late data for day 1
    python run_pipeline.py             # gold refreshes only 2026-11-01
    python 12_recover.py               # undo a bad write with RESTORE

Advanced Delta features (after the pipeline has run both days):
    python 16_generated.py             # table builder API + generated column (bad row is rejected)
    python 17_deletion_vectors.py      # the same DELETE with and without deletion vectors, then REORG PURGE
    python 18_clone.py                 # SHALLOW CLONE of gold, change the clone, prod stays untouched
    python 19_clustering.py            # liquid clustering: CLUSTER BY, OPTIMIZE, change the columns
    python 20_properties.py            # table properties, log retention and a checkpoint at version 10
INTERVIEW.md has the eight interview questions from the last chapter.

Schedule it (Linux/macOS): crontab -e and add
    0 2 * * * /full/path/to/bike-lakehouse/run_daily.sh
Windows: use Task Scheduler with a .bat file that runs
    cd /d C:\path\to\bike-lakehouse && .venv\Scripts\activate && python run_pipeline.py

5. Troubleshooting
------------------
- "Java gateway process exited" / "UnsupportedClassVersionError": install Java 17 or 21, set JAVA_HOME, new terminal.
- ClassNotFound ... delta: pyspark and delta-spark versions do not match. Reinstall with: pip install -r requirements.txt
- Windows "winutils" / "HADOOP_HOME is unset": see step 2, or use WSL.
- See the JVM start-up logs:  SPARK_QUIET=0 python bronze.py   (PowerShell: $env:SPARK_QUIET="0"; python bronze.py)
- Start over at any time: delete the lakehouse, landing, landing_live, exports and spark-warehouse folders.
- Always run the scripts from the project folder (paths are relative to it).
