home
diamond Go Premium
Data Engineering Path  ·  Airflow
Apache Airflow Logo

Kafka Integration

Batch Orchestration Meeting a Streaming World

Airflow is not a streaming platform - that's been said plainly since the first module of this course. Kafka usually enters an Airflow pipeline at the edges: producing a message when a batch job finishes, or consuming a bounded batch of messages as one discrete task, never as a continuously-running stream inside a task itself.

A Note on This Page's Screenshots
Same honest treatment as EMR/Redshift/Glue/KubernetesPodOperator: this would need a real Kafka broker running alongside this sandbox, which isn't set up here. Code-only, no live run.

Producing: Publishing an Event

from airflow.providers.apache.kafka.operators.produce import ProduceToTopicOperator

def get_order_completed_event(**context):
    return [("order_id", str(context["ds"]).encode("utf-8"))]

publish_completion_event = ProduceToTopicOperator(
    task_id="publish_order_completed",
    kafka_config_id="kafka_default",
    topic="orders.completed",
    producer_function=get_order_completed_event,
)

This is the same idea as the SnsPublishOperator covered in the AWS operators section — "pipeline finished, tell the rest of the architecture" — just for teams standardized on Kafka instead of SNS.

Consuming: Waiting For and Reading Messages

from airflow.providers.apache.kafka.sensors.kafka import AwaitMessageSensor

def process_message(message):
    key = message.key().decode("utf-8") if message.key() else None
    return key

wait_for_upstream_event = AwaitMessageSensor(
    task_id="wait_for_upstream_completed_event",
    kafka_config_id="kafka_default",
    topics=["upstream.completed"],
    apply_function="path.to.process_message",
)

AwaitMessageSensor is deferrable by default — same mechanism covered in the Architecture module, so a worker slot isn't held open the entire time it's waiting for a message to arrive.


The Connection

airflow connections add kafka_default \
  --conn-type kafka \
  --conn-extra '{"bootstrap.servers": "kafka-broker:9092", "group.id": "airflow-consumers"}'

Everything about how to reach the broker lives in this Connection, same pattern as every other external system covered in this course.

Tip — the usual shape of this integration
In practice, Kafka rarely appears inside the middle of a batch DAG. The far more common pattern: a standalone streaming consumer (running continuously, outside Airflow entirely - a Flink job, a small dedicated service) writes completed batches to S3/a warehouse, and that is what an Airflow DAG picks up next, often via a Dataset (covered in the previous module) rather than talking to Kafka directly at all.
lock

This content is reserved for Premium Members.

Upgrade to Premium

Entity Details

Create New Item

help

Submit Technical Query

Have a question or run into an issue? Describe it below, upload an optional screenshot, and our engineering team will answer it!

image Attach image (optional)

Submit Feedback

build Free Developer Utility Free Tool
gavel

Privacy & Legal Disclaimer

1. Client-Side Browser Processing

All utility tools on DeepEngineerHub (including Image to PDF, Text Formatters, JSON Converters, and Encryptors) execute 100% locally within your client browser using WebAssembly and JavaScript. No uploaded images, text, or documents are transmitted, collected, or stored on remote servers.

2. Limitation of Liability ("As-Is" Provision)

Tools and services are provided free of charge for convenience and educational purposes "as-is" without warranties of any kind. DeepEngineerHub shall not be held liable for any data loss, formatting inconsistencies, or indirect damages resulting from tool usage.

3. Open Source & Third-Party Software

Certain utilities utilize open-source client libraries (such as jsPDF, Mermaid.js, Pyodide) licensed under MIT, Apache, or BSD open licenses. All intellectual property remains with their respective copyright holders.