Service | Port | Info |
---|---|---|
Kafka | 9092 | Default port |
Zookeeper | 2181 | Default port |
Conduktor Kafka
in local docker.Conducktor
in docker.
docker compose up -d --wait
auto.create.topics.enable=false
Note: Trobleshoot connectivity issues on Windows WSL 2 Connecting-to-kafka-running-on-windows-wsl-2/.
kafka-storage.sh random-uuid
kafka-storage.sh format -t Nl0GbGlqReSzOsMirclqoQ -c /path/to/kafka-2.13-3.9.0/config/kraft/server.properties
kafka-server-start.sh /path/to/kafka-2.13-3.9.0/config/kraft/server.properties
Ctrl + C
in terminal.Note: Replace
localhost
with[::1]
if using IPv6.
############################
##### LOCALHOST #####
############################
# ****Note: Replace `localhost` with `[::1]` if using IPv6
# List all topics in Kafka server
kafka-topics.sh --bootstrap-server localhost:9092 --list
kafka-topics.sh --bootstrap-server [::1]:9092 --list
# Describe all topics in Kafka server with configuration details
kafka-topics.sh --bootstrap-server localhost:19092 --describe
kafka-topics.sh --bootstrap-server [::1]:19092 --describe
# Describe specific topic in Kafka server with configuration details
kafka-topics.sh --bootstrap-server localhost:9092 --topic first_topic --describe
kafka-topics.sh --bootstrap-server [::1]:9092 --topic first_topic --describe
# Create topic named `first_topic`
kafka-topics.sh --bootstrap-server localhost:9092 --topic first_topic --create
kafka-topics.sh --bootstrap-server [::1]:9092 --topic first_topic --create
# Create a topic with name `second_topic` with 3 partitions
kafka-topics.sh --bootstrap-server localhost:9092 --topic second_topic --create --partitions 3
kafka-topics.sh --bootstrap-server [::1]:9092 --topic second_topic --create --partitions 3
# Create a topic with name `third_topic` with 3 partitions and 2 replicas
# Faile in local: Replication factor should not be greater than available brokers.
kafka-topics.sh --bootstrap-server localhost:9092 --topic third_topic --create --partitions 3 --replication-factor 2
kafka-topics.sh --bootstrap-server [::1]:9092 --topic third_topic --create --partitions 3 --replication-factor 2
# Create a topic (working)
kafka-topics.sh --bootstrap-server localhost:9092 --topic third_topic --create --partitions 3 --replication-factor 1
kafka-topics.sh --bootstrap-server [::1]:9092 --topic third_topic --create --partitions 3 --replication-factor 1
# Delete a topic
kafka-topics.sh --bootstrap-server localhost:9092 --topic first_topic --delete
kafka-topics.sh --bootstrap-server [::1]:9092 --topic first_topic --delete
# (only works if delete.topic.enable=true)
# list all active brokers
zookeeper-shell.sh localhost:2181 ls /brokers/ids
zookeeper-shell.sh [::1]:2181 ls /brokers/ids
About / Introduction
Steps
Note:
All Commands:
############################
##### LOCALHOST #####
############################
# Crete topic if doesn't exists
kafka-topics.sh --bootstrap-server localhost:9092 --topic first_topic --create --partitions 1
kafka-topics.sh --bootstrap-server [::1]:9092 --topic first_topic --create --partitions 1
# producing
# For IPv4 use - --bootstrap-server localhost:9092
kafka-console-producer.sh --bootstrap-server [::1]:9092 --topic first_topic
> Hello World
>My name is Conduktor
>I love Kafka
>^C (<- Ctrl + C is used to exit the producer)
# producing with properties acknowledge all
kafka-console-producer.sh --bootstrap-server [::1]:9092 --topic first_topic --producer-property acks=all
> some message that is acked
> just for fun
> fun learning!
# producing to a non existing topic throws warnning and the topic will be created internally.
# we must prevent topics from auto creation, check steps section for details.
kafka-console-producer.sh --bootstrap-server [::1]:9092 --topic new_topic
> hello world!
# our new topic only has 1 partition
kafka-topics.sh --bootstrap-server [::1]:9092 --list
kafka-topics.sh --bootstrap-server [::1]:9092 --topic new_topic --describe
# edit config/server.properties or config/kraft/server.properties
# num.partitions=3
# produce against a non existing topic again
kafka-console-producer.sh --bootstrap-server [::1]:9092 --topic new_topic_2
hello again!
# this time our topic has 3 partitions
kafka-topics.sh --bootstrap-server [::1]:9092 --list
kafka-topics.sh --bootstrap-server [::1]:9092 --topic new_topic_2 --describe
# produce messages with keys
kafka-console-producer.sh --bootstrap-server [::1]:9092 --topic first_topic --property parse.key=true --property key.separator=:
>key1:value1
>name:srvivek
References:
############################
##### LOCALHOST #####
############################
# create a topic with 3 partitions
kafka-topics.sh --bootstrap-server [::1]:9092 --topic first_topic --create --partitions 3
# consuming new messages
kafka-console-consumer.sh --bootstrap-server [::1]:9092 --topic first_topic
# producing other terminal
# RoundRobin to all partitions
kafka-console-producer.sh --bootstrap-server [::1]:9092 --producer-property partitioner.class=org.apache.kafka.clients.producer.RoundRobinPartitioner --topic first_topic
# consuming messages from beginning
kafka-console-consumer.sh --bootstrap-server [::1]:9092 --topic first_topic --from-beginning
# consume messages and display key, values and timestamp in consumer
kafka-console-consumer.sh --bootstrap-server [::1]:9092 --topic first_topic --formatter org.apache.kafka.tools.consumer.DefaultMessageFormatter --property print.timestamp=true --property print.key=true --property print.value=true --property print.partition=true --from-beginning
# create a topic with 3 partitions
# If doesn't exists or have 1 partition
kafka-topics.sh --bootstrap-server [::1]:9092 --topic third_topic --create --partitions 3
# start one consumer
# It will not read old messages
kafka-console-consumer.sh --bootstrap-server [::1]:9092 --topic third_topic --group my-first-application
# start one producer and start producing
kafka-console-producer.sh --bootstrap-server [::1]:9092 --producer-property partitioner.class=org.apache.kafka.clients.producer.RoundRobinPartitioner --topic third_topic
# start another consumer part of the same group. See messages being spread
kafka-console-consumer.sh --bootstrap-server [::1]:9092 --topic third_topic --group my-first-application
# start another consumer part of a different group from beginning
kafka-console-consumer.sh --bootstrap-server [::1]:9092 --topic third_topic --group my-second-application --from-beginning
GROUP TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
my-first-application third_topic 2 13 15 2 console-consumer-5b341142-1f1a-4838-84e0-790690e71804 /0:0:0:0:0:0:0:1 console-consumer
my-first-application third_topic 1 13 14 1 console-consumer-5b341142-1f1a-4838-84e0-790690e71804 /0:0:0:0:0:0:0:1 console-consumer
my-first-application third_topic 0 13 15 2 console-consumer-5b341142-1f1a-4838-84e0-790690e71804 /0:0:0:0:0:0:0:1 console-consumer
GROUP CONSUMER-ID HOST CLIENT-ID #PARTITIONS
my-first-application console-consumer-5b341142-1f1a-4838-84e0-790690e71804 /0:0:0:0:0:0:0:1 console-consumer 3
GROUP TOPIC PARTITION NEW-OFFSET
my-first-application third_topic 2 0
my-first-application third_topic 1 0
my-first-application third_topic 0 0
GROUP TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
my-first-application third_topic 2 0 16 16 - - -
my-first-application third_topic 1 0 15 15 - - -
my-first-application third_topic 0 0 15 15 - - -
# documentation for the command
kafka-consumer-groups.sh
# list consumer groups
kafka-consumer-groups.sh --bootstrap-server [::1]:9092 --list
# describe one specific group
kafka-consumer-groups.sh --bootstrap-server [::1]:9092 --describe --group my-second-application
# describe another group
kafka-consumer-groups.sh --bootstrap-server [::1]:9092 --describe --group my-first-application
# desriber members of the group
kafka-consumer-groups.sh --bootstrap-server [::1]:9092 --describe --group my-first-application --members
# start a consumer
kafka-console-consumer.sh --bootstrap-server [::1]:9092 --topic first_topic --group my-first-application
# describe the group now
kafka-consumer-groups.sh --bootstrap-server [::1]:9092 --describe --group my-first-application
# describe a console consumer group (change the end number)
kafka-consumer-groups.sh --bootstrap-server [::1]:9092 --describe --group console-consumer-10592
# start a console consumer
kafka-console-consumer.sh --bootstrap-server [::1]:9092 --topic first_topic --group my-first-application
# describe the group again
kafka-consumer-groups.sh --bootstrap-server [::1]:9092 --describe --group my-first-application
#############################
###### Reset offset ######
#############################
# Dry Run: reset the offsets to the beginning of each partition
kafka-consumer-groups.sh --bootstrap-server [::1]:9092 --group my-first-application --reset-offsets --to-earliest --topic third_topic --dry-run
# execute flag is needed
kafka-consumer-groups.sh --bootstrap-server [::1]:9092 --group my-first-application --reset-offsets --to-earliest --topic third_topic --execute
# describe the consumer group again
kafka-consumer-groups.sh --bootstrap-server [::1]:9092 --describe --group my-first-application
# consume from where the offsets have been reset
kafka-console-consumer.sh --bootstrap-server [::1]:9092 --topic third_topic --group my-first-application
# describe the group again
kafka-consumer-groups.sh --bootstrap-server [::1]:9092 --describe --group my-first-application