-
Notifications
You must be signed in to change notification settings - Fork 3k
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
fix(airflow): added support for jinja template for datahub emitter operator #11300
Merged
hsheth2
merged 16 commits into
datahub-project:master
from
dushayntAW:fix/ING-694/add-airflow-jinja-templating-for-datahubemitteroperator
Sep 17, 2024
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
6824778
fix(ingestion/airflow-plugin): fixed the issue with jinja template
dushayntAW abdd55b
fix(ingestion/airflow-plugin): fixed the issue with jinja template
dushayntAW 378031d
fix(ingestion/airflow-plugin): fixed linter issue
dushayntAW 7b2b81f
fix(ingestion/airflow-plugin): fixed linter issue
dushayntAW c46b0c4
fix(ingestion/airflow-plugin): fixed falining test
dushayntAW ccf59a2
fix(ingestion/airflow-plugin): fixed falining test
dushayntAW 12d7edf
fix(ingestion/airflow-plugin): fixed falining test
dushayntAW 2402a94
fix(ingestion/airflow-plugin): fixed falining test
dushayntAW 154dc22
fix(ingestion/airflow-plugin): incorporated the review comments
dushayntAW d5a008d
fix(ingestion/airflow-plugin): fixed linter issues
dushayntAW ee62226
fix(ingestion/airflow-plugin): fixed linter issues
dushayntAW c35bb33
fix(ingestion/airflow-plugin): fixed review comments, support for Met…
dushayntAW 1698077
fix(ingestion/airflow-plugin): fixed linter issue
dushayntAW 32748cb
fix(ingestion/airflow-plugin): updated testcase
dushayntAW 099fce6
fix(ingestion/airflow-plugin): updated testcase
dushayntAW 4e4552a
fix(ingestion/airflow-plugin): updated testcase
dushayntAW 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
|
||
from airflow.models import BaseOperator | ||
from airflow.utils.decorators import apply_defaults | ||
from avrogen.dict_wrapper import DictWrapper | ||
from datahub.emitter.mcp import MetadataChangeProposalWrapper | ||
from datahub.metadata.com.linkedin.pegasus2avro.mxe import MetadataChangeEvent | ||
|
||
from datahub_airflow_plugin.hooks.datahub import ( | ||
|
@@ -45,11 +47,13 @@ class DatahubEmitterOperator(DatahubBaseOperator): | |
:type datahub_conn_id: str | ||
""" | ||
|
||
template_fields = ["metadata"] | ||
|
||
# See above for why these mypy type issues are ignored here. | ||
@apply_defaults # type: ignore[misc] | ||
def __init__( # type: ignore[no-untyped-def] | ||
self, | ||
mces: List[MetadataChangeEvent], | ||
mces: List[Union[MetadataChangeEvent, MetadataChangeProposalWrapper]], | ||
datahub_conn_id: str, | ||
**kwargs, | ||
): | ||
|
@@ -59,5 +63,50 @@ def __init__( # type: ignore[no-untyped-def] | |
) | ||
self.metadata = mces | ||
|
||
def _render_template_fields(self, field_value, context, jinja_env): | ||
if isinstance(field_value, DictWrapper): | ||
for key, value in field_value.items(): | ||
setattr( | ||
field_value, | ||
key, | ||
self._render_template_fields(value, context, jinja_env), | ||
) | ||
elif isinstance(field_value, list): | ||
for item in field_value: | ||
self._render_template_fields(item, context, jinja_env) | ||
elif isinstance(field_value, str): | ||
return super().render_template(field_value, context, jinja_env) | ||
else: | ||
return super().render_template(field_value, context, jinja_env) | ||
return field_value | ||
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. why aren't we calling |
||
|
||
def execute(self, context): | ||
hsheth2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if context: | ||
jinja_env = self.get_template_env() | ||
|
||
""" | ||
The `_render_template_fields` method is called in the `execute` method to ensure that all template fields | ||
are rendered with the current execution context, which includes runtime variables and other dynamic data, | ||
is only available during the execution of the task. | ||
|
||
The `render_template` method is not overridden because the `_render_template_fields` method is used to | ||
handle the rendering of template fields recursively. | ||
This approach allows for more granular control over how each field is rendered, | ||
especially when dealing with complex data structures like `DictWrapper` and lists. | ||
|
||
By not overriding `render_template`, the code leverages the existing functionality | ||
provided by the base class while adding custom logic for specific cases. | ||
""" | ||
for item in self.metadata: | ||
if isinstance(item, MetadataChangeProposalWrapper): | ||
for key in item.__dict__.keys(): | ||
value = getattr(item, key) | ||
setattr( | ||
item, | ||
key, | ||
self._render_template_fields(value, context, jinja_env), | ||
) | ||
if isinstance(item, MetadataChangeEvent): | ||
self._render_template_fields(item, context, jinja_env) | ||
dushayntAW marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
self.generic_hook.get_underlying_hook().emit(self.metadata) |
71 changes: 71 additions & 0 deletions
71
...ules/airflow-plugin/tests/integration/dags/datahub_emitter_operator_jinja_template_dag.py
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from datetime import datetime, timedelta | ||
|
||
from airflow import DAG | ||
from datahub.emitter.mcp import MetadataChangeProposalWrapper | ||
from datahub.metadata.com.linkedin.pegasus2avro.mxe import MetadataChangeEvent | ||
from datahub.metadata.schema_classes import ( | ||
BrowsePathEntryClass, | ||
BrowsePathsV2Class, | ||
DatasetPropertiesClass, | ||
DatasetSnapshotClass, | ||
) | ||
|
||
from datahub_airflow_plugin.operators.datahub import DatahubEmitterOperator | ||
|
||
default_args = { | ||
"owner": "airflow", | ||
"depends_on_past": False, | ||
"start_date": datetime(2023, 1, 1), | ||
"email": ["[email protected]"], | ||
"email_on_failure": False, | ||
"execution_timeout": timedelta(minutes=5), | ||
} | ||
|
||
|
||
with DAG( | ||
"datahub_emitter_operator_jinja_template_dag", | ||
default_args=default_args, | ||
description="An example dag with jinja template", | ||
schedule_interval=None, | ||
tags=["example_tag"], | ||
catchup=False, | ||
default_view="tree", | ||
): | ||
add_custom_properties = DatahubEmitterOperator( | ||
task_id="datahub_emitter_operator_jinja_template_dag_task", | ||
mces=[ | ||
MetadataChangeProposalWrapper( | ||
entityUrn="urn:li:dataset:(urn:li:dataPlatform:hive,datahub.example.mcpw_example,DEV)", | ||
aspect=BrowsePathsV2Class( | ||
path=[BrowsePathEntryClass("mcpw_example {{ ds }}")], | ||
), | ||
), | ||
MetadataChangeProposalWrapper( | ||
entityUrn="urn:li:dataset:(urn:li:dataPlatform:hive,datahub.example.mcpw_example_{{ ts_nodash }},DEV)", | ||
aspect=BrowsePathsV2Class( | ||
path=[BrowsePathEntryClass("mcpw_example {{ ds }}")], | ||
), | ||
), | ||
MetadataChangeEvent( | ||
proposedSnapshot=DatasetSnapshotClass( | ||
urn="urn:li:dataset:(urn:li:dataPlatform:hive,datahub.example.lineage_example,DEV)", | ||
aspects=[ | ||
DatasetPropertiesClass( | ||
customProperties={"jinjaTemplate": "{{ ds }}"} | ||
) | ||
], | ||
), | ||
), | ||
MetadataChangeEvent( | ||
proposedSnapshot=DatasetSnapshotClass( | ||
urn="urn:li:dataset:(urn:li:dataPlatform:hive,datahub.example.lineage_example_{{ ts_nodash }},DEV)", | ||
dushayntAW marked this conversation as resolved.
Show resolved
Hide resolved
|
||
aspects=[ | ||
DatasetPropertiesClass( | ||
customProperties={"jinjaTemplate": "{{ ds }}"} | ||
) | ||
], | ||
), | ||
), | ||
], | ||
datahub_conn_id="datahub_file_default", | ||
) |
Oops, something went wrong.
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.