linux系統,kafka常用命令


kafka版本過高所致,2.2+=的版本,已經不需要依賴zookeeper來查看/創建topic,新版本使用 --bootstrap-server替換老版本的 --zookeeper-server。

[root@kwephis1160698 bin]# ./kafka-topics.sh --list --zookeeper localhost:2181
Exception in thread "main" joptsimple.UnrecognizedOptionException: zookeeper is not a recognized option
        at joptsimple.OptionException.unrecognizedOption(OptionException.java:108)
        at joptsimple.OptionParser.handleLongOptionToken(OptionParser.java:510)
        at joptsimple.OptionParserState$2.handleArgument(OptionParserState.java:56)
        at joptsimple.OptionParser.parse(OptionParser.java:396)
        at kafka.admin.TopicCommand$TopicCommandOptions.<init>(TopicCommand.scala:567)
        at kafka.admin.TopicCommand$.main(TopicCommand.scala:47)
        at kafka.admin.TopicCommand.main(TopicCommand.scala)

1、開啟動
./kafka-server-start.sh ../config/server.properties

后台啟動
./kafka-server-start.sh -daemon ../config/server.properties

./kafka-server-start.sh ../config/server.properties &

2、停止
./kafka-server-stop.sh

3、查看topic列表

./kafka-topics.sh --bootstrap-server localhost:9092 --list

[root@071-009-058-079 bin]# ./kafka-topics.sh --bootstrap-server localhost:9092 --list
__consumer_offsets
test

4、創建topic,指定分區數、副本數

./kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test_002

[root@071-009-058-079 bin]# ./kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 3 --topic test_002
WARNING: Due to limitations in metric names, topics with a period ('.') or underscore ('_') could collide. To avoid issues it is best to use either, but not both.
Created topic test_002.
[root@071-009-058-079 bin]# ./kafka-topics.sh --bootstrap-server localhost:9092 --list
__consumer_offsets
test
test_002

5、查看topic詳情,包括 Partition 個數,副本數,ISR 信息

./kafka-topics.sh  --bootstrap-server localhost:9092 --describe --topic test

[root@071-009-058-079 bin]# ./kafka-topics.sh  --bootstrap-server localhost:9092 --describe --topic test
Topic: test     TopicId: P_594pX_Q7q5ggCdCCjJ4Q PartitionCount: 1       ReplicationFactor: 1    Configs: 
        Topic: test     Partition: 0    Leader: 0       Replicas: 0     Isr: 0

6、修改指定 Topic 的 Partition 個數

./kafka-topics.sh  --bootstrap-server localhost:9092 --alter --topic test_002 --partitions 5

[root@071-009-058-079 bin]# ./kafka-topics.sh  --bootstrap-server localhost:9092 --list
__consumer_offsets
test
test_002
[root@071-009-058-079 bin]# ./kafka-topics.sh  --bootstrap-server localhost:9092 --describe --topic test_002
Topic: test_002 TopicId: QLwblYmBSx2HbTsppVokUQ PartitionCount: 3       ReplicationFactor: 1    Configs: 
        Topic: test_002 Partition: 0    Leader: 0       Replicas: 0     Isr: 0
        Topic: test_002 Partition: 1    Leader: 0       Replicas: 0     Isr: 0
        Topic: test_002 Partition: 2    Leader: 0       Replicas: 0     Isr: 0
[root@071-009-058-079 bin]# ./kafka-topics.sh  --bootstrap-server localhost:9092 --alter --topic test_002 --partitions 5 
[root@071-009-058-079 bin]# 
[root@071-009-058-079 bin]# ./kafka-topics.sh  --bootstrap-server localhost:9092 --describe --topic test_002
Topic: test_002 TopicId: QLwblYmBSx2HbTsppVokUQ PartitionCount: 5       ReplicationFactor: 1    Configs: 
        Topic: test_002 Partition: 0    Leader: 0       Replicas: 0     Isr: 0
        Topic: test_002 Partition: 1    Leader: 0       Replicas: 0     Isr: 0
        Topic: test_002 Partition: 2    Leader: 0       Replicas: 0     Isr: 0
        Topic: test_002 Partition: 3    Leader: 0       Replicas: 0     Isr: 0
        Topic: test_002 Partition: 4    Leader: 0       Replicas: 0     Isr: 0

7、刪除Topic

./kafka-topics.sh  --bootstrap-server localhost:9092 --delete --topic test_002

只會刪除zookeeper中的元數據,消息文件須手動刪除

[root@071-009-058-079 bin]# ./kafka-topics.sh  --bootstrap-server localhost:9092 --list
__consumer_offsets
test
test_002
[root@071-009-058-079 bin]# ./kafka-topics.sh  --bootstrap-server localhost:9092 --delete --topic test_002
[root@071-009-058-079 bin]# ./kafka-topics.sh  --bootstrap-server localhost:9092 --list
__consumer_offsets
test

8、控制台生產消息
./kafka-console-producer.sh --broker-list localhost:9092 --topic test

[root@071-009-058-079 bin]# ./kafka-console-producer.sh --broker-list localhost:9092 --topic test
>hello
>nihao
>haha

9、控制台消費消息

a)消費消息從頭開始

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

b)消費從最新的開始

./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --offset latest --partition 0

10、其它(暫未詳細補充)

./kafka-run-class.sh kafka.tools.GetOffsetShell --topic test01  --time -1 --broker-list 0.0.0.0:9092 --partitions 2
結果:test01:2:12495

徹底刪除topic,要到zookeeper客戶端
    --(1)刪除kafka存儲目錄(server.properties文件log.dirs配置,默認為"/tmp/kafka-logs")相關topic目錄
    --(2)如果配置了delete.topic.enable=true直接通過命令刪除,如果命令刪除不掉,直接通過zookeeper-client 刪除掉broker下的topic即可。
    
step1:cd /usr/local/zookeeper-3.6.2/bin/
step2:進入zookeeper客戶端
    # ./zkCli.sh
step3:查看有哪些topics./zkCli.sh./
    # ls /brokers/topics
step4:刪除某一topic
    # deleteall  /brokers/topics/test02

./kafka-consumer-groups.sh --bootstrap-server kafkadev:9091 --list --command-config ../config/producer.properties
./kafka-consumer-groups.sh --bootstrap-server 0.0.0.0:9092,0.0.0.0:9092,0.0.0.0:9092 --list --command-config ../config/producer.properties
查看kafka服務是否啟動
jps -l
ps -ef|grep kafka


免責聲明!

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



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