kafka2:常用topic命令
常用操作
创建一个topic
bin/kafka-topics.sh –create –zookeeper 192.168.3.230:2181 –replication-factor 3 –partitions 1 –topic test-topic
–topic指定topic name
–partitions指定分区数,这个参数需要根据broker数和数据量决定,正常情况下,每个broker上两个partition最好;
–replication-factor指定partition的replicas数,建议设置为2;
查看创建的topic
bin/kafka-topics.sh –describe –zookeeper 192.168.3.230:2181 –topic test-topic
Topic:test-replicated-topic PartitionCount:1 ReplicationFactor:3 Configs:
Topic: test-replicated-topic Partition: 0 Leader: 1 Replicas: 1,2,0 Isr: 1,2,0
查看topic列表及具体信息<
bin/kafka-topics.sh –describe –zookeeper 192.168.3.230 :2181
创建消息
bin/kafka-console-producer.sh –broker-list 192.168.3.234:9092 –topic test-topic
消费消息
bin/kafka-console-consumer.sh –zookeeper 192.168.3.234:2181 –from-beginning –topic test-topic
程序自动创建
开启自动创建配置:auto.create.topics.enable=true
使用程序直接往kafka中相应的topic发送数据,如果topic不存在就会按默认配置进行
kafka删除topic方法
- 添加配置:
${kafka_home}/config/server.properties
delete.topic.enable=true
- 重启kafka server:
${kafka_home}/bin/kafka-server-start.sh ~/kafka/config/server.properties
- 删除指定topic
${kafka_home}/bin/kafka-topics.sh \--delete \--zookeeper localhost:2181 \--topic daemon12