Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[daggy-u] fix indentation of indented code snippets #22571

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,35 +52,35 @@ Open the `assets/dbt.py` file and do the following:
1. There are two properties that we’ll want from `dbt_resource_props`: the `type` (ex., model, source, seed, snapshot) and the `name`, such as `trips` or `stg_trips`. Access both of those properties from the `dbt_resource_props` argument and store them in their own respective variables (`type` and `name`):

```python
def get_asset_key(self, dbt_resource_props):
type = dbt_resource_props["resource_type"]
name = dbt_resource_props["name"]
def get_asset_key(self, dbt_resource_props):
type = dbt_resource_props["resource_type"]
name = dbt_resource_props["name"]
```

2. As mentioned above, the asset keys of our existing Dagster assets used by our dbt project are named `taxi_trips` and `taxi_zones`. If you were to print out the `name`, you’d see that the dbt sources are named `trips` and `zones`. Therefore, to match our asset keys up, we can prefix our keys with the string `taxi_` .

Copy and paste the following code to return an `AssetKey` of `AssetKey(f"taxi_{name}")`:

```python
def get_asset_key(self, dbt_resource_props):
type = dbt_resource_props["resource_type"]
name = dbt_resource_props["name"]
def get_asset_key(self, dbt_resource_props):
type = dbt_resource_props["resource_type"]
name = dbt_resource_props["name"]

return AssetKey(f"taxi_{name}")
return AssetKey(f"taxi_{name}")
```

3. You have full control over how each asset can be named, as you can define how asset keys are created. In our case we only want to rename the dbt sources, but we can keep the asset keys of the models the same.

The object-oriented pattern of the `DagsterDbtTranslator` means that we can leverage the existing implementations of the parent class by using the `super` method. We’ll use this pattern to customize how the sources are defined but default to the original logic for deciding the model asset keys. Copy and paste the code below to complete the `get_asset_key` function:

```python
def get_asset_key(self, dbt_resource_props):
resource_type = dbt_resource_props["resource_type"]
name = dbt_resource_props["name"]
if resource_type == "source":
return AssetKey(f"taxi_{name}")
else:
return super().get_asset_key(dbt_resource_props)
def get_asset_key(self, dbt_resource_props):
resource_type = dbt_resource_props["resource_type"]
name = dbt_resource_props["name"]
if resource_type == "source":
return AssetKey(f"taxi_{name}")
else:
return super().get_asset_key(dbt_resource_props)
```

You’ve successfully written your first translator!
cmpadden marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -140,4 +140,4 @@ else:
)
def dbt_analytics(context: AssetExecutionContext, dbt: DbtCliResource):
yield from dbt.cli(["build"], context=context).stream()
```
```