- Creating and Maintaining dbt documentation
- Debugging data modelling errors
- Developing dbt models
- Implementing and Maintaining External Dependencies
- Implementing dbt tests
- Leveraging the dbt state
- Managing data pipelines
- Understanding dbt models governance
- Core Concepts
- dbt Tests
- Troubleshooting
- Best Practices
- Reference
Creating and Maintaining dbt documentation
Debugging data modelling errors
Developing dbt models
Materialisation
- view
- incremental
- table
- ephemeral
If the materialized option is not specified, the default dbt materialisation is view.
Implementing and Maintaining External Dependencies
Implementing dbt tests
Indirect Selection
indirect_selectionparameter controls how dbt handles tests on upstream models when a specific model or resource is selected for execution.- the default mode is
eagermode.- Tests associated with the selected resource (directly or indirectly upstream) are always executed.
- For example, if you run
dbt test --select model_a, dbt will run:- All tests associated with
model_adirectly. - Tests on upstream dependencies of
model_a, e.g.model_bifmodel_adepends onmodel_b.
- All tests associated with
buildablemode.- Runs tests only on models that can be materialised, i.e. models that are not ephemeral or seeds.
- This mode is slightly more inclusive than
cautiousby including tests whose references are each within the selected nodes (or their ancestors).
cautiousmode.- Ensures that tests are executed and models are built only when all necessary dependencies of selected models are met.
- Tests are only executed for the resources that are explicitly selected, not their dependencies.
- This is a more conservative mode, ensuring no unexpected tests are run.
emptymode.- Run tests only on explicitly selected models and skips all dependencies.
- When you run a test with empty model, dbt will run only the tests associated with the directly selected resource,
- and skip all tests for upstream dependencies.
Leveraging the dbt state
Managing data pipelines
Understanding dbt models governance
restrict-access configuration
Restrict-access configuration is used to
- restrict access to certain models in a dbt project so they cannot be referenced outside the package in which they are defined.
- This is particularly useful for crating private model within a dbt project, ensuring that only models in the same package or group can reference them.
Core Concepts
Common dbt commands
dbt deps.- To download and install dependencies required for a dbt project.
dbt docs generate.- generating your project’s documentation
dbt docs serve.- starts a webserver on port 8080 to serve your documentation locally.
dbt run.- running dbt transformation.
dbt seed.- seeding data into tables.
dbt source freshness.- To check the freshness of data in the sources based on a defined threshold.
dbt test.- running tests on the models.
Hooks
- dbt hook are used to execute pre- and post model actions.
- It allows you to run SQL statement or commands before or after the main model transformations are performed.
- This can be useful for tasks such as data loading, setting up configurations, or performing additional calculation.
Materialisation
Materialisation refers to the way dbt persists the results of a model (a SQL query) in the database.
Materialisation determine how and when the data is stored and updated in the database.
dbt provides several built-in materialisation for different use cases:
views.- create a database view.
- Use case: lightweight transformation where you want to always query the latest data without storing it physically.
incremental.- create a table and updates it incrementally with new data
- Use case: large datasets where only new or changed data needs to be processed and added to the existing table.
tables.- create a physical table in the database.
- Use case: transformation that are computationally expensive or when you need to ensure consistent performance by storing the results.
ephemeral.- It allows you to create temporary models that are not persisted in the database.
- Instead, ephemeral models are inlined into SQL queries of downstream models that reference them.
- This can be useful for intermediate transformations that do not need to be stored as separate tables or views.
Semantic Layer
Snapshot
- Snapshot allows you to capture and store the state of your data at specific points-in-time.
- This is particularly useful for tracking changes in your data over time, such as slowly changing dimensions (SCDs).
How to create snapshot
- You define a snapshot in a
.sqlfile within the snapshot directory of your dbt project. - The definition includes the source table, the unique key, and the columns to track.
Example Snapshot definition:
| |
- Run Snapshots with
dbt snapshotcommand. - This command will execute the snapshot and store the historical records in the specified target schema.
Valid configurations in dbt_project.yml
- Seeds
- models
- sources
- profile
dbt Tests
Why do we need test?
- Fell comfortable and confident in the code you are writing.
- Ensure your code continues to work as expected.
- To help data consumers make data-informed decisions on accurate data.
- To increase the likelihood of success.
- To build trust in the platform.
- Save time as models documented with assertions helps future you (and others) contribute to the codebase.
What makes a good test?
- Automated. low effort/repeatable.
- Fast. if testing takes too long, no one will do it.
- Reliable. believe them when they say something does work.
- Informative. leave you clues about what to fix based on the error.
- Focused. every test should validate one assumption.
Test on One Database Object
- Assert something about the data that you think is true
- Contents of the data
- Constraints of the table
- The grain of the table
Troubleshooting
Table missing primary key constraint during execution
- Inspect the model’s compiled SQL code to verify that the primary key constraint is being created correctly.
Optimise SQL models with {{ref()}} function
- Replacing SQL queries that reference other models with the
{{ref()}}function improves maintainability and ensures proper dependency management.
Best Practices
Structure a dbt project
- Organising models into logical folders and subfolders.
- Using modularisation and reusable macros.
Reference
| |