How to use a custom resource key when using @dbt_assets
#19079
-
Per documentation here: https://docs.dagster.io/_apidocs/libraries/dagster-dbt#dagster_dbt.dbt_assets The However, I don't see a way to do this. This is a problem, because there is no way to choose which |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
In our API examples, we use Pythonic Dagster Resources. There, the parameter name of the resource is associated with the resource key in the So, you can just customize the parameter name if you want to use a different resource key, similar to customizing @dbt_assets(manifest=dbt_manifest_path)
def jaffle_shop_dbt_assets(
+ context: AssetExecutionContext, my_custom_dbt_resource_key: DbtCliResource
):
yield from my_custom_dbt_resource_key.cli(["build"], context=context).stream()
defs = Definitions(
assets=[jaffle_shop_dbt_assets],
resources={
+ "my_custom_dbt_resource_key": DbtCliResource(...),
},
) If you're dynamically generating your dbt assets, then you can also just dynamically retrieve the resource associated with your dbt project. Here's another example: dbt_projects = [
{
"project_dir": Path("my/path/to/project/a"),
"dbt_manifest_path": Path("my/path/to/project/a/manifest"),
"name": "dbt_project_a"
},
{
"project_dir": Path("my/path/to/project/b"),
"dbt_manifest_path": Path("my/path/to/project/a/manifest"),
"name": "dbt_project_b"
},
]
def create_my_dbt_assets_from_dbt_project(dbt_project: dict) -> AssetsDefinition:
dbt_project_name = dbt_project["name"]
@dbt_assets(
name=dbt_project_name,
manifest=dbt_project["dbt_manifest_path"],
required_resource_keys={dbt_project_name},
)
def jaffle_shop_dbt_assets(context: AssetExecutionContext):
dbt = getattr(context.resources, dbt_project_name)
yield from dbt.cli(["build"], context=context).stream()
return jaffle_shop_dbt_assets
defs = Definitions(
assets=[
create_my_dbt_assets_from_dbt_project(dbt_project)
for dbt_project in dbt_projects
],
resources={
dbt_project["name"]: DbtCliResource(project_dir=dbt_project["project_dir"]
for dbt_project in dbt_projects
},
) |
Beta Was this translation helpful? Give feedback.
In our API examples, we use Pythonic Dagster Resources. There, the parameter name of the resource is associated with the resource key in the
Definitions
object. Here's some relevant documentation.So, you can just customize the parameter name if you want to use a different resource key, similar to customizing
dbt_resource_key
inload_assets_from_dbt_project
orload_assets_from_dbt_manifest
. Here's an example, with the relevant lines highlighted.@dbt_assets(manifest=dbt_manifest_path) def jaffle_shop_dbt_assets( + context: AssetExecutionContext, my_custom_dbt_resource_key: DbtCliResource ): yield from my_custom_dbt_resource_key.cli(["build"], context=context).stream() defs = De…