tutorial.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. """
  19. ### Tutorial Documentation
  20. Documentation that goes along with the Airflow tutorial located
  21. [here](https://airflow.apache.org/tutorial.html)
  22. """
  23. from __future__ import annotations
  24. # [START tutorial]
  25. # [START import_module]
  26. import textwrap
  27. from datetime import datetime, timedelta
  28. # The DAG object; we'll need this to instantiate a DAG
  29. from airflow.models.dag import DAG
  30. # Operators; we need this to operate!
  31. from airflow.operators.bash import BashOperator
  32. # [END import_module]
  33. # [START instantiate_dag]
  34. with DAG(
  35. "tutorial",
  36. # [START default_args]
  37. # These args will get passed on to each operator
  38. # You can override them on a per-task basis during operator initialization
  39. default_args={
  40. "depends_on_past": False,
  41. "email": ["airflow@example.com"],
  42. "email_on_failure": False,
  43. "email_on_retry": False,
  44. "retries": 1,
  45. "retry_delay": timedelta(minutes=5),
  46. # 'queue': 'bash_queue',
  47. # 'pool': 'backfill',
  48. # 'priority_weight': 10,
  49. # 'end_date': datetime(2016, 1, 1),
  50. # 'wait_for_downstream': False,
  51. # 'sla': timedelta(hours=2),
  52. # 'execution_timeout': timedelta(seconds=300),
  53. # 'on_failure_callback': some_function, # or list of functions
  54. # 'on_success_callback': some_other_function, # or list of functions
  55. # 'on_retry_callback': another_function, # or list of functions
  56. # 'sla_miss_callback': yet_another_function, # or list of functions
  57. # 'on_skipped_callback': another_function, #or list of functions
  58. # 'trigger_rule': 'all_success'
  59. },
  60. # [END default_args]
  61. description="A simple tutorial DAG",
  62. schedule=timedelta(days=1),
  63. start_date=datetime(2021, 1, 1),
  64. catchup=False,
  65. tags=["example"],
  66. ) as dag:
  67. # [END instantiate_dag]
  68. # t1, t2 and t3 are examples of tasks created by instantiating operators
  69. # [START basic_task]
  70. t1 = BashOperator(
  71. task_id="print_date",
  72. bash_command="date",
  73. )
  74. t2 = BashOperator(
  75. task_id="sleep",
  76. depends_on_past=False,
  77. bash_command="sleep 5",
  78. retries=3,
  79. )
  80. # [END basic_task]
  81. # [START documentation]
  82. t1.doc_md = textwrap.dedent(
  83. """\
  84. #### Task Documentation
  85. You can document your task using the attributes `doc_md` (markdown),
  86. `doc` (plain text), `doc_rst`, `doc_json`, `doc_yaml` which gets
  87. rendered in the UI's Task Instance Details page.
  88. ![img](https://imgs.xkcd.com/comics/fixing_problems.png)
  89. **Image Credit:** Randall Munroe, [XKCD](https://xkcd.com/license.html)
  90. """
  91. )
  92. dag.doc_md = __doc__ # providing that you have a docstring at the beginning of the DAG; OR
  93. dag.doc_md = """
  94. This is a documentation placed anywhere
  95. """ # otherwise, type it like this
  96. # [END documentation]
  97. # [START jinja_template]
  98. templated_command = textwrap.dedent(
  99. """
  100. {% for i in range(5) %}
  101. echo "{{ ds }}"
  102. echo "{{ macros.ds_add(ds, 7)}}"
  103. {% endfor %}
  104. """
  105. )
  106. t3 = BashOperator(
  107. task_id="templated",
  108. depends_on_past=False,
  109. bash_command=templated_command,
  110. )
  111. # [END jinja_template]
  112. t1 >> [t2, t3]
  113. # [END tutorial]