The FileSensor operator in Apache Airflow is used to monitor the existence of a specific file in a directory. It pauses the execution of a task until the specified file is detected. This is especially useful for workflows that depend on data files being available before processing begins.
Example: Using FileSensor to Wait for a File
Suppose we have a DAG that processes data once a CSV file is available in a specific directory. We’ll use FileSensor to check if the file exists before running any data processing.
Example DAG with FileSensor
Explanation of the DAG
FileSensor:
- The
wait_for_filetask usesFileSensorto wait until the file specified byfilepath(data.csv) exists. fs_conn_id="fs_default"is the connection ID for the file system, set in Airflow's Connections UI. This can be configured to point to different file systems or directories as needed.poke_interval=60makes the sensor check every 60 seconds to see if the file exists.timeout=60 * 60 * 2(2 hours) specifies that the sensor should stop waiting after 2 hours if the file has not appeared.mode="poke"keeps the task slot occupied while checking, thoughmode="reschedule"is an alternative that frees the slot between checks.
- The
Data Processing Task:
- The
data_processingtask runs only once theFileSensordetects the file, ensuring that the data processing begins only after the file is available.
- The
Dependencies:
- The
FileSensortask must succeed beforedata_processingruns, ensuring a file dependency in the workflow.
- The
Benefits
- Dependency Management: Ensures that tasks depending on file availability only run when the file exists.
- Flexible Monitoring: Can be configured to check at different intervals and set timeouts for missing files.
- Efficient Scheduling: With
mode="reschedule", the sensor can save resources by not blocking worker slots during the waiting period.

GREAT
ReplyDeleteThe **FileSensor operator in Apache Airflow** is used to check whether a particular file is available before allowing the next task in a workflow to run. It is useful when a pipeline depends on an external file, such as a CSV or log file, that may arrive at an unpredictable time. The FileSensor periodically checks the specified file location and waits until the file is found.Airflow Course. Once the file becomes available, the workflow continues with the next task, such as reading, cleaning, or processing the data. This helps prevent pipelines from starting data-processing tasks before the required input file is ready.
ReplyDelete