Kafka Flashcards
CLI create a topic in Kafka
kafka-topics.sh –zookeeper 127.0.0.1:2181 –topic my_topic –create –partitions 3 –replication-factor 2
1) must reference zookeeper
2) must define partitions & replication factor
What limits your replication factor?
Broker number. You cannot have a replication factor higher than the number of brokers you have set.
CLI list Kafka topics
kafka-topics.sh –zookeeper 127.0.0.1:2181 –list
CLI get info about a topic
kafka-topics.sh –zookeeper 127.0.0.1:2181 –topic my_topic –describe
CLI delete a topic
kafka-topics.sh –zookeeper 127.0.0.1:2181 –topic my_topic –delete
DON’T DO THIS ON WINDOWS
CLI create a producer
kafka-console-producer.sh –broker-list 127.0.0.1:9092 –topic my_topic (–producer-property acks=all)
script, specify broker and where to find them, topic (optional properties)
CLI get out of producer
ctrl + C
what happens when you produce to a topic that doesn’t exist?
Kafka creates the topic, sends the message when a leader becomes available.
Topic is created with default values (partitions 1, replication factor 1)
change default values for topics
edit the server.properties file (in text editor):
num,partitions=1 (change to custom setting)
CLI create consumer
kafka-console-consumer.sh –bootstrap-server 127.0.0.1:9092 –topic my_topic
(begins getting messages produced after this is run. For all, add “–from-beginning”)
CLI set consumer group
Every consumer is part of a group; Id is the name of our application.
kafka-console-consumer.sh –bootstrap-server 127.0.0.1:9092 –topic my_topic –group my-application
Why consumer groups?
Every consumer is part of a group; Id is the name of our application.
Members of a group share the load, do load balancing. You can have as many consumers in a group as you have partitions. If one consumer goes down, the others will automatically rebalance the load.
CLI consumer groups: 1) list groups, 2) get info about a group
1) kafka-consumer-groups.sh –bootstrap-server 127.0.0.1:9092 –list
2) –describe –group my-application
CLI reset offset in a consumer group
Tell kafka where the group should start reading. (see options with kafka-consumer-groups.sh) (Shift forward or backwards with num or -num.)
- specify group, where to rest offsets, topic
- execute
kafka-consumer-groups.sh –bootstrap-server 127.0.0.1:9092 –group my-application –reset-offsets –to-earliest –execute –topic my_topic
What dependencies does Kafka need in Java POM?
1) Kafka-clients (not just kafka)
2) slf4j-simple (for logging) and remove scope