-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathI Intro to Airflow.py
179 lines (150 loc) · 3.99 KB
/
I Intro to Airflow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"""
complete introduction to the components of Apache Airflow and learn how and why you should use them.
\ workflow /
> set of steps to accomplish a d.e task
..such as -downloading files -copying data -filtering info -writing to a database
> of varying leves of complexity
\ airflow /
> platform to program workflows
- creating
- scheduling
- monitoring
> can implement any language, but workflows written : in Python
> implement workflows as DAGs : Direct Acyclic Graphs
> access via: code, command-line, web interface
| DAG simple definition eg: |
etl_dag = DAG(
dag_id = 'etl_pipeline',
default_args = {"start_data":"2020-01-08"}
)
------
| Run a workflow in Airflow eg: |
airflow run <dag_ig> <task_id> <start_date>
airflow run example_etl download-file 2020-01-10
-------
"""
#|
#|
### Running a task in Airflow
"""-Which command would you enter in the console to run the desired task?"""
airflow run etl_pipeline download_file 2020-01-08
#|
#|
### Examining Airflow commands
"""-Which of the following is NOT an Airflow sub-command?
| get airflow documentation ariflow description |
airflow -h
-list_dags
-edit_dag
-test
-scheduler
"""
# Answ: edit_dag
#|
#|
"""
\ What's a DAG /
> Direct : flow representing dependencies
> Acyclic : do not loop
> Graph : actual set of components
# written in python, components in any language
# made up of components : tasks to be executed (operators, sensors)
# dependencies defined (explicit or implicit)
| Define a DAG eg:|
from airflow.models import DAG
from datetime import datetime
default_arguments = {
'owner' : 'jdoe',
'emial' : 'jdoe@datacamp.com',
'start_date' : datetime(2020, 1, 20)
}
etl_dag = DAG( 'etl_workflow', default_args=default_arguments )
---------
"""
#|
#|
### Defining a simple DAG
"""1/3"""
# Import the DAG object
from airflow.models import DAG
#|
"""2/3"""
# Import the DAG object
from airflow.models import DAG
# Define the default_args dictionary
default_args = {
'owner': 'dsmith',
'start_date': datetime(2020, 1, 14),
'retries' : 2
}
#|
"""3/3"""
# Import the DAG object
from airflow.models import DAG
# Define the default_args dictionary
default_args = {
'owner': 'dsmith',
'start_date': datetime(2020, 1, 14),
'retries': 2
}
# Instantiate the DAG object
etl_dag = DAG( 'example_etl', default_args= default_args)
#|
#|
### Working with DAGs and the Airflow shell
"""-How many DAGs are present in the Airflow system from the command-line?
| list DAGs inshell |
airflow list_dags
"""
airflow list_dags
# Answ: 2
#|
#|
### Troubleshooting DAG creation
"""Instructions
Use the airflow shell command to determine which DAG is not being recognized correctly.
After you determine the broken DAG, open the file and fix any Python errors.
Once modified, verify that the DAG now appears within Airflow's output."""
airflow list_dags
----------
from airflow.models import DAG # fixed part
default_args = {
'owner': 'jdoe',
'email': 'jdoe@datacamp.com'
}
dag = DAG( 'refresh_data', default_args=default_args )
#|
#|
"""
\ Airflow web interface /
.
"""
#|
#|
### Airflow web interface
"""Which airflow command would you use to start the webserver on port 9090?"""
airflow webserver -h # see documentation
-------
airflow webserver -p 9090
#|
#|
### Navigating the Airflow UI
"""Which of the following events have NOT run on your Airflow instance?
-cli_scheduler
-cli_webserver
-cli_worker"""
# -browse -logs
# Answ: cli_worker
#|
#|
### Examining DAGs with the Airflow UI
"""she would like to know which operator is NOT in use with the DAG called update_state, as your team is trying to verify the
components used in production workflows.
-Remember to select the operator NOT used in this DAG.
BashOperator
PythonOperator
JdbcOperator
SimpleHttpOperator"""
# -go inside update_state name
# -graph view
# ANSW: JdbcOperator