|
| 1 | +# Data Platform Architecture Guide - Dagster Project |
| 2 | + |
| 3 | +This directory contains a complete Dagster project demonstrating data platform architectural patterns using modern Dagster best practices: Components, multiple code locations, and dignified Python. |
| 4 | + |
| 5 | +## Project Structure |
| 6 | + |
| 7 | +``` |
| 8 | +data-platform-architecture-guide/ |
| 9 | +├── pyproject.toml # Dependencies and project config |
| 10 | +├── workspace.yaml # Multi-code-location workspace |
| 11 | +├── README.md # This file |
| 12 | +├── src/ |
| 13 | +│ ├── data_platform_guide/ # Component-based code locations |
| 14 | +│ │ ├── __init__.py |
| 15 | +│ │ ├── databricks_delta/ # Code location 1: Databricks Delta Lakehouse |
| 16 | +│ │ │ ├── __init__.py |
| 17 | +│ │ │ ├── definitions.py # Definitions for Databricks location |
| 18 | +│ │ │ └── defs/ |
| 19 | +│ │ │ ├── components/ |
| 20 | +│ │ │ │ └── lakehouse/ # Lakehouse medallion component |
| 21 | +│ │ │ │ ├── component.py |
| 22 | +│ │ │ │ └── defs.yaml |
| 23 | +│ │ │ ├── assets/ |
| 24 | +│ │ │ │ ├── bronze/ # Bronze layer (raw Delta tables) |
| 25 | +│ │ │ │ ├── silver/ # Silver layer (validated Delta tables) |
| 26 | +│ │ │ │ └── gold/ # Gold layer (curated Delta tables) |
| 27 | +│ │ │ └── resources.py # Databricks resources |
| 28 | +│ │ └── snowflake_medallion/ # Code location 2: Snowflake dbt Medallion |
| 29 | +│ │ ├── __init__.py |
| 30 | +│ │ ├── definitions.py # Definitions for Snowflake location |
| 31 | +│ │ ├── dbt_project/ # Full dbt project |
| 32 | +│ │ │ ├── dbt_project.yml |
| 33 | +│ │ │ ├── models/ |
| 34 | +│ │ │ │ ├── bronze/ # Bronze layer models |
| 35 | +│ │ │ │ ├── silver/ # Silver layer models |
| 36 | +│ │ │ │ └── gold/ # Gold layer models |
| 37 | +│ │ │ └── sources.yml |
| 38 | +│ │ └── defs/ |
| 39 | +│ │ ├── components/ |
| 40 | +│ │ │ ├── medallion/ # Medallion component |
| 41 | +│ │ │ └── dbt_project/ # dbt component |
| 42 | +│ │ └── resources.py # Snowflake + dbt resources |
| 43 | +│ └── defs/ # Main code location with architecture patterns |
| 44 | +│ ├── __init__.py |
| 45 | +│ ├── definitions.py # Main Definitions export |
| 46 | +│ ├── assets/ # Architecture pattern examples |
| 47 | +│ │ ├── __init__.py |
| 48 | +│ │ ├── etl_pipeline.py # ETL pattern |
| 49 | +│ │ ├── elt_pipeline.py # ELT pattern |
| 50 | +│ │ ├── lakehouse_pipeline.py # Lakehouse pattern |
| 51 | +│ │ └── composable_resources.py # Composable resources pattern |
| 52 | +│ └── resources/ # Shared resources |
| 53 | +│ ├── __init__.py |
| 54 | +│ └── mock_storage.py |
| 55 | +└── tests/ # Test suite |
| 56 | + ├── __init__.py |
| 57 | + ├── conftest.py |
| 58 | + └── test_assets.py |
| 59 | +``` |
| 60 | + |
| 61 | +## Getting Started |
| 62 | + |
| 63 | +### Prerequisites |
| 64 | + |
| 65 | +- Python 3.13+ |
| 66 | +- [uv](https://github.com/astral-sh/uv) package manager |
| 67 | + |
| 68 | +### Installation |
| 69 | + |
| 70 | +1. Create a virtual environment and install dependencies: |
| 71 | + |
| 72 | +```bash |
| 73 | +uv venv |
| 74 | +source .venv/bin/activate # On Windows: .venv\Scripts\activate |
| 75 | +uv pip install -e . |
| 76 | +``` |
| 77 | + |
| 78 | +2. Verify installation: |
| 79 | + |
| 80 | +```bash |
| 81 | +dg check defs |
| 82 | +``` |
| 83 | + |
| 84 | +### Running the Project |
| 85 | + |
| 86 | +Start the Dagster UI: |
| 87 | + |
| 88 | +```bash |
| 89 | +dg dev |
| 90 | +``` |
| 91 | + |
| 92 | +Or using the traditional command: |
| 93 | + |
| 94 | +```bash |
| 95 | +dagster dev |
| 96 | +``` |
| 97 | + |
| 98 | +This will start the Dagster webserver at `http://localhost:3000` where you can: |
| 99 | + |
| 100 | +- View all assets from both code locations |
| 101 | +- Materialize assets individually or in groups |
| 102 | +- Explore the asset lineage graph |
| 103 | +- See different architecture patterns side by side |
| 104 | + |
| 105 | +## Running Tests |
| 106 | + |
| 107 | +Run the test suite: |
| 108 | + |
| 109 | +```bash |
| 110 | +pytest tests/ -v |
| 111 | +``` |
| 112 | + |
| 113 | +Run specific test files: |
| 114 | + |
| 115 | +```bash |
| 116 | +pytest tests/test_assets.py -v |
| 117 | +``` |
| 118 | + |
| 119 | +## Code Locations |
| 120 | + |
| 121 | +This project demonstrates two separate code locations, each implementing the medallion architecture pattern: |
| 122 | + |
| 123 | +### 1. Databricks Delta (Code Location 1) |
| 124 | + |
| 125 | +**Pattern**: Lakehouse with Medallion Architecture |
| 126 | +**Storage**: Delta Lake tables |
| 127 | +**Technology**: Databricks, Delta Lake |
| 128 | + |
| 129 | +**Architecture:** |
| 130 | + |
| 131 | +- **Bronze Layer**: Raw sensor data loaded to Delta tables |
| 132 | +- **Silver Layer**: Cleaned and validated sensor data in Delta tables |
| 133 | +- **Gold Layer**: Aggregated sensor summaries for consumption |
| 134 | + |
| 135 | +**Component**: `LakehouseComponent` |
| 136 | + |
| 137 | +- Supports `demo_mode` for local development |
| 138 | +- Real implementation uses Databricks and Delta Lake |
| 139 | +- Demo mode uses local file system or DuckDB |
| 140 | + |
| 141 | +**Usage:** |
| 142 | + |
| 143 | +```python |
| 144 | +# Component configuration in defs/components/lakehouse/defs.yaml |
| 145 | +demo_mode: true # Set to false for production |
| 146 | +``` |
| 147 | + |
| 148 | +### 2. Snowflake dbt Medallion (Code Location 2) |
| 149 | + |
| 150 | +**Pattern**: ELT with Medallion Architecture |
| 151 | +**Storage**: Snowflake tables |
| 152 | +**Technology**: Snowflake, dbt, SQL transformations |
| 153 | + |
| 154 | +**Architecture:** |
| 155 | + |
| 156 | +- **Bronze Layer**: Raw customer data loaded from sources |
| 157 | +- **Silver Layer**: Cleaned and validated customer data (dbt models) |
| 158 | +- **Gold Layer**: Curated customer summaries for business consumption (dbt models) |
| 159 | + |
| 160 | +**Component**: `MedallionComponent` |
| 161 | + |
| 162 | +- Integrates with full dbt project |
| 163 | +- Uses `dagster-dbt` for dbt asset generation |
| 164 | +- Supports `demo_mode` with DuckDB adapter |
| 165 | + |
| 166 | +**dbt Models:** |
| 167 | + |
| 168 | +- `bronze_customers.sql`: Raw data from sources |
| 169 | +- `silver_customers.sql`: Cleaned and validated data |
| 170 | +- `gold_customer_summary.sql`: Aggregated summaries |
| 171 | + |
| 172 | +**Usage:** |
| 173 | + |
| 174 | +```python |
| 175 | +# Component configuration in defs/components/medallion/defs.yaml |
| 176 | +demo_mode: true # Set to false for production |
| 177 | +``` |
| 178 | + |
| 179 | +## Demo Mode |
| 180 | + |
| 181 | +Both code locations support demo mode for local development without external dependencies: |
| 182 | + |
| 183 | +- **Databricks Delta**: Uses local file system or DuckDB instead of Databricks |
| 184 | +- **Snowflake dbt**: Uses DuckDB adapter instead of Snowflake |
| 185 | + |
| 186 | +To switch between demo and production modes, update the `demo_mode` parameter in the component YAML files. |
| 187 | + |
| 188 | +## Architecture Patterns |
| 189 | + |
| 190 | +### Medallion Architecture |
| 191 | + |
| 192 | +Both code locations implement the medallion architecture pattern: |
| 193 | + |
| 194 | +1. **Bronze (Raw)**: Unprocessed data as it arrives from source systems |
| 195 | + - Preserves original data for auditability |
| 196 | + - Enables reprocessing and historical analysis |
| 197 | + - No transformations applied |
| 198 | + |
| 199 | +2. **Silver (Cleaned)**: Validated and standardized data |
| 200 | + - Data quality checks applied |
| 201 | + - Schema standardization |
| 202 | + - Deduplication and cleaning |
| 203 | + - Ready for internal consumption |
| 204 | + |
| 205 | +3. **Gold (Curated)**: Business-ready aggregated data |
| 206 | + - Aggregated and enriched |
| 207 | + - Optimized for consumption |
| 208 | + - Designed for dashboards and applications |
| 209 | + |
| 210 | +### When to Use Each Pattern |
| 211 | + |
| 212 | +**Databricks Delta Lakehouse:** |
| 213 | + |
| 214 | +- Very large volumes of data |
| 215 | +- Need both batch and streaming processing |
| 216 | +- Cost-effective storage is a priority |
| 217 | +- Want to separate storage from compute |
| 218 | +- Need ACID guarantees with Delta Lake |
| 219 | + |
| 220 | +**Snowflake dbt Medallion:** |
| 221 | + |
| 222 | +- Want to preserve raw data for flexibility |
| 223 | +- Multiple teams need different transformations |
| 224 | +- Cloud warehouse compute is affordable |
| 225 | +- Enable self-service analytics |
| 226 | +- Prefer SQL-based transformations |
| 227 | + |
| 228 | +## Components |
| 229 | + |
| 230 | +This project uses Dagster Components for reusable architecture patterns: |
| 231 | + |
| 232 | +- **LakehouseComponent**: Implements medallion architecture for Databricks Delta |
| 233 | +- **MedallionComponent**: Implements medallion architecture for Snowflake dbt |
| 234 | + |
| 235 | +Components provide: |
| 236 | + |
| 237 | +- Declarative configuration via YAML |
| 238 | +- Demo mode support for local development |
| 239 | +- Reusable patterns across projects |
| 240 | +- Clear separation of concerns |
| 241 | + |
| 242 | +## Dignified Python Standards |
| 243 | + |
| 244 | +This project follows dignified Python standards: |
| 245 | + |
| 246 | +- **Python 3.13+ syntax**: `list[str]`, `dict[str, int]`, `str | None` |
| 247 | +- **ABC interfaces**: Uses `abc.ABC` instead of `typing.Protocol` |
| 248 | +- **LBYL patterns**: Check before access, no exception-based control flow |
| 249 | +- **Absolute imports**: Module-level imports only |
| 250 | +- **ConfigurableResource**: Modern resource pattern instead of `@resource` decorators |
| 251 | + |
| 252 | +## Production Considerations |
| 253 | + |
| 254 | +### Resource Configuration |
| 255 | + |
| 256 | +In production, configure real connections: |
| 257 | + |
| 258 | +**Databricks:** |
| 259 | + |
| 260 | +- Set `demo_mode: false` in component YAML |
| 261 | +- Configure `server_hostname`, `http_path`, and `token` via environment variables |
| 262 | +- Use actual Databricks workspace and Delta Lake storage |
| 263 | + |
| 264 | +**Snowflake:** |
| 265 | + |
| 266 | +- Set `demo_mode: false` in component YAML |
| 267 | +- Configure Snowflake credentials via environment variables |
| 268 | +- Update dbt profiles for Snowflake connection |
| 269 | +- Run `dbt compile` to generate manifest.json |
| 270 | + |
| 271 | +### Data Formats |
| 272 | + |
| 273 | +- **Delta Lake**: Use Delta tables for ACID guarantees and time travel |
| 274 | +- **Snowflake**: Use appropriate table types (transient, permanent) based on needs |
| 275 | +- Implement partitioning for large datasets |
| 276 | +- Consider clustering keys for query performance |
| 277 | + |
| 278 | +### Scalability |
| 279 | + |
| 280 | +- Use Spark or similar for large-scale processing (Databricks) |
| 281 | +- Implement partitioning and bucketing strategies |
| 282 | +- Consider streaming for real-time requirements |
| 283 | +- Monitor and optimize query performance |
| 284 | + |
| 285 | +### Observability |
| 286 | + |
| 287 | +- Add comprehensive logging and metrics |
| 288 | +- Track data lineage across layers |
| 289 | +- Monitor data quality at each stage |
| 290 | +- Set up alerts for pipeline failures |
| 291 | + |
| 292 | +## Architecture Patterns in `src/defs/` |
| 293 | + |
| 294 | +The `src/defs/` directory contains examples of common architecture patterns: |
| 295 | + |
| 296 | +### ETL Pattern (`src/defs/assets/etl_pipeline.py`) |
| 297 | + |
| 298 | +Traditional Extract-Transform-Load pattern where data is transformed before loading. |
| 299 | + |
| 300 | +### ELT Pattern (`src/defs/assets/elt_pipeline.py`) |
| 301 | + |
| 302 | +Extract-Load-Transform pattern where raw data is loaded first, then transformed in the warehouse. |
| 303 | + |
| 304 | +### Lakehouse Pattern (`src/defs/assets/lakehouse_pipeline.py`) |
| 305 | + |
| 306 | +Medallion architecture with bronze, silver, and gold layers. |
| 307 | + |
| 308 | +### Composable Resources (`src/defs/assets/composable_resources.py`) |
| 309 | + |
| 310 | +Demonstrates how to build reusable, composable resources using ABC interfaces and ConfigurableResource. |
| 311 | + |
| 312 | +## Development Workflow |
| 313 | + |
| 314 | +1. **Local Development**: Use demo mode for both code locations |
| 315 | +2. **Testing**: Run `pytest tests/ -v` to run the test suite |
| 316 | +3. **Validation**: Run `dg check defs` to validate definitions |
| 317 | +4. **Materialization**: Use `dg dev` to start UI and materialize assets |
| 318 | +5. **Production**: Update component YAML files with production configuration |
| 319 | + |
| 320 | +## Notes |
| 321 | + |
| 322 | +- These are simplified examples for demonstration purposes |
| 323 | +- In production, add comprehensive error handling, logging, and resource management |
| 324 | +- Match the architecture to your business requirements and constraints |
| 325 | +- Start simple and evolve as needs change |
| 326 | +- Components can be extended and customized for specific use cases |
0 commit comments