前提:使用kafka2.0
旧版指定服务器用--zookeeper ,新版用--bootstrap-server
//查看所有主题的具体情况,包括分区,leader、isr
/usr/local/kafka/bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe
Topic: llc-test-topic PartitionCount: 1 ReplicationFactor: 1 Configs: segment.bytes=1073741824
Topic: llc-test-topic Partition: 0 Leader: 0 Replicas: 0 Isr: 0
Topic: llc2021 PartitionCount: 4 ReplicationFactor: 3 Configs: segment.bytes=1073741824
Topic: llc2021 Partition: 0 Leader: 0 Replicas: 2,3,0 Isr: 0,2,3
Topic: llc2021 Partition: 1 Leader: 0 Replicas: 3,0,2 Isr: 0,2,3
Topic: llc2021 Partition: 2 Leader: 0 Replicas: 0,2,3 Isr: 0,2,3
Topic: llc2021 Partition: 3 Leader: 0 Replicas: 2,0,3 Isr: 0,2,3
...略(还有_consumer_offsets默认50个partitions)
//查看指定主题的具体情况,包括分区,leader、isr
/usr/local/kafka/bin/kafka-topics.sh --bootstrap-server localhost:9092 --topic llc2021 --describe
Topic: llc2021 PartitionCount: 4 ReplicationFactor: 3 Configs: segment.bytes=1073741824
Topic: llc2021 Partition: 0 Leader: 0 Replicas: 2,3,0 Isr: 0,2,3
Topic: llc2021 Partition: 1 Leader: 0 Replicas: 3,0,2 Isr: 0,2,3
Topic: llc2021 Partition: 2 Leader: 0 Replicas: 0,2,3 Isr: 0,2,3
Topic: llc2021 Partition: 3 Leader: 0 Replicas: 2,0,3 Isr: 0,2,3
//列出所有主题名字
/usr/local/kafka/bin/kafka-topics.sh --bootstrap-server localhost:9092 --list
llc-test-obj-topic
llc-test-topic
llc2021
//创建topic
/usr/local/kafka/bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic llc2022 --replication-factor 3 --partitions 2
//查询后的结果
Topic: llc2022 PartitionCount: 2 ReplicationFactor: 3 Configs: segment.bytes=1073741824
Topic: llc2022 Partition: 0 Leader: 3 Replicas: 3,0,2 Isr: 3,0,2
Topic: llc2022 Partition: 1 Leader: 0 Replicas: 0,2,3 Isr: 0,2,3
//增加分区(不能减少分区,不然报错)
/usr/local/kafka/bin/kafka-topics.sh --bootstrap-server localhost:9092 --alter --topic llc2022 --partitions 3
//查询后的结果
Topic: llc2022 PartitionCount: 3 ReplicationFactor: 3 Configs: segment.bytes=1073741824
Topic: llc2022 Partition: 0 Leader: 3 Replicas: 3,0,2 Isr: 3,0,2
Topic: llc2022 Partition: 1 Leader: 0 Replicas: 0,2,3 Isr: 0,2,3
Topic: llc2022 Partition: 2 Leader: 2 Replicas: 2,3,0 Isr: 2,3,0
//减少分区会报错
Error while executing topic command : Topic currently has 3 partitions, which is higher than the requested 2.
[2021-12-05 00:42:47,425] ERROR org.apache.kafka.common.errors.InvalidPartitionsException: Topic currently has 3 partitions,
which is higher than the requested 2. (kafka.admin.TopicCommand$)
//删除topic
/usr/local/kafka/bin/kafka-topics.sh --bootstrap-server localhost:9092 --delete --topic llc-test-obj-topic
//查询后结果
__consumer_offsets
llc-test-topic
llc2021
llc2022
//发送消息
/usr/local/kafka/bin/kafka-console-producer.sh --bootstrap-server localhsot:9092 --topic llc2022
等待键入消息,按回车等于发送消息,注意这是没有key的,如果需要key,加上参数 --property parse.key=true
//无key
>Hello Kafka!
>你好 kafka!
//有key
>Lei Li Hello Kafka!
>Meimei Han 你好 kafka!
//列出消费者组
/usr/local/kafka/bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
sadan
sadan_fake
//某个消费者组具体详情
/usr/local/kafka/bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group sadan_fake --describe
GROUP TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
sadan_fake llc2022 0 - 0 - consumer-sadan_fake-1-2e2f360e-89de-416d-a3f6-2c07ba235561 /192.168.1.106 consumer-sadan_fake-1
sadan_fake llc2021 0 0 0 0 consumer-sadan_fake-1-2e2f360e-89de-416d-a3f6-2c07ba235561 /192.168.1.106 consumer-sadan_fake-1
sadan_fake llc2021 1 0 0 0 consumer-sadan_fake-1-2e2f360e-89de-416d-a3f6-2c07ba235561 /192.168.1.106 consumer-sadan_fake-1
sadan_fake llc2022 2 - 0 - consumer-sadan_fake-3-bc05f9ec-7d16-4dad-ac40-2876c43cdc87 /192.168.1.106 consumer-sadan_fake-3
sadan_fake llc2021 3 4 4 0 consumer-sadan_fake-3-bc05f9ec-7d16-4dad-ac40-2876c43cdc87 /192.168.1.106 consumer-sadan_fake-3
sadan_fake llc2022 1 - 0 - consumer-sadan_fake-2-43b7b396-f7ed-4266-9b85-fc7f78f09e3c /192.168.1.106 consumer-sadan_fake-2
sadan_fake llc2021 2 1 1 0 consumer-sadan_fake-2-43b7b396-f7ed-4266-9b85-fc7f78f09e3c /192.168.1.106 consumer-sadan_fake-2
参考:
kafka命令大全