✅ 1. Operators (Classic style)
Tasks are created explicitly using Operator classes like BashOperator, PythonOperator, ShortCircuitOperator, etc.
Example
Characteristics
| Aspect | Operators |
|---|---|
| Syntax style | Imperative ("define tasks, then set dependencies") |
| Task type | Anything with a specific operator (Bash, SQL, Kubernetes, EMR, etc.) |
| XCom | Must be managed manually (xcom_pull, return values explicitly) |
| UI display | Shows operator class name (e.g., BashOperator) |
| Dependencies | Set manually (task1 >> task2) |
| Pros | Very explicit; works with all operator types; stable; more control |
| Cons | Verbose; Python functions need PythonOperator wrappers |
✅ 2. Decorators (TaskFlow API)
Introduced in Airflow 2.0 to simplify DAG creation using modern, Pythonic syntax.
Example
Characteristics
| Aspect | Decorators (TaskFlow) |
|---|---|
| Syntax style | Functional and declarative |
| Task type | Python tasks only (using your own Python code) |
| XCom | Automatic (function return value → XCom) |
| UI display | Shows task as @task Python call |
| Dependencies | Inferred automatically via function calls |
| Pros | Very clean; auto XCom; fewer imports; easier testing |
| Cons | Only for Python tasks; cannot replace BashOperator, SparkSubmitOperator, etc. |
🔍 Key Behavioral Differences
1. Creation Model
Operators:
You instantiate a task:
Decorators:
You call a function and it becomes a task:
2. Dependencies
Operators:
Set explicitly:
Decorators:
Dependencies come from function calls:
Airflow infers:
3. XCom Handling
Operators:
Manual:
Decorators:
Automatic:
4. Types of Work You Can Do
Operators:
Use when you need specialized behaviors:
-
Bash
-
SQL
-
Docker
-
Kubernetes
-
Spark
-
EMR
-
Sensors
-
Transfer operators (S3 → GCS)
Decorators:
Only for Python code.
You still need operators for non-Python tasks.
5. UI Appearance
Operators:
Tooltip shows operator class:
Decorators:
Tooltip shows:
🎯 When to Use Each
Use Operators when:
-
You need a specialized operator (Bash, Spark, Docker, etc.)
-
You want explicit, more traditional Airflow construction
-
You are migrating older DAGs
-
You need fine-grained task control
Use TaskFlow decorators when:
-
Your logic is pure Python
-
You want automatic XCom passing
-
You prefer cleaner, modern DAG definitions
-
You're building fully Python-based workflows
🧩 Combined Use (Most Common)
Most modern DAGs use both together:

浙公网安备 33010602011771号