Apache Kafkaをコマンドラインで試してみる

  • 2022.08.23
  • IT
NO IMAGE

 

–bootstrap-serverとは

Messageを受信するKafkaクラスタのBrokerのホスト名とポート番号を指定する

–bootstrap-server localhost:9092

ブローカーとトピックとパーテーションの関係

Producer[データ送信]->Broker[topic[partition1, partition2, etc…]]

パーテーションの作成

こんな感じでパーテーションを指定したりして作れますと。
1009 kafka-topics --bootstrap-server localhost:9092 --topic first-topic --create
1010 kafka-topics --bootstrap-server localhost:9092 --list
1011 kafka-topics --bootstrap-server localhost:9092 --topic first-topic --create --partitions 3
1012 kafka-topics --bootstrap-server localhost:9092 --topic second-topic --create --partitions 3
1013 kafka-topics --bootstrap-server localhost:9092 --topic second-topic --describe
1014 kafka-topics --bootstrap-server localhost:9092 --topic first-topic --describe

ブローカーよりも、レプリカが多いとエラーが起きる

1015 kafka-topics --bootstrap-server localhost:9092 --topic third-topic --create --partitions 3 --replication-factor 2
1017 kafka-topics --bootstrap-server localhost:9092 --topic second-topic --delete
1018 kafka-topics --bootstrap-server localhost:9092 --list
1019 kafka-topics --bootstrap-server localhost:9092 --topic first-topic --delete

データの挿入には、Producerを使う

1023 kafka-console-producer --bootstrap-server localhost:9092 --topic first-tooic
1024 kafka-console-producer --bootstrap-server localhost:9092 --topic first-topic

データの閲覧には、Consumerを使う。

1001 kafka-console-consumer --bootstrap-server localhost:9092 --topic first-topic
1002 kafka-console-consumer --bootstrap-server localhost:9092 --topic first-topic --from-beginning
1003 kafka-console-consumer --bootstrap-server localhost:9092 --topic first-topic --formatter kafka.tools.DefaultMessageFormatter --property print.timestamp=true --property print.key=true --property print.value=true --from-beginning
–from-beginingオプションを付けないと、最初からのデータを見ることはできない。
ちなみにパーテーションが1つじゃない場合は、ランダムで値が出てくる。

Consumer groupについて。

kafka-console-consumerを複数のタブで実行して、データを受信した場合、通常は、タブそれぞれでデータを受信する。
でも、グループにすると、ブローカーからのデータは、グループ内でテキトウに分配される。
ただし、グループ内のConsumerの数は、パーテーションの数だけに限定される。Consumerがパーテーションの数を超える場合、
超えた分のパーテーションにはデータは送信されない。
kafka-console-consumer --bootstrap-server localhost:9092 --topic first-topic --group my-first-application