Kafka服务器命令总结

时间:2025-03-20 18:37:02

1. 生产者相关

  1. 发送消息(这里注意不能使用localhost,要使用指定ip)
[root@iZ2zedqr9yeos47fg4uor5Z kafka_2.12-2.8.0]# bin/ --broker-list 172.24.10.198:9092 --topic test
>a
>b

2. 消费者相关

  1. 消费消息
[root@iZ2zedqr9yeos47fg4uor5Z kafka_2.12-2.8.0]# bin/ --bootstrap-server 172.24.10.198:9092 --topic test --from-beginning
a
b
  1. 查看所有的消费者组
[root@iZ2zedqr9yeos47fg4uor5Z kafka_2.12-2.8.0]# ./bin/ --bootstrap-server 172.24.10.198:9092 --list
test-consumer-group
  1. 查看指定消费者组的消费情况
[root@iZ2zedqr9yeos47fg4uor5Z kafka_2.12-2.8.0]# ./bin/ --bootstrap-server 172.24.10.198:9092 --describe --group test-consumer-group

Consumer group 'test-consumer-group' has no active members.

GROUP               TOPIC           PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID     HOST            CLIENT-ID
test-consumer-group test            0          4611            4611            0               -               -               -
  1. 重置offset
[root@iZ2zedqr9yeos47fg4uor5Z bin]# ./ --bootstrap-server 172.24.10.198:9092 --group test-consumer-group --reset-offsets --topic test2 --to-offset 100 --execute

GROUP                          TOPIC                          PARTITION  NEW-OFFSET
test-consumer-group            test2                          0          100
--to-earliest:   设置到最早位移处,也就是0
--to-latest:     设置到最新处,也就是主题分区HW的位置
--to-offset NUM: 指定具体的位移位置
--shift-by NUM:  基于当前位移向前回退多少
--by-duration:   回退到多长时间

3. 主题相关

  1. 查看主题
[root@iZ2zedqr9yeos47fg4uor5Z kafka_2.12-2.8.0]# bin/ --list --zookeeper localhost:2181
__consumer_offsets
test
zxf
  1. 清除主题数据
[root@iZ2zedqr9yeos47fg4uor5Z bin]# ./ --delete --zookeeper 172.24.10.198:2181 --topic test
Topic test is marked for deletion.
Note: This will have no impact if  is not set to true.
  1. 修改某个主题的过期时间(不需要重启kafka,内存修改,重启kafka后会丢失)
[root@iZ2zedqr9yeos47fg4uor5Z bin]# ./ --zookeeper localhost:2181 --entity-type topics --entity-name topic_name_xx --alter --add-config =1296000000
Warning: --zookeeper is deprecated and will be removed in a future version of Kafka.
Use --bootstrap-server instead to specify a broker to connect to.
Completed updating config for entity: topic 'topic_name_xx'.
  1. 查看某个主题的当前过期时间配置
[root@iZ2zedqr9yeos47fg4uor5Z bin]#  ./ --zookeeper localhost:2181 --topic topic_name_xx --describe
Topic: topic_name_xx        TopicId: FntNDJn6QdS9XEiA8FDUIw PartitionCount: 1       ReplicationFactor: 1    Configs: =1296000000
        Topic: topic_name_xx        Partition: 0    Leader: 0       Replicas: 0     Isr: 0