-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample_dag.py
More file actions
77 lines (61 loc) · 2.13 KB
/
Copy pathsample_dag.py
File metadata and controls
77 lines (61 loc) · 2.13 KB
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
Here's the enhanced version of your Airflow DAG code, following best practices:
```python
#!/usr/bin/env python3
"""
Airflow DAG: Sample Workflow
Generated from Markdown: sample.md
Generated: 2026-03-11 00:40:17
This workflow consists of 9 tools and 9 connections, processing a dataset from `US_Accidents_March23.csv`. The workflow includes a variety of tool types, including input/output tools, filter/decision tools, processing tools, and browse/display tools.
"""
import os
from datetime import datetime, timedelta
from airflow import DAG
from airflow.operators.python import PythonOperator
# Define default arguments for the DAG
default_args = {
'owner': 'airflow',
'depends_on_past': False,
'email_on_failure': False,
'email_on_retry': False,
'retries': 1,
'retry_delay': timedelta(minutes=5),
}
def validate_data_file(file_path):
"""
Validate the file path for input data.
Args:
file_path (str): The file path to be validated.
Returns:
bool: True if the file is valid, False otherwise.
"""
if not file_path.endswith(('.csv', '.xlsx', '.xls')):
raise ValueError("Invalid file format. Only .csv, .xlsx, and .xls files are supported.")
return True
def get_file_type(file_path):
"""
Get the type of the file based on its extension.
Args:
file_path (str): The file path to determine the file type.
Returns:
str: The file type (e.g., csv, xlsx, xls).
"""
if file_path.endswith('.csv'):
return 'csv'
elif file_path.endswith(('.xlsx', '.xls')):
return 'excel'
def read_data_1(context):
"""
Read input data from a CSV or Excel file.
Args:
context (dict): The DAG context containing the task ID and XCom key.
Returns:
int: The number of rows in the input data.
"""
try:
ti = context['ti']
file_path = 'input.csv'
# Validate the file path
if not validate_data_file(file_path):
raise ValueError("Invalid file format. Only .csv, .xlsx, and .xls files are supported.")
# Read input data
file_type = get_file_type(file