This project demonstrates how to integrate Soda data quality contracts into an Apache Airflow data pipeline. It shows how to validate data quality before writing to production databases, preventing bad data from contaminating your data warehouse.
πWatch Live Demo
The demo includes:
- Airflow DAG that orchestrates a data pipeline with Soda contract verification
- Soda Contracts that define data quality rules
- Test Data Generation for demonstrating data quality checks
- PostgreSQL Integration for storing validated data
- Soda Cloud Integration for monitoring and observability
βββββββββββββββ
β Load β Generate 5 rows of test data
ββββββββ¬βββββββ
β
βΌ
βββββββββββββββ
β Verify β Verify contract with DuckDB (in-memory)
β (DuckDB) β β οΈ Data Quality Gate
ββββββββ¬βββββββ
β
βΌ
βββββββββββββββ
β Insert β Insert verified data to PostgreSQL
β (PostgreSQL)β
ββββββββ¬βββββββ
β
βΌ
βββββββββββββββ
β Verify β Verify contract on PostgreSQL
β (PostgreSQL)β Publish results to Soda Cloud
βββββββββββββββ
- Python 3.10+
- PostgreSQL database
- Soda Cloud account (for publishing results)
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install Soda packages from private PyPI
pip install -i https://pypi.dev.sodadata.io/simple -U soda-postgres soda-duckdb
# Install other dependencies
pip install -r requirements.txtCreate a .env file in the project root:
# PostgreSQL Connection
POSTGRES_HOST=your-postgres-host
POSTGRES_PORT=5432
POSTGRES_DATABASE=your-database
POSTGRES_USER=your-username
POSTGRES_PASSWORD=your-password
# Soda Cloud
SODA_CLOUD_ENV=your-environment
SODA_CLOUD_API_KEY=your-api-key
SODA_CLOUD_API_SECRET=your-api-secret
# Airflow (optional)
AIRFLOW_HOME=/path/to/soda-contract-webinarCreate the orders table:
psql -h <host> -U <user> -d <database> -f ddl/create_orders_table.sql# Test both connections
./soda.sh connection-testSee AIRFLOW_README.md for detailed instructions.
# Start Airflow
airflow standalone
# Access UI at http://localhost:8080
# Trigger the DAG: orders_pipeline_with_soda# Generate contract
./soda.sh generate -p webinardb/postgres/public/orders
# Verify contract
./soda.sh verify -p contracts/webinardb/postgres/public/orders.yaml
# Publish contract
./soda.sh publish -p contracts/webinardb/postgres/public/orders.yamlGenerates 5 rows of test order data with:
- Valid UUIDs for order_id and customer_id
- Random dates, amounts, addresses, and statuses
- Option to generate errors for testing (
hasErrors: true)
- Validates data quality using DuckDB in-memory
- Acts as a data quality gate before database write
- Fails the pipeline if checks don't pass
- Inserts verified data into
public.orderstable - Only runs if Step 2 passes
- Verifies contract on the actual PostgreSQL table
- Publishes results to Soda Cloud for monitoring
The contract (contracts/webinardb/postgres/public/orders_final.yaml) contains all the checks and can be used as a reference.
The contract includes:
- Schema validation: No extra columns, fixed column order
- UUID validation: order_id and customer_id must be valid UUID v4
- Date validation: No missing dates, shipping_date must be after order_date
- Status validation: Must be one of: PENDING, SHIPPED, CANCELLED, RETURNED, REFUNDED
- Amount validation: Must be non-negative
- Address validation: Length between 5 and 200 characters
- Country code validation: Must be uppercase 2-letter ISO code
- Uniqueness: No duplicate order_ids
To test the pipeline with data quality errors:
-
In Airflow UI, trigger the DAG with config:
{ "hasErrors": true } -
The pipeline will generate data with:
- Row 0: shipping_date before order_date
- Row 1: Invalid status value
- Row 2: Missing status value
-
The pipeline should fail at
verify_locallystep
The soda.sh script provides convenient commands on top of Soda CLI.
# Ensure all dependencies are installed
pip install -r requirements.txt
# Verify Soda packages
pip list | grep soda# Test connections
./soda.sh connection-testSee AIRFLOW_README.md for Airflow-specific troubleshooting.
- Soda v4 Reference Documentation - Complete reference for Soda's interfaces, configuration options, and APIs
- Presentation from Webinar
