一、Kafka 启动与停止
kafka 启动命令
# 指定对应的配置文件启动
kafka-server-start.sh /hadoop/kafka/config/server.properties
# -daemon 以后台的方式启动
kafka-server-start.sh -daemon /hadoop/kafka/config/server.properties
# 指定JMX port端口启动,指定jmx,可以方便监控kafka集群
JMX_PORT=9991 kafka-server-start.sh -daemon /hadoop/kafka/config/server.properties
kafka 停止命令
kafka-server-stop.sh
或者
kill -9 pid
二、kafka topic 相关
创建 topic
# 创建一个 test 的topic 3个分区2个副本,并且注册中心为 localhost:2181
kafka-topics.sh --create --zookeeper localhost:2181 --topic test --replication-factor 2 --partitions 3
删除 topic
kafka-topics.sh --zookeeper :2181 --delete --topic test
说明:
如果kafka配置delete.topic.enable=true,那么可以直接删除topic
如果kafka配置delete.topic.enable=false,出现提示:
Topic test is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.
这里只是标记删除,并没有删除数据,同时也不能往这个topic写入数据。
手动删除topic信息,具体操作如下:
# 1. 打开zkCli
zkCli.sh -server localhost:2181
# 2. 删除zookeeper的topic相关信息
# 删除topic test的consumer group,如果有消费记录的话
rmr /consumers/test-group
rmr /config/topics/test
rmr /brokers/topics/test
rmr /admin/delete_topics/test
# 3. 在每台机器上,删除topic的log文件
rm -rf /hadoop/kafka/logs/test-*
# 4.重新启动kafka集群
kafka-server-stop.sh
kafka-server-start.sh -daemon /hadoop/kafka/config/server.properties
修改 topic 的分区
# 注意:分区数量只能增加,不能减少
kafka-topics.sh --zookeeper localhost:2181 --alter --topic test --partitions 5
查看topic
# 查看所有topic
kafka-topics.sh --zookeeper localhost:2181 --list
或
kafka-topics.sh --bootstrap-server localhost:9092 --list
# 查看所有topic的详细信息
kafka-topics.sh --zookeeper localhost:2181 --describe
# 查看指定topic的详细信息
kafka-topics.sh --zookeeper localhost:2181 --describe --topic test
# describe命令还提供一些参数,用于过滤输出结果,如:
# --topic-with-overrides:可以找出所有包含覆盖配置的主题,它只会列出包含与集群不一样配置的主题
kafka-topics.sh --zookeeper localhost:2181 --describe --topics-with-overrides
# describe有两个参数用于找出有问题的分区
# --unavailable-partitions:列出所有没有首领的分区,这些分区已经处于离线状态,对于生产者和消费者来说是不可用的
# --under-replicated-partitions:列出所有包含不同步副本的分区。
kafka-topics.sh --zookeeper localhost:2181 --describe --unavailable-partitions
kafka-topics.sh --zookeeper localhost:2181 --describe --under-replicated-partitions
三、kafka 生产者
开启一个 Producer 客户端
kafka-console-producer.sh --broker-list localhost:9092 --topic test
四、kafka 消费者
开启一个 Consumer 客户端
# --from-beginning 可以消费历史数据
kafka-console-consumer.sh --bootstrap-server localhost:9092 --from-beginning --topic test
# 指定group消费者组
kafka-console-consumer.sh --bootstrap-server localhost:9092 --from-beginning --group test_group --topic test
五、消费者组命令
说明:
在一个消费者群组中只会有一个消费者收到消息,另外的消费者是无法收到消息的;
如果两个消费者在不同的消费者群组中,那么两个消费者进程都能收到消息。
查询消费者组列表 --list
# 相当于zkCli客户端执行 ls /consumer
kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
查询消费者组详情--describe
# 所有消费者组详情 --all-group
kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --all-groups
# 指定消费者组详情 --group
kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group test_group
# 输出结果
TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID
test 0 8 8 0 -
test 1 8 8 0 -
test 2 8 8 0 -
# CURRENT-OFFSET: 当前消费者组最近提交的offset,也就是消费者分区里读取的当前位置
# LOG-END-OFFSET: 当前最高水位偏移量,也就是最近一个读取消息的偏移量,同时也是最近一个提交到集群的偏移量
# LAG:消费者的CURRENT-OFFSET与broker的LOG-END-OFFSET之间的差距
查询消费者组成员信息 --members
# 所有消费者组成员信息
kafka-consumer-groups.sh --describe --all-groups --members --bootstrap-server localhost:9092
# 指定消费者组成员信息
kafka-consumer-groups.sh --describe --members --group test_group --bootstrap-server localhost:9092
查询消费者组状态信息--state
# 所有消费者组状态信息
kafka-consumer-groups.sh --describe --state --all-groups --bootstrap-server localhost:9092
# 指定消费者组状态信息
kafka-consumer-groups.sh --describe --state --group test_group --bootstrap-server localhost:9092
删除消费者组--delete
# 删除所有消费者组
kafka-consumer-groups.sh --delete --all-groups --bootstrap-server localhost:9092
# 删除指定消费者组
kafka-consumer-groups.sh --delete --group test_group --bootstrap-server localhost:9092
说明:
想要删除消费者组前提是这个消费组的所有客户端都停止消费/不在线才能够成功删除,否则会报下面异常
Error: Deletion of some consumer groups failed:
* Group 'test_group' could not be deleted due to: java.util.concurrent.ExecutionException: org.apache.kafka.common.errors.GroupNotEmptyException: The group is not empty.