February 3rd, 2024
Kafka on docker-compose

Kafka setup, create topic, send message and receive messages

https://www.conduktor.io/kafka/kafka-topics-cli-tutorial/

				
					
# --------------
# docker-compose.yml
# --------------
version: '2'
services:
  zookeeper:
    image: confluentinc/cp-zookeeper:latest
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
    ports:
      - 22181:2181
  
  kafka:
    image: confluentinc/cp-kafka:latest
    depends_on:
      - zookeeper
    ports:
      - 29092:29092
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092,PLAINTEXT_HOST://localhost:29092
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
# --------------

## Create topic
# -------------
kafka-topics --bootstrap-server localhost:29092 --topic firstTopi1 --create --partitions 3 --replication-factor 1
# Created topic firstTopi1.


## List topics
# -------------
kafka-topics --bootstrap-server localhost:29092 --list
# firstTopi1
# first_topic
# test_topic


## Describe topic
# -------------
kafka-topics --bootstrap-server localhost:29092 --describe --topic first_topic
# Topic: first_topic      TopicId: VwJGV8HeSXa1NjmZ5Hfvxw PartitionCount: 3       ReplicationFactor: 1    Configs: 
#         Topic: first_topic      Partition: 0    Leader: 1       Replicas: 1     Isr: 1
#         Topic: first_topic      Partition: 1    Leader: 1       Replicas: 1     Isr: 1
#         Topic: first_topic      Partition: 2    Leader: 1       Replicas: 1     Isr: 1

## Delete topic
# -------------
kafka-topics --bootstrap-server localhost:29092 --delete --topic first_topic


## Produce message
# -------------
kafka-console-producer --bootstrap-server localhost:29092 --topic test_topic
>Hello World
>abc

# Consume latest message
# -------------
kafka-console-consumer --bootstrap-server localhost:29092 --topic test_topic
# -----
Hello World
abc

# Consume messages from begining
# -------------
kafka-console-consumer --bootstrap-server localhost:29092 --topic test_topic --from-beginning
# -----
hi
Hello
Hi
How 
are
you
hi
hello
sdf
				
			

Leave a Reply

Your email address will not be published. Required fields are marked *