Kafka操作命令


1. Kafka下載:
wget https://archive.apache.org/dist/kafka/0.8.1/kafka_2.9.2-0.8.1.tgz

解壓 tar zxvf kafka_2.9.2-0.8.1.tgz

啟動zookeeper

nohup bin/zookeeper-server-start.sh config/zookeeper.properties &

或者 zkServer.sh start 啟動

啟動kafka

nohup bin/kafka-server-start.sh -daemon config/server.properties &

創建主題

bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
bin/kafka-topics.sh --create --zookeeper master:2181 --replication-factor 2 --partitions 8 --topic DreamTopic
bin/kafka-topics.sh --create --zookeeper 192.168.100.125:2181 --replication-factor 1 --partitions 1 --topic DreamTopic1

#replication-factor 表示該topic需要在不同的broker中保存幾份, partitions為幾個分區

查看topic個數

  bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic test
  bin/kafka-topics.sh --describe --zookeeper 192.168.100.125:2181 --topic DreamTopic1

  bin/kafka-topics.sh --list --zookeeper master:2181,node2:2181,node1:2181

查看topic的生產者和消費者信息

./kafka-topics.sh --describe --zookeeper localhost:2181 --topic idoall_testTopic,DreamTopic

[root@linux-node2 bin]# ./kafka-topics.sh --describe --zookeeper localhost:2181 --topic dream
Topic:dream    PartitionCount:5    ReplicationFactor:2    Configs:
    Topic: dream    Partition: 0    Leader: 1    Replicas: 1,2    Isr: 1,2
    Topic: dream    Partition: 1    Leader: 2    Replicas: 2,3    Isr: 2,3
    Topic: dream    Partition: 2    Leader: 3    Replicas: 3,1    Isr: 3,1
    Topic: dream    Partition: 3    Leader: 1    Replicas: 1,3    Isr: 1,3
    Topic: dream    Partition: 4    Leader: 2    Replicas: 2,1    Isr: 2,1

leader:負責處理消息的讀和寫,leader是從所有節點中隨機選擇的.

Replicas:列出了所有的副本節點,不管節點是否在服務中.

Lsr:是正在服務中的節點.

 

生產者測試

bin/kafka-console-producer.sh --broker-list kafka1:9092 --topic test
bin/kafka-console-producer.sh --broker-list 192.168.100.125:9092 --topic DreamTopic1

消費者測試

bin/kafka-console-consumer.sh --zookeeper 192.168.100.125:2181 --topic test --from-beginning
bin/kafka-console-consumer.sh --zookeeper kafka1:2181 --topic test --from-beginning
bin/kafka-console-consumer.sh --zookeeper 192.168.100.125:2181 --topic DreamTopic1 --from-beginning

kafka刪除topic方法

1) bin/kafka-topics.sh --delete --zookeeper master:2181 --topic DreamTopic

如果刪除后查看topic顯示為:marked for deletion  則需要在每一台機器中的 config/server.properties 文件加入  delete.topic.enable=true,然后重啟kafka

2) 刪除kafka存儲目錄(server.properties文件log.dirs配置,默認為"/tmp/kafka-logs")相關topic目錄刪除zookeeper "/brokers/topics/"目錄下相關topic節點


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM