guh.me - gustavo's personal blog

Apache Kafka for Beginners

My notes from the Udemy course: Apache Kafka for Beginners.

Kafka Theory

Kafka Command Line Interface

Kafka Topics

kafka-topics can be used to manage topics. You must specify Zookeeper when running any operation.

Create a topic

kafka-topics \
    --zookeper 127.0.0.1:2181 \
    --topic my-kafka-topic \
    --partitions 3 \
    --replication-factor 1 \
    --create

List topics

kafka-topics \
    --zookeper 127.0.0.1:2181 \
    --list

Describe a topic

kafka-topics \
    --zookeper 127.0.0.1:2181 \
    --topic my-kafka-topic \
    --describe

Delete a topic

kafka-topics \
    --zookeper 127.0.0.1:2181 \
    --topic my-kafka-topic \
    --delete

Kafka Console Producer

kafka-console-producer reads data from standard input and publishes it to Kafka.

Produce a message

kafka-console-producer \
    --broker-list 127.0.0.1:9092 \
    --topic my-kafka-topic

‘CTRL + C` will interrupt the input.

Produce a message with properties

kafka-console-producer \
    --broker-list 127.0.0.1:9092 \
    --topic my-kafka-topic
    --producer-property acks=all

Kafka Console Consumer

kafka-console-consumer reads data from Kafka and outputs it to standard output.

Consume a topic

kafka-console-consumer \
    --bootstrap-server 127.0.0.1:9092 \
    --topic my-kafka-topic

Consume a topic from beginning

kafka-console-consumer \
    --bootstrap-server 127.0.0.1:9092 \
    --topic my-kafka-topic \
    --from-beginning

Consume a topic with group

kafka-console-consumer \
    --bootstrap-server 127.0.0.1:9092 \
    --topic my-kafka-topic \
    --group my-application

Kafka Consumer Groups

kafka-consumer-groups lists, describes, deletes and resets offsets for consumer groups.

List consumer groups

kafka-consumer-groups \
    --bootstrap-server 127.0.0.1:9092 \
    --list

Describe a consumer group

kafka-consumer-groups \
    --bootstrap-server 127.0.0.1:9092 \
    --group my-application \
    --describe

Reset the offset for a group

kafka-consumer-groups \
    --bootstrap-server 127.0.0.1:9092 \
    --group my-application \
    --reset-offsets \
    --to-earliest \
    --topic my-kafka-topic \
    --execute

Advanced Configuration

Idempotent Producers

Message Compression

Kafka Ecosystem