Representing the dbt project in Dagster
As you'll frequently point your Dagster code to the target/manifest.json file and your dbt project in this course, it'll be helpful to keep a reusable representation of the dbt project. This can be easily done using the DbtProject class.
๐ Note: The
defs/project.pyfile exists in the starter project but is empty. You'll be adding all the code below to this file.
In the defs/project.py file, add the following imports:
# src/dagster_and_dbt/defs/project.py
from pathlib import Path
from dagster_dbt import DbtProject
The Path class from the pathlib standard library will help us create an accurate pointer to where our dbt project is. The DbtProject class is imported from the dagster_dbt package that we installed earlier.
After the import, add the following code:
dbt_project = DbtProject(
project_dir=Path(__file__).joinpath("../..", "analytics").resolve(),
)
This code creates a representation of the dbt project called dbt_project. The code defining the location of the project directory might look a little complicated, so letโs break it down:
- The location of the
project.pyfile (via__file__) is used as a point of reference for finding the dbt project - The arguments in
joinpathpoint us towards our dbt project by appending the following to the current path:- Two directory levels up (
"../..") - A directory named
analytics, which is the directory containing our dbt project
- Two directory levels up (
- The
resolvemethod turns that path into an absolute file path that points to the dbt project correctly from any file we're working in
At this point, your complete defs/project.py file should look like this:
# src/dagster_and_dbt/defs/project.py
from pathlib import Path
from dagster_dbt import DbtProject
dbt_project = DbtProject(
project_dir=Path(__file__).joinpath("../..", "analytics").resolve(),
)
๐ก Stuck? You can reference the completed code in
src/dagster_and_dbt/completed/lesson_3/defs/project.pyto compare your work.
Now that you can access your dbt project from any other file with the dbt_project representation, let's move on to the first place where you'll use it: creating the Dagster resource that will run dbt.