kafka是如何控制一条

2025-12-17 16:16:28
推荐回答(1个)
回答1:

kafka的常用命令

新建一个topic

./bin/kafka-topics.sh --create --zookeeper hadoop04:2181,hadoop05:2181,hadoop06:2181 --partition 3 --replication-factor 3 --topic test-0

test-0是topic的名字

replication-factor 3 副本数

topic存储的是元数据,通过它找到真实的数据

改变分区

./bin/kafka-topics.sh --alter --zookeeper 192.168.14.131:2181,192.168.14.131:2182,192.168.14.131:2183 --partition 5 --topic test03

分区信息查询

./bin/kafka-topics.sh --describe --zookeeper hadoop04:2181,hadoop05:2181,hadoop06:2181 --topic test-0

结果为:leader主分区(broker Id) replicationfactor副本数 Isr是否存活(存储的顺序)

Topic:test-0 PartitionCount:3 ReplicationFactor:3 Configs:

Topic: test-0 Partition: 0 Leader: 0 Replicas: 0,2,1 Isr: 0,2,1

Topic: test-0 Partition: 1 Leader: 1 Replicas: 1,0,2 Isr: 1,0,2

Topic: test-0 Partition: 2 Leader: 2 Replicas: 2,1,0 Isr: 2,1,0

查询语句:

bin/kafka-topics.sh --list --zookeeper hadoop04:2181,hadoop05:2181,hadoop06:2181

删除语句

bin/kafka-topics.sh --delete --zookeeper hadoop04:2181,hadoop05:2181,hadoop06:2181 --topic test-0

结果是test-0 - marked for deletion

表示不能真正的删除只是做了个标记要删除取zookeeper去真正删除(这个配置如果设置为true可以真正删除,设置false不能删除)

#删除topic需要server.properties中设置delete.topic.enable=true否则只是标记删除

delete.topic.enable=false

rmr /brokers/topics/test-0 ,删除client中的brokers中topic对应的

rmr /config/topics/test-0 ,删配置

rmr /admin/delete_topics,删除admin中的标记为删除的topics

分区设置后只可以增加不可以减少

六API操作

启动生产者

./bin/kafka-console-producer.sh --broker-list 192.168.14.131:9092,192.168.14.132:9092,192.168.14.133:9092 --topic test03

启动消费者

./bin/kafka-console-consumer.sh --zookeeper 192.168.247.131:2181,192.168.247.132:2181,192.168.247.133:2181 --topic test03 --from-beginning