Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 6,050 Bytes
d15f07c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | ---
sidebar_position: 3
---
# Open Navigator dbt Project
Transforms bronze AI extractions into production-ready search tables.
## Purpose
This dbt project handles:
- **Bronze β Production transformations** (AI extracted data)
- **Data quality testing**
- **Incremental processing** (only new records)
- **Entity deduplication**
- **Documentation generation**
## Project Structure
```
dbt_project/
βββ dbt_project.yml # Project configuration
βββ profiles.yml.example # Database connection template
βββ models/
β βββ staging/ # Clean bronze data
β β βββ _staging.yml
β β βββ stg_bronze_contacts.sql
β β βββ stg_bronze_organizations_meetings.sql
β β βββ stg_bronze_bills.sql
β βββ intermediate/ # Deduplicate
β β βββ _intermediate.yml
β β βββ int_contacts_deduped.sql
β βββ marts/ # Production tables
β βββ _marts.yml
β βββ contact_ai.sql
βββ macros/ # Reusable SQL functions
β βββ calculate_confidence.sql
β βββ normalize_bill_number.sql
β βββ normalize_name.sql
βββ tests/ # Custom data quality tests
βββ README.md # This file
```
## Quick Start
### 1. Install dbt
```bash
# Install dbt-postgres
pip install dbt-postgres
# Verify installation
dbt --version
```
### 2. Configure Database Connection
```bash
# Copy example profiles
cp profiles.yml.example ~/.dbt/profiles.yml
# Edit with your database credentials
nano ~/.dbt/profiles.yml
```
Or set environment variables:
```bash
export POSTGRES_PASSWORD=your_password
export NEON_HOST=your-neon-host.neon.tech
export NEON_USER=your_user
export NEON_PASSWORD=your_password
```
### 3. Test Connection
```bash
# Check dbt can connect
dbt debug
# Should show:
# Yes Connection test: [OK connection ok]
```
### 4. Run Models
```bash
# Run all models
dbt run
# Run specific model
dbt run --select stg_bronze_contacts
# Run with full refresh (rebuild everything)
dbt run --full-refresh
# Run tests
dbt test
# Generate documentation
dbt docs generate
dbt docs serve # Opens in browser
```
## Model Layers
### Staging (`models/staging/`)
**Purpose:** Clean and normalize bronze data
- `stg_bronze_contacts.sql` - Clean contact names, filter invalid records
- `stg_bronze_organizations_meetings.sql` - Normalize org names, clean EINs
- `stg_bronze_bills.sql` - Standardize bill numbers
**Materialization:** `view` (no storage, computed on-the-fly)
### Intermediate (`models/intermediate/`)
**Purpose:** Deduplicate and prepare for production
- `int_contacts_deduped.sql` - One record per person per org
**Materialization:** `table` (stored, fast to query)
### Marts (`models/marts/`)
**Purpose:** Production-ready tables for API
- `contact_ai.sql` - AI-extracted contacts (incremental)
**Materialization:** `incremental` (only processes new records)
## Testing
### Run Tests
```bash
# Run all tests
dbt test
# Run tests for specific model
dbt test --select contact_ai
# Run specific test type
dbt test --select test_type:unique
dbt test --select test_type:not_null
```
### Available Tests
1. **Schema tests** (in `.yml` files)
- `unique` - No duplicates
- `not_null` - No NULL values
- `accepted_values` - Value in allowed list
- `relationships` - Foreign key exists
2. **Custom tests** (in `tests/` folder)
- Custom SQL assertions
## Incremental Processing
Models marked `materialized='incremental'` only process new records:
```sql
{% if is_incremental() %}
WHERE extracted_at > (SELECT MAX(last_updated) FROM {{ this }})
{% endif %}
```
### Full Refresh
To rebuild everything from scratch:
```bash
dbt run --full-refresh --select contact_ai
```
## Macros
Reusable SQL functions in `macros/`:
### `calculate_confidence(datasource)`
```sql
SELECT {{ calculate_confidence('datasource') }} as score
-- Returns 1.0 for authoritative, 0.60 for AI extraction
```
### `normalize_bill_number(column)`
```sql
SELECT {{ normalize_bill_number('official_number') }} as bill_num
-- 'HB 123' β 'HB123'
```
### `normalize_name(column)`
```sql
SELECT {{ normalize_name('full_name') }} as name_clean
-- Lowercase, trim, remove special chars
```
## Workflow Integration
### Combined with Python ETL
```bash
#!/bin/bash
# Full ETL pipeline
# 1. Python: Load bronze data
python scripts/datasources/gemini/load_meeting_transcripts_bronze.py
# 2. dbt: Transform to production
cd dbt_project
dbt run --select staging+
dbt run --select intermediate+
dbt run --select marts+
dbt test
# 3. Python: Export to parquet (if needed)
cd ..
python scripts/data/export_to_gold_parquet.py
```
## Troubleshooting
### "relation does not exist"
**Problem:** Source table not found
**Solution:** Check you're connected to the right database
```bash
dbt debug
# Look at "target" database
```
### "Compilation Error: macro 'dbt_utils' is not defined"
**Problem:** Missing dbt packages
**Solution:** Install packages
```bash
# Create packages.yml
cat > packages.yml << EOF
packages:
- package: dbt-labs/dbt_utils
version: 1.1.1
EOF
# Install
dbt deps
```
### "Incremental model not updating"
**Problem:** New records not being processed
**Solution:** Check timestamp logic
```bash
# Full refresh to rebuild
dbt run --full-refresh --select contact_ai
```
## Resources
- [dbt Documentation](https://docs.getdbt.com/)
- [dbt Best Practices](https://docs.getdbt.com/guides/best-practices)
- [SQL Style Guide](https://github.com/dbt-labs/corp/blob/main/dbt_style_guide.md)
## Related Documentation
- [dbt ETL Strategy](../development/dbt-etl-strategy.md) - Full architecture guide
- [Bronze to Production Merge](../development/bronze-to-production-merge.md) - Merge strategy
- [Data Sources](https://github.com/getcommunityone/open-navigator/blob/main/docs/DATA_SOURCES.md) - All data sources
## Next Steps
1. **Install packages:** `dbt deps`
2. **Run models:** `dbt run`
3. **Run tests:** `dbt test`
4. **Generate docs:** `dbt docs generate && dbt docs serve`
5. **Iterate:** Add more models incrementally
|