Skip to content
BENIBIX Learn

Airflow Hook

Data🗓️ OrchestrationApache Airflow

Also known as Hook (Airflow), PostgresHook, S3Hook

Code that logs in to one system for you, like a database. Tasks use it.

Think of it like

It is like a phone that already knows how to dial. You say who to call, and it does the rest.

Example

Lucy needs a query that no ready-made task offers. In her task she makes a PostgresHook with the shop_db connection. She runs the query in two lines.

Why it matters

It saves you from writing login and retry code for every system. The hook already knows how that system works.

Definition

An Airflow hook is ready-made code for talking to one outside system. There is one for PostgreSQL, one for Amazon S3 and many more. You give it a connection name, and it logs in for you. Then you call its methods to run a query or move a file. Many operators are built on top of a hook. You use a hook directly when no operator does quite what you need.

Example code

python
from airflow.providers.postgres.hooks.postgres import PostgresHook

hook = PostgresHook(postgres_conn_id="shop_db")
rows = hook.get_records("SELECT COUNT(*) FROM orders")
The hook logs in with the shop_db connection and runs one query.
The hook sits in the middle. It takes the login from the connection and talks to the database.

Common mistake

Making a hook at the top of the DAG file. That code runs each time the file is read, not just when the task runs.

Key takeaways

  1. 1
    One system each. A hook per database or service.
  2. 2
    Uses a connection. Logs in by conn_id.
  3. 3
    Under operators. Operators are built from hooks.

Compare