Open5
Apache Kafkaを試す

Dockerコンテナにインストールしてみる。
ホストマシン上で以下を実行して、Javaのコンテナを起動する
docker run --name kafka -it eclipse-temurin:21-alpine bash
コンテナ上で、Kafkaをダウンロードする
cd /opt
wget https://dlcdn.apache.org/kafka/3.6.0/kafka_2.13-3.6.0.tgz
tar -xzf kafka_2.13-3.6.0.tgz
cd kafka_2.13-3.6.0

KRaft(ZooKeeperなし)でKafkaを起動する
KAFKA_CLUSTER_ID="$(bin/kafka-storage.sh random-uuid)"
bin/kafka-storage.sh format -t $KAFKA_CLUSTER_ID -c config/kraft/server.properties
bin/kafka-server-start.sh config/kraft/server.properties

別コンソールからコンテナにログインする
docker exec -it kafka bash

トピックを作成する。
# bin/kafka-topics.sh --create --topic sample-topic --bootstrap-server localhost:9092
Created topic sample-topic.
# bin/kafka-topics.sh --describe --topic sample-topic --bootstrap-server localhost:9092
Topic: sample-topic TopicId: wgLkiJY7QpiVkOvz-QiSsw PartitionCount: 1 ReplicationFactor: 1 Configs: segment.bytes=1073741824
Topic: sample-topic Partition: 0 Leader: 1 Replicas: 1 Isr: 1

イベントをトピックに書き込む。Ctrl+C
で書き込みを終了できる。
# bin/kafka-console-producer.sh --topic sample-topic --bootstrap-server localhost:9092
>This is my first event
>This is my second event
>^C
トピックのイベントを読み込む。Ctrl+C
で読み込みを中断できる。
# bin/kafka-console-consumer.sh --topic sample-topic --from-beginning --bootstrap-server localhost:9092
This is my first event
This is my second event
^C