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

Add template fields to ProduceToTopicOperator #6

Merged
merged 1 commit into from
Aug 3, 2022

Conversation

sergeyh
Copy link
Contributor

@sergeyh sergeyh commented Jul 2, 2022

This change enables Jinja templating for ProduceToTopicOperator. It's useful when the producer function needs access to execution context, e.g. date interval, or when it depends on the upstream result, e.g. XCom variable. Otherwise, you have to write a custom PythonOperator and use the Hook/Producer directly.

Example 1 - dynamically route messages to a topic based on upstream status:

ProduceToTopicOperator(topic="export_result_{{ ti.xcom_pull(task_ids='export_status') }}")

Example 2 - access the context variables, e.g. DagRun or TaskInstance, from producer_function:

def produce_messages(**context):
    return load_from_db(start_date=context['start_date'], end_date=context['end_date'])
   
ProduceToTopicOperator(producer_function_kwargs={
    "start_date": "{{ data_interval_start }}",
    "end_date": "{{ data_interval_end }}"
})

Tested this manually. Unfortunately, I don't know how to write a unit test for this.

@dylanbstorey dylanbstorey merged commit 287683e into astronomer:main Aug 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants