kafka 常用命令

kafka 常用命令。

生产者

发送消息

发送消息有如下三中方式:

  • 发送并忘记(fire-and-forget): 不关心消息是否达到,大部分时候是可靠的,可能会丢一部分消息;
  • 同步发送:
  • 异步发送

topic 相关

1
2
3
4
5
# 创建 topic
./kafka-topics.sh --create --zookeeper zookeeper:2181 --replication-factor 1 --partitions 1 --topic test

#列出所有 topic
./kafka-topics.sh --zookeeper zookeeper:2181/kafka --list

查看 topic

1
2
3
4
# ./kafka-topics.sh --zookeeper zookeeper:2181 --describe --topic test

Topic:test PartitionCount:1 ReplicationFactor:1 Configs:
Topic: test Partition: 0 Leader: 0 Replicas: 0 Isr: 0

增加分区数量

1
2
3
4
5
6
7
8
# ./kafka-topics.sh --zookeeper zookeeper:2181 --alter --topic test --partitions 4

# ./kafka-topics.sh --zookeeper zookeeper:2181 --describe --topic test
Topic:test PartitionCount:4 ReplicationFactor:1 Configs:
Topic: test Partition: 0 Leader: 0 Replicas: 0 Isr: 0
Topic: test Partition: 1 Leader: 0 Replicas: 0 Isr: 0
Topic: test Partition: 2 Leader: 0 Replicas: 0 Isr: 0
Topic: test Partition: 3 Leader: 0 Replicas: 0 Isr: 0

消息相关

1
2
3
4
5
6
7
8
# 发送消息
./kafka-console-producer.sh --broker-list localhost:9092 --topic test

# 消费消息
./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning

# 消费一条消息
./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic t_my_topic --max-messages 1

group 相关

1
2
3
4
5
6
7
8
#列出所有 consumer group
./kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list

#某个 consumer group 信息
./kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group group_id_qa

# 将某个 group 的 topic 重置到 earliest
./kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group example --topic aether_hosts --execute --reset-offsets --to-earliest