Skip to content

Instantly share code, notes, and snippets.

@tednaleid
Last active January 1, 2025 19:05
Show Gist options
  • Save tednaleid/1835e3629e245d7ef675bd8423ee0d5c to your computer and use it in GitHub Desktop.
Save tednaleid/1835e3629e245d7ef675bd8423ee0d5c to your computer and use it in GitHub Desktop.
minimal docker compose using KRaft and simple golang sarama code to list topics
# Minimal KRaft Kafka cluster with one broker
# create a topic with:
# docker exec -it kafka-compose-test-kafka-1 /opt/kafka/bin/kafka-topics.sh --create --topic new-topic --partitions 1 --replication-factor 1 --bootstrap-server localhost:9092
version: '3'
services:
kafka:
image: apache/kafka:3.8.0
ports:
- "9092:9092"
environment:
KAFKA_NODE_ID: 1
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,BROKER:PLAINTEXT,EXTERNAL:PLAINTEXT
KAFKA_LISTENERS: CONTROLLER://:19093,BROKER://:29092,EXTERNAL://0.0.0.0:9092
KAFKA_ADVERTISED_LISTENERS: BROKER://kafka:29092,EXTERNAL://localhost:9092
KAFKA_CONTROLLER_QUORUM_VOTERS: '1@kafka:19093'
KAFKA_PROCESS_ROLES: 'broker,controller'
KAFKA_INTER_BROKER_LISTENER_NAME: 'BROKER'
KAFKA_CONTROLLER_LISTENER_NAMES: 'CONTROLLER'
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'true'
// quick `klist` application to list topics on a kafka cluster using Sarama
package main
import (
"fmt"
"log"
"os"
"strings"
"github.com/IBM/sarama"
)
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: kafka-topics-lister <broker:port>")
os.Exit(1)
}
// Create config
config := sarama.NewConfig()
config.Version = sarama.V3_0_0_0 // Kafka 3.0+ version
// Create client
brokers := []string{os.Args[1]}
client, err := sarama.NewClient(brokers, config)
if err != nil {
log.Fatalf("Error creating client: %v", err)
}
defer client.Close()
// Get topics
topics, err := client.Topics()
if err != nil {
log.Fatalf("Error getting topics: %v", err)
}
// Print topics
if len(topics) == 0 {
fmt.Println("No topics found")
return
}
fmt.Println("Topics:")
fmt.Println(strings.Join(topics, "\n"))
}
# create the topic
docker exec -it kafka-compose-test-kafka-1 /opt/kafka/bin/kafka-topics.sh --create --topic new-topic --partitions 1 --replication-factor 1 --bootstrap-server localhost:9092
# running golang code above takes 20ms to list the topic
time ./klist 127.0.0.1:9092
Topics:
new-topic
./klist 127.0.0.1:9092 0.00s user 0.00s system 31% cpu 0.021 total
# kcat (brew install kcat) also takes 20ms
time kcat -L -b 127.0.0.1:9092
Metadata for all topics (from broker -1: 127.0.0.1:9092/bootstrap):
1 brokers:
broker 1 at localhost:9092 (controller)
1 topics:
topic "new-topic" with 1 partitions:
partition 0, leader 1, replicas: 1, isrs: 1
kcat -L -b 127.0.0.1:9092 0.01s user 0.01s system 72% cpu 0.029 total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment