example_xcomargs.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on an
  14. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. # KIND, either express or implied. See the License for the
  16. # specific language governing permissions and limitations
  17. # under the License.
  18. """Example DAG demonstrating the usage of the XComArgs."""
  19. from __future__ import annotations
  20. import logging
  21. import pendulum
  22. from airflow.decorators import task
  23. from airflow.models.dag import DAG
  24. from airflow.operators.bash import BashOperator
  25. log = logging.getLogger(__name__)
  26. @task
  27. def generate_value():
  28. """Empty function"""
  29. return "Bring me a shrubbery!"
  30. @task
  31. def print_value(value, ts=None):
  32. """Empty function"""
  33. log.info("The knights of Ni say: %s (at %s)", value, ts)
  34. with DAG(
  35. dag_id="example_xcom_args",
  36. start_date=pendulum.datetime(2021, 1, 1, tz="UTC"),
  37. catchup=False,
  38. schedule=None,
  39. tags=["example"],
  40. ) as dag:
  41. print_value(generate_value())
  42. with DAG(
  43. "example_xcom_args_with_operators",
  44. start_date=pendulum.datetime(2021, 1, 1, tz="UTC"),
  45. catchup=False,
  46. schedule=None,
  47. tags=["example"],
  48. ) as dag2:
  49. bash_op1 = BashOperator(task_id="c", bash_command="echo c")
  50. bash_op2 = BashOperator(task_id="d", bash_command="echo c")
  51. xcom_args_a = print_value("first!")
  52. xcom_args_b = print_value("second!")
  53. bash_op1 >> xcom_args_a >> xcom_args_b >> bash_op2