Skip to content

Commit

Permalink
[dagster-tableau] Update custom translator example in Tableau docs (d…
Browse files Browse the repository at this point in the history
…agster-io#26029)

## Summary & Motivation

Update the custom translator example in the Tableau docs to reflect
`get_asset_spec().key`.

This PR stack will be merged after dagster-io#25941 lands, so that
`replace_attributes()` can be called as
`AssetSpec().replace_attributes()`
  • Loading branch information
maximearmstrong authored Nov 25, 2024
1 parent 6a650ff commit 94bcd2a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
23 changes: 18 additions & 5 deletions docs/content/integrations/tableau.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ defs = dg.Definitions(assets=[*tableau_specs], resources={"tableau": tableau_wor

### Customize asset definition metadata for Tableau assets

By default, Dagster will generate asset keys for each Tableau asset based on its type and name and populate default metadata. You can further customize asset properties by passing a custom <PyObject module="dagster_tableau" object="DagsterTableauTranslator" /> subclass to the <PyObject module="dagster_tableau" method="load_tableau_asset_specs" /> function. This subclass can implement methods to customize the asset keys or specs for each Tableau asset type.
By default, Dagster will generate asset specs for each Tableau asset based on its type, and populate default metadata. You can further customize asset properties by passing a custom <PyObject module="dagster_tableau" object="DagsterTableauTranslator" /> subclass to the <PyObject module="dagster_tableau" method="load_tableau_asset_specs" /> function. This subclass can implement methods to customize the asset specs for each Tableau asset type.

```python file=/integrations/tableau/customize-tableau-asset-defs.py
from dagster_tableau import (
DagsterTableauTranslator,
TableauCloudWorkspace,
load_tableau_asset_specs,
)
from dagster_tableau.translator import TableauContentData
from dagster_tableau.translator import TableauContentData, TableauContentType

import dagster as dg

Expand All @@ -129,9 +129,20 @@ tableau_workspace = TableauCloudWorkspace(
# A translator class lets us customize properties of the built
# Tableau assets, such as the owners or asset key
class MyCustomTableauTranslator(DagsterTableauTranslator):
def get_sheet_spec(self, data: TableauContentData) -> dg.AssetSpec:
# We add a custom team owner tag to all sheets
return super().get_sheet_spec(data)._replace(owners=["team:my_team"])
def get_asset_spec(self, data: TableauContentData) -> dg.AssetSpec:
# We create the default asset spec using super()
default_spec = super().get_asset_spec(data)
# We customize the metadata and asset key prefix for all assets, including sheets,
# and we customize the team owner tag only for sheets.
return default_spec.replace_attributes(
key=default_spec.key.with_prefix("prefix"),
metadata={**default_spec.metadata, "custom": "metadata"},
owners=(
["team:my_team"]
if data.content_type == TableauContentType.SHEET
else ...
),
)


tableau_specs = load_tableau_asset_specs(
Expand All @@ -140,6 +151,8 @@ tableau_specs = load_tableau_asset_specs(
defs = dg.Definitions(assets=[*tableau_specs], resources={"tableau": tableau_workspace})
```

Note that `super()` is called in each of the overridden methods to generate the default asset spec. It is best practice to generate the default asset spec before customizing it.

### Load Tableau assets from multiple workspaces

Definitions from multiple Tableau workspaces can be combined by instantiating multiple Tableau resources and merging their specs. This lets you view all your Tableau assets in a single asset graph:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
TableauCloudWorkspace,
load_tableau_asset_specs,
)
from dagster_tableau.translator import TableauContentData
from dagster_tableau.translator import TableauContentData, TableauContentType

import dagster as dg

Expand All @@ -20,9 +20,20 @@
# A translator class lets us customize properties of the built
# Tableau assets, such as the owners or asset key
class MyCustomTableauTranslator(DagsterTableauTranslator):
def get_sheet_spec(self, data: TableauContentData) -> dg.AssetSpec:
# We add a custom team owner tag to all sheets
return super().get_sheet_spec(data)._replace(owners=["team:my_team"])
def get_asset_spec(self, data: TableauContentData) -> dg.AssetSpec:
# We create the default asset spec using super()
default_spec = super().get_asset_spec(data)
# We customize the metadata and asset key prefix for all assets, including sheets,
# and we customize the team owner tag only for sheets.
return default_spec.replace_attributes(
key=default_spec.key.with_prefix("prefix"),
metadata={**default_spec.metadata, "custom": "metadata"},
owners=(
["team:my_team"]
if data.content_type == TableauContentType.SHEET
else ...
),
)


tableau_specs = load_tableau_asset_specs(
Expand Down

0 comments on commit 94bcd2a

Please sign in to comment.