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