Let's discuss what this example is doing, specifically the load_assets_from_dbt_project function. This function loads dbt models into Dagster as assets, creating one Dagster asset for each model.
When invoked, this function:
Compiles your dbt project,
Parses the metadata provided by dbt, and
Generates a set of software-defined assets reflecting the models in the project. These assets share the same underlying op, which will invoke dbt to run the models represented by the loaded assets.
Next, you'll define the code location for your Dagster project. A code location, created using the Definitions object, is a collection of definitions in a Dagster project, such as assets, resources, and so on.
Assets loaded from dbt require a dbt resource, which is responsible for firing off dbt CLI commands. Using the dbt_cli_resource resource, we can supply a dbt resource to the dbt project.
Open the __init__.py file, located in /tutorial_template/tutorial_dbt_dagster, and add the following code:
# /tutorial_template/tutorial_dbt_dagster/__init__.pyimport os
from dagster_dbt import dbt_cli_resource
from tutorial_dbt_dagster import assets
from tutorial_dbt_dagster.assets import DBT_PROFILES, DBT_PROJECT_PATH
from dagster import Definitions, load_assets_from_modules
resources ={"dbt": dbt_cli_resource.configured({"project_dir": DBT_PROJECT_PATH,"profiles_dir": DBT_PROFILES,},),}
defs = Definitions(assets=load_assets_from_modules([assets]), resources=resources)
Let's take a look at what's happening here:
In the resources key, we've provided configuration info for the dbt_cli_resource resource.
Added all assets in the assets module and the resources mapped to the resources key to the Definitions object. This supplies the resource we created to our assets.
Using load_assets_from_modules, we've added all assets in the assets module as definitions. This approach allows any new assets we created to be automatically included in the code location instead of needing to manually add them one by one.