Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Apache Kafka Cheat Sheet

Kafka Cheat Sheet

Display Topic Information

$ --describe --zookeeper localhost:2181 --topic beacon
Topic:beacon	PartitionCount:6	ReplicationFactor:1	Configs:
	Topic: beacon	Partition: 0	Leader: 1	Replicas: 1	Isr: 1
	Topic: beacon	Partition: 1	Leader: 1	Replicas: 1	Isr: 1

Add Partitions to a Topic

$ --alter --zookeeper localhost:2181 --topic beacon --partitions 3
WARNING: If partitions are increased for a topic that has a key, the partition logic or ordering of the messages will be affected
Adding partitions succeeded!

Change topic retention i.e set SLA

bin/ --zookeeper localhost:2181 --alter --topic mytopic --config*

This set retention of 8-hours on messages coming to topic mytopic. After 8 hours message will be deleted.

Delete Topic

$ kafka.admin.DeleteTopicCommand --zookeeper localhost:2181 --topic test --create --zookeeper localhost:2181 --replication-factor 1 --partitions 3 --topic file_acquire_complete --create --zookeeper localhost:2181 --replication-factor 1 --partitions 3 --topic job_result --create --zookeeper localhost:2181 --replication-factor 1 --partitions 3 --topic trigger_match --create --zookeeper localhost:2181 --replication-factor 1 --partitions 3 --topic event_result
$ --create --zookeeper localhost:2181 --replication-factor 1 --partitions 3 --topic job_result
Created topic "job_result".
$ --list --zookeeper localhost:2181
$ --broker-list localhost:9092 --topic test
$ --zookeeper localhost:2181 --topic test --from-beginning

List existing topics

bin/ --zookeeper localhost:2181 --list

Purge a topic

bin/ --zookeeper localhost:2181 --alter --topic mytopic --config

... wait a minute ...

bin/ --zookeeper localhost:2181 --alter --topic mytopic --delete-config

Delete a topic

bin/ --zookeeper localhost:2181 --delete --topic mytopic

Get the earliest offset still in a topic

bin/ --broker-list localhost:9092 --topic mytopic --time -2

Get the latest offset still in a topic

bin/ --broker-list localhost:9092 --topic mytopic --time -1

Consume messages with the console consumer

bin/ --new-consumer --bootstrap-server localhost:9092 --topic mytopic --from-beginning

Get the consumer offsets for a topic

bin/ --zookeeper=localhost:2181 --topic=mytopic --group=my_consumer_group

Read from __consumer_offsets

Add the following property to config/ exclude.internal.topics=false

bin/ --consumer.config config/ --from-beginning --topic __consumer_offsets --zookeeper localhost:2181 --formatter "kafka.coordinator.GroupMetadataManager\$OffsetsMessageFormatter"

Kafka Consumer Groups

List the consumer groups known to Kafka

bin/ --zookeeper localhost:2181 --list (old api)

bin/ --new-consumer --bootstrap-server localhost:9092 --list (new api)

View the details of a consumer group

bin/ --zookeeper localhost:2181 --describe --group <group name>


Getting the last five message of a topic

kafkacat -C -b localhost:9092 -t mytopic -p 0 -o -5 -e


Starting the Zookeeper Shell

bin/ localhost:2181

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment