-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
[11/n][dagster-fivetran] Implement materialization method in FivetranWorkspace #25961
Open
maximearmstrong
wants to merge
6
commits into
maxime/use-translator-instance-in-load-fivetran-asset-specs
Choose a base branch
from
maxime/rework-fivetran-11
base: maxime/use-translator-instance-in-load-fivetran-asset-specs
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+392
−152
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
35ae56d
[11/n][dagster-fivetran] Implement materialization method in Fivetran…
maximearmstrong 8713ef8
Remove fetch_column_metadata and infer_missing_tables
maximearmstrong bf81d05
Lint
maximearmstrong 65059ef
Update materialization logic
maximearmstrong 4982b3b
Move materialization logic to method
maximearmstrong b0e8961
Update materialization logic; add tests
maximearmstrong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,15 +4,17 @@ | |
import time | ||
from datetime import datetime, timedelta | ||
from functools import partial | ||
from typing import Any, Callable, Mapping, Optional, Sequence, Tuple, Union | ||
from typing import Any, Callable, Iterator, Mapping, Optional, Sequence, Tuple, Union | ||
from urllib.parse import urljoin | ||
|
||
import requests | ||
from dagster import ( | ||
AssetExecutionContext, | ||
AssetMaterialization, | ||
Definitions, | ||
Failure, | ||
InitResourceContext, | ||
MaterializeResult, | ||
MetadataValue, | ||
OpExecutionContext, | ||
__version__, | ||
|
@@ -25,7 +27,7 @@ | |
from dagster._core.definitions.asset_spec import AssetSpec | ||
from dagster._core.definitions.definitions_load_context import StateBackedDefinitionsLoader | ||
from dagster._core.definitions.resource_definition import dagster_maintained_resource | ||
from dagster._record import record | ||
from dagster._record import as_dict, record | ||
from dagster._utils.cached_method import cached_method | ||
from dagster._vendored.dateutil import parser | ||
from pydantic import Field, PrivateAttr | ||
|
@@ -36,12 +38,20 @@ | |
DagsterFivetranTranslator, | ||
FivetranConnector, | ||
FivetranConnectorScheduleType, | ||
FivetranConnectorTableProps, | ||
FivetranDestination, | ||
FivetranMetadataSet, | ||
FivetranSchemaConfig, | ||
FivetranWorkspaceData, | ||
) | ||
from dagster_fivetran.types import FivetranOutput | ||
from dagster_fivetran.utils import get_fivetran_connector_url, get_fivetran_logs_url | ||
from dagster_fivetran.utils import ( | ||
get_fivetran_connector_table_name, | ||
get_fivetran_connector_url, | ||
get_fivetran_logs_url, | ||
get_translator_from_fivetran_assets, | ||
metadata_for_table, | ||
) | ||
|
||
FIVETRAN_API_BASE = "https://api.fivetran.com" | ||
FIVETRAN_API_VERSION = "v1" | ||
|
@@ -832,6 +842,7 @@ class FivetranWorkspace(ConfigurableResource): | |
|
||
_client: FivetranClient = PrivateAttr(default=None) | ||
|
||
@cached_method | ||
def get_client(self) -> FivetranClient: | ||
return FivetranClient( | ||
api_key=self.api_key, | ||
|
@@ -929,10 +940,107 @@ def load_asset_specs( | |
dagster_fivetran_translator=dagster_fivetran_translator or DagsterFivetranTranslator(), | ||
) | ||
|
||
def sync_and_poll( | ||
self, context: Optional[Union[OpExecutionContext, AssetExecutionContext]] = None | ||
def _generate_materialization( | ||
self, | ||
fivetran_output: FivetranOutput, | ||
dagster_fivetran_translator: DagsterFivetranTranslator, | ||
): | ||
raise NotImplementedError() | ||
connector = FivetranConnector.from_connector_details( | ||
connector_details=fivetran_output.connector_details | ||
) | ||
schema_config = FivetranSchemaConfig.from_schema_config_details( | ||
schema_config_details=fivetran_output.schema_config | ||
) | ||
|
||
for schema_source_name, schema in schema_config.schemas.items(): | ||
if not schema.enabled: | ||
continue | ||
|
||
for table_source_name, table in schema.tables.items(): | ||
if not table.enabled: | ||
continue | ||
|
||
asset_key = dagster_fivetran_translator.get_asset_spec( | ||
props=FivetranConnectorTableProps( | ||
table=get_fivetran_connector_table_name( | ||
schema_name=schema.name_in_destination, | ||
table_name=table.name_in_destination, | ||
), | ||
connector_id=connector.id, | ||
name=connector.name, | ||
connector_url=connector.url, | ||
schema_config=schema_config, | ||
database=None, | ||
service=None, | ||
) | ||
).key | ||
|
||
yield AssetMaterialization( | ||
asset_key=asset_key, | ||
description=( | ||
f"Table generated via Fivetran sync: {schema.name_in_destination}.{table.name_in_destination}" | ||
), | ||
metadata={ | ||
**metadata_for_table( | ||
as_dict(table), | ||
get_fivetran_connector_url(fivetran_output.connector_details), | ||
include_column_info=True, | ||
database=None, | ||
schema=schema.name_in_destination, | ||
table=table.name_in_destination, | ||
), | ||
"schema_source_name": schema_source_name, | ||
"table_source_name": table_source_name, | ||
}, | ||
) | ||
|
||
def sync_and_poll( | ||
self, context: Union[OpExecutionContext, AssetExecutionContext] | ||
) -> Iterator[Union[AssetMaterialization, MaterializeResult]]: | ||
"""Executes a sync and poll process to materialize Fivetran assets. | ||
|
||
Args: | ||
context (Union[OpExecutionContext, AssetExecutionContext]): The execution context | ||
from within `@fivetran_assets`. If an AssetExecutionContext is passed, | ||
its underlying OpExecutionContext will be used. | ||
|
||
Returns: | ||
Iterator[Union[AssetMaterialization, MaterializeResult]]: An iterator of MaterializeResult | ||
or AssetMaterialization. | ||
""" | ||
assets_def = context.assets_def | ||
dagster_fivetran_translator = get_translator_from_fivetran_assets(assets_def) | ||
|
||
connector_id = next( | ||
check.not_none(FivetranMetadataSet.extract(spec.metadata).connector_id) | ||
for spec in assets_def.specs | ||
) | ||
|
||
client = self.get_client() | ||
fivetran_output = client.sync_and_poll( | ||
connector_id=connector_id, | ||
) | ||
|
||
materialized_asset_keys = set() | ||
for materialization in self._generate_materialization( | ||
fivetran_output=fivetran_output, dagster_fivetran_translator=dagster_fivetran_translator | ||
): | ||
# Scan through all tables actually created, if it was expected then emit a MaterializeResult. | ||
# Otherwise, emit a runtime AssetMaterialization. | ||
if materialization.asset_key in context.selected_asset_keys: | ||
yield MaterializeResult( | ||
asset_key=materialization.asset_key, metadata=materialization.metadata | ||
) | ||
materialized_asset_keys.add(materialization.asset_key) | ||
else: | ||
context.log.warning( | ||
f"An unexpected asset was materialized: {materialization.asset_key}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe note that we're still going to yield a materialization event. |
||
) | ||
yield materialization | ||
|
||
unmaterialized_asset_keys = context.selected_asset_keys - materialized_asset_keys | ||
if unmaterialized_asset_keys: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice |
||
context.log.warning(f"Assets were not materialized: {unmaterialized_asset_keys}") | ||
|
||
|
||
@experimental | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We reconstruct a
FivetranConnectorTableProps
to get the asset key usingget_asset_spec(...).key