|
| 1 | +# Copyright 2018 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""An example DAG demonstrating simple Apache Airflow operators.""" |
| 16 | + |
| 17 | +# [START composer_simple] |
| 18 | +from __future__ import print_function |
| 19 | + |
| 20 | +import datetime |
| 21 | + |
| 22 | +from airflow import models |
| 23 | +from airflow.operators import bash_operator |
| 24 | +from airflow.operators import python_operator |
| 25 | + |
| 26 | + |
| 27 | +default_dag_args = { |
| 28 | + # The start_date describes when a DAG is valid / can be run. Set this to a |
| 29 | + # fixed point in time rather than dynamically, since it is evaluated every |
| 30 | + # time a DAG is parsed. See: |
| 31 | + # https://airflow.apache.org/faq.html#what-s-the-deal-with-start-date |
| 32 | + 'start_date': datetime.datetime(2018, 1, 1), |
| 33 | +} |
| 34 | + |
| 35 | +# Define a DAG (directed acyclic graph) of tasks. |
| 36 | +# Any task you create within the context manager is automatically added to the |
| 37 | +# DAG object. |
| 38 | +with models.DAG( |
| 39 | + 'simple_greeting', |
| 40 | + default_args=default_dag_args) as dag: |
| 41 | + def greeting(): |
| 42 | + print('Hello World!') |
| 43 | + |
| 44 | + # An instance of an operator is called a task. In this case, the |
| 45 | + # hello_python task calls the "greeting" Python function. |
| 46 | + hello_python = python_operator.PythonOperator( |
| 47 | + task_id='hello', |
| 48 | + python_callable=greeting) |
| 49 | + |
| 50 | + # Likewise, the goodbye_bash task calls a Bash script. |
| 51 | + goodbye_bash = bash_operator.BashOperator( |
| 52 | + task_id='bye', |
| 53 | + bash_command='echo Goodbye.') |
| 54 | + |
| 55 | + # Define the order in which the tasks complete by using the >> and << |
| 56 | + # operators. In this example, hello_python executes before goodbye_bash. |
| 57 | + hello_python >> goodbye_bash |
| 58 | +# [END composer_simple] |
0 commit comments