-
Notifications
You must be signed in to change notification settings - Fork 0
/
airflow_submission_project.py
85 lines (68 loc) · 2.11 KB
/
airflow_submission_project.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
################## Exercise 1: Create imports, DAG argument, and Defintion
from datetime import datetime, timedelta
from airflow.models import DAG
from airflow.operators.bash_operator import BashOperator
# Task 1.1: Define DAG arguments
default_args = {
'owner': 'Ana',
'start_date': datetime(2024, 7, 17),
'retries': 1,
'retry_delay': timedelta(minutes=5),
}
# Task 1.2: Define the DAG
dag = DAG(
'final_project_DAG',
default_args=default_args,
description='final DAG project for coursework',
schedule = timedelta(seconds=5)
)
################## Exercise 2: Create the tasks using BashOperator
# Task 2.1: Create a task to unzip data.
task1 = BashOperator(
task_id='unzip_file',
bash_command='unzip ecommerce.zip',
dag=dag,
)
# Task 2.2: Create a task to extract data from csv file
task2 = BashOperator(
task_id='extract_csv',
bash_command='cat Ecommerce_data.csv',
dag=dag,
)
# Task 2.3: Create a task to extract data from tsv file
task3 = BashOperator(
task_id='extract_tsv',
bash_command='cat Ecommerce_data_tsv.txt',
dag=dag,
)
# Task 2.4: Create a task to extract data from fixed width file
task4 = BashOperator(
task_id='extract_txt',
bash_command='cat Ecommerce_data_fw.txt',
dag=dag,
)
# Task 2.5: Create a task to consolidate data extracted from previous tasks
task5 = BashOperator(
task_id='combine_data',
bash_command='cat Ecommerce_data.csv Ecommerce_data_tsv.txt Ecommerce_data_fw.txt >> all.txt',
dag=dag,
)
# Task 2.6: Transform the data
task6 = BashOperator(
task_id='transform_data',
bash_command='sort all.txt >> sorted.txt',
dag=dag,
)
# Task 2.7: Define the task pipeline
task1 >> task2 >> task3 >> task4 >> task5 >> task6
################## Exercise 3
# # Task 3.1: Submit the DAG
# export AIRFLOW_HOME=/home/project/airflow
# echo $AIRFLOW_HOME
# # Task3.2: Unpause and trigger the DAG
# export AIRFLOW_HOME=/home/project/airflow
# cp airflow_submission_project.py $AIRFLOW_HOME/dags
# # Task 3.3: List the DAG tasks
# airflow tasks list airflow_submission_project
# # Task 3.4: Monitor the DAG
# Done via UI