经常用到的Kafka命令记录以下,方便查找。kafka的搭建可以参考这篇文章Kafka和zookeeper搭建

Topic操作

  • 创建topic

    1
    bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 3 --topic test_topic
  • 查看topic具体信息

    1
    bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic test_topic
  • 查看所有的topic列表

    1
    bin/kafka-topics.sh --list --bootstrap-server localhost:9092
  • 删除topic

    1
    bin/kafka-topics.sh --zookeeper localhost:2181 --delete --topic test_topic
  • 修改topic副本数

    1
    bin/kafka-topics.sh --alter --zookeeper localhost:2181 --partitions 8 --topic test_topic

消费组操作

  • 显示所有消费组

    1
    bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --all-groups --list
  • 查看所有消费组详情

    1
    bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --all-groups --describe
  • 查看单独组的详细信息

    1
    bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group test_group --describe
  • 删除消费组(前提是没有在消费的)

    1
    bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group test_group  --delete

控制台生产消费

  • 生产者发送消息

    1
    bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test_topic

    然后控制台输入内容,回车即可

  • 消费者消费消息

    --from-beginning是指从头进行消费,可能会打印大量东西在控制台上。

    1
    bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test_topic --from-beginning