A comprehensive example project demonstrating Dagster capabilities with CSV files and DuckDB.
- Python 3.8 or higher installed on your computer
- Windows: Download from python.org (check "Add Python to PATH" during installation!)
- Mac: Usually pre-installed, or install via python.org
- Linux: Usually pre-installed, or use
sudo apt install python3 python3-venv
- Download or clone this project
- Open the project folder
- Double-click
setup.bat - Wait for installation to complete
- Run these commands in Command Prompt or PowerShell:
venv\Scripts\activate dagster dev
- Open your browser to http://localhost:3000
- Download or clone this project
- Open Terminal and navigate to the project folder:
cd path/to/dagster-example - Run the setup script:
chmod +x setup.sh ./setup.sh
- Start Dagster:
source venv/bin/activate # Activate if not already active dagster dev
- Open your browser to http://localhost:3000
Once Dagster is running, you'll see the web UI where you can:
- View all data assets in a visual graph
- Click any asset and press "Materialize" to run it
- See the results and logs in real-time
π Want to learn more? Check out docs/INDEX.md for complete documentation!
This project showcases various Dagster features including:
- Assets: Loading, transforming, and aggregating data
- Resources: DuckDB integration for data warehousing
- Jobs: Orchestrating multiple assets
- Schedules: Running jobs on a regular schedule
- Sensors: Event-driven pipeline execution
- Partitions: Processing data in time-based chunks
dagster-example/
βββ data/
β βββ raw/ # Raw CSV files
β β βββ customers.csv
β β βββ products.csv
β β βββ sales.csv
β βββ processed/ # Processed output files
β βββ warehouse/ # DuckDB database files
βββ dagster_example/
β βββ __init__.py # Main definitions
β βββ resources.py # DuckDB resource
β βββ jobs.py # Job definitions
β βββ schedules.py # Schedule definitions
β βββ sensors.py # Sensor definitions
β βββ assets/
β βββ basic_assets.py # Data loading assets
β βββ transformation_assets.py # Data transformation
β βββ aggregation_assets.py # Analytics & aggregations
β βββ advanced_assets.py # Partitions & advanced patterns
βββ pyproject.toml
βββ setup.py
If you prefer to set up manually or the automated script doesn't work:
Windows:
python -m venv venv
venv\Scripts\activateMac/Linux:
python3 -m venv venv
source venv/bin/activatepip install --upgrade pip
pip install -e "."dagster devThen open your browser to http://localhost:3000
The project includes several groups of assets:
raw_customers: Loads customer data from CSVraw_products: Loads product data from CSVraw_sales: Loads sales data from CSV
enriched_sales: Joins sales with products and customersproduct_metrics: Calculates profit margins and markups
daily_sales_summary: Daily aggregated metricscustomer_analytics: Customer lifetime value analysiscategory_performance: Product category performancestate_sales_analysis: Geographic sales analysisproduct_recommendations: Co-purchase recommendations
daily_partitioned_sales: Sales data partitioned by day
The project includes three CSV files with sample e-commerce data:
- customers.csv: 10 customers with contact info and locations
- products.csv: 10 products across Electronics and Furniture categories
- sales.csv: 20 sales transactions
After running the assets, DuckDB will contain tables like:
-- Raw tables
raw_customers
raw_products
raw_sales
-- Transformed tables
enriched_sales (includes customer & product details)
product_metrics (profitability analysis)
-- Analytics tables
daily_sales_summary
customer_analytics
category_performance
state_sales_analysis
product_recommendationsAssets represent data that you want to create and maintain. Each asset:
- Has clear dependencies (inputs)
- Produces materialized output
- Includes logging and metadata
Example:
@asset(
description="Load raw customer data from CSV into DuckDB",
group_name="raw_data",
)
def raw_customers(context: AssetExecutionContext, duckdb: DuckDBResource):
csv_path = Path("data/raw/customers.csv").absolute()
duckdb.read_csv_to_table(str(csv_path), "raw_customers")Resources provide reusable services to assets. The DuckDBResource:
- Manages database connections
- Provides helper methods
- Can be configured per environment
Assets automatically form a DAG (Directed Acyclic Graph):
raw_customers βββ
raw_products βββΌββ> enriched_sales ββ> daily_sales_summary
raw_sales ββββββ βββ> customer_analytics
Jobs select which assets to materialize:
daily_analytics_job: Refreshes all analyticsetl_job: Loads and transforms raw dataanalytics_only_job: Updates analytics only
Schedules run jobs automatically:
daily_schedule = ScheduleDefinition(
job=daily_analytics_job,
cron_schedule="0 6 * * *", # 6 AM daily
)Sensors trigger jobs based on events:
@sensor(job=etl_job)
def sales_file_sensor(context):
# Check if sales.csv has been modified
# Return RunRequest if file changedPartitions process data in chunks (e.g., by date):
@asset(partitions_def=DailyPartitionsDefinition(start_date="2023-11-01"))
def daily_partitioned_sales(context, enriched_sales):
# Process one day at a time
partition_date = context.partition_key- Start with Basic Assets: Look at
basic_assets.pyto see simple data loading - View the Asset Graph: Open Dagster UI and explore the asset lineage
- Materialize an Asset: Click "Materialize" on
raw_customers - Check the Database: Query DuckDB to see the loaded data
- Explore Transformations: Study
transformation_assets.pyfor SQL transforms - Run a Job: Execute
daily_analytics_jobto see multiple assets - Add Metadata: Enhance assets with custom metadata
- Create a Schedule: Modify
schedules.pyto run at different times
- Work with Partitions: Materialize specific date partitions
- Build Sensors: Create custom sensors for your data sources
- Add New Assets: Extend the project with your own analytics
- Configure Resources: Set up different DuckDB databases for dev/prod
After materializing all assets, query DuckDB directly:
import duckdb
conn = duckdb.connect("data/warehouse/analytics.duckdb")
# Top customers by revenue
conn.execute("""
SELECT customer_name, lifetime_value
FROM customer_analytics
ORDER BY lifetime_value DESC
LIMIT 5
""").df()
# Best performing category
conn.execute("""
SELECT * FROM category_performance
""").df()
# Daily trends
conn.execute("""
SELECT sale_date, total_revenue, total_profit
FROM daily_sales_summary
ORDER BY sale_date
""").df()After materializing assets, you can explore the database:
python open_duckdb_ui.pyThis opens a web UI to query and explore your data!
dagster asset materialize -adagster job execute -j daily_analytics_jobdagster asset materialize -a monthly_partitioned_sales --partition 2024-05-01- Add More Data: Create additional CSV files for returns, inventory, etc.
- New Metrics: Build assets for customer segmentation or churn analysis
- External Sources: Connect to APIs or databases
- Alerts: Add sensors that notify on data quality issues
- Tests: Write asset tests to validate data quality
- Deployment: Deploy to Dagster Cloud or self-hosted
- Windows: Make sure you checked "Add Python to PATH" during installation
- Mac/Linux: Try using
python3instead ofpython
chmod +x setup.sh
./setup.sh# Make sure virtual environment is activated
source venv/bin/activate # Mac/Linux
venv\Scripts\activate # Windows
# Reinstall
pip install -e "."# Use a different port
dagster dev -p 3001# Close any open connections and restart Dagster
# Windows: Close Command Prompt/PowerShell windows
# Mac/Linux:
pkill -f dagster
dagster dev- Make sure you're in the correct project directory
- Try restarting: Stop Dagster (Ctrl+C) and run
dagster devagain - Check the terminal for any error messages
Feel free to extend this example project with:
- Additional asset patterns
- More complex transformations
- Integration with other tools
- Documentation improvements
Happy Learning! π
For questions or issues, consult the Dagster community.