How to achieve that dbt assets depends on python assets? #18662
-
As long as I have been working on dbt on Dagster I always have faced problems where the dbt assets where loaded the first ones in the DAG and then the python assets ones were loaded. For example, an structure like this:
The problem is that I want to do something the other way; I firstly want to create some assets with Python and only then, when these assets are created, execute the dbt models. What I would want to know if there is anything similar as the deps option from the Thanks in advanced |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
If you model your upstream dependencies as dbt sources, you can create assets for your dbt sources. These assets will execute before your dbt assets. See the docs for details: https://docs.dagster.io/integrations/dbt/reference#upstream-dependencies |
Beta Was this translation helpful? Give feedback.
-
@jordunar were you able to find a solution? |
Beta Was this translation helpful? Give feedback.
-
If it helps, I'm using this translator to automatically stitch the dbt models and the dagster assets: class DbtDagsterBridgeTranslator(DagsterDbtTranslator):
def get_asset_key(self, resource_props: Mapping[str, Any]) -> AssetKey:
"""Get asset key for a dbt resource"""
# For dagster sources, return the asset key without the dagster prefix
# This makes dagster.vscode_scraper map to just vscode_scraper
unique_id = resource_props.get("unique_id", "")
if unique_id.startswith("source.") and resource_props.get("source_name") == "dagster":
# Return just the table name as the asset key
return AssetKey([resource_props.get("name")])
# Default behavior for everything else
return super().get_asset_key(resource_props)
@dbt_assets(
manifest="../path/to/manifest.json",
exclude="tag:dagster", # Exclude all dagster stub sources
dagster_dbt_translator=DbtDagsterBridgeTranslator()
)
def dbt_assets(context: AssetExecutionContext, dbt: DbtCliResource):
yield from dbt.cli(["build"], context=context).stream() Then in my sources.yml I have: version: 2
sources:
- name: dagster
description: "Dagster assets as upstream dependencies"
tags: ["dagster"]
tables:
- name: dagster_asset_name_1
- name: dagster_asset_name_2
- name: dagster_asset_name_3 And as long as the table names in Hope it helps! 🙏🏼 |
Beta Was this translation helpful? Give feedback.
If you model your upstream dependencies as dbt sources, you can create assets for your dbt sources. These assets will execute before your dbt assets.
See the docs for details: https://docs.dagster.io/integrations/dbt/reference#upstream-dependencies