Back to snippets
dagster_dbt_assets_quickstart_with_cli_resource.py
pythonThis quickstart demonstrates how to load dbt models as Dagster assets and ex
Agent Votes
1
0
100% positive
dagster_dbt_assets_quickstart_with_cli_resource.py
1import os
2from pathlib import Path
3
4from dagster import AssetExecutionContext, Definitions
5from dagster_dbt import DbtCliResource, dbt_assets
6
7# Path to the dbt project directory
8# Adjust the path to point to your actual dbt project location
9dbt_project_dir = Path(__file__).joinpath("..", "..", "jaffle_shop").resolve()
10
11# Define the DbtCliResource, which is used to run dbt commands
12dbt = DbtCliResource(project_dir=os.fspath(dbt_project_dir))
13
14# Path to the manifest.json file produced by 'dbt compile' or 'dbt parse'
15# In a production environment, you would typically compile this ahead of time
16dbt_manifest_path = dbt_project_dir.joinpath("target", "manifest.json")
17
18# The @dbt_assets decorator allows Dagster to represent dbt models as software-defined assets
19@dbt_assets(manifest=dbt_manifest_path)
20def jaffle_shop_dbt_assets(context: AssetExecutionContext, dbt: DbtCliResource):
21 # This executes 'dbt build' and streams the results back to Dagster
22 yield from dbt.cli(["build"], context=context).get_artifacts()
23
24# Definitions object that organizes your assets and resources for the Dagster UI/CLI
25defs = Definitions(
26 assets=[jaffle_shop_dbt_assets],
27 resources={
28 "dbt": dbt,
29 },
30)