hussein-awala commented on PR #29762:
URL: https://github.com/apache/airflow/pull/29762#issuecomment-1568576556
> I wonder if it’d be worthwhile to make the example not use
PythonOperator—it’s not that valuable to know about using this particular
operator since you really should use taskflow instead.
@uranusjr I'd say that's a good idea, I can use this one:
```python
"""Example DAG demonstrating the usage of dynamic task mapping with
non-TaskFlow operators."""
from __future__ import annotations
from datetime import datetime
from airflow import DAG
from airflow.models import BaseOperator
class AddOneOperator(BaseOperator):
"""A custom operator that adds one to the input."""
def __init__(self, input, **kwargs):
super().__init__(**kwargs)
self.input = input
def execute(self, context):
return self.input + 1
class SumItOperator(BaseOperator):
"""A custom operator that sums the input."""
def __init__(self, values, **kwargs):
super().__init__(**kwargs)
self.values = values
def execute(self, context):
total = sum(self.values)
print(f"Total was {total}")
return total
with DAG(
dag_id="example_dynamic_task_mapping_with_no_taskflow_operators",
start_date=datetime(2022, 3, 4),
catchup=False,
):
# map the task to a list of values
add_one_task =
AddOneOperator.partial(task_id="add_one").expand(input=[1, 2, 3])
# aggregate (reduce) the mapped tasks results
sum_it_task = SumItOperator(task_id="sum_it", values=add_one_task.output)
```
WDYT?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]