kafka报错:
如果出现以下错误:
Error while executing topic command : Replication factor: 1 larger than available brokers: 0.
先看看自己的kafka版本,我的是3.0版本,和之前版本的命令是不同的,如果按照之前版本的命令操作,即使是对的,可会报出这个错误
[wwj@hadoop102 bin]$ ./kafka-topics.sh --create --topic GMALL_START --zookeeper hadoop102:2181,hadoop103:2181,hadoop104:2181 --partitions 4 --replication-factor 1
结果报错:
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.
Error while executing topic command : Replication factor: 1 larger than available brokers: 0.
[2020-07-15 23:11:39,049] ERROR org.apache.kafka.common.errors.InvalidReplicationFactorException: Replication factor: 1 larger than available brokers: 0.
(kafka.admin.TopicCommand$)
使用这个命令就可以正常创建topic了:
kafka-topics.sh --create --bootstrap-server hadoop102:9092 --topic GMALL_START0213 --partitions 4 --replication-factor 1
kafka命令行操作
1)查看当前服务器中的所有topic
[test@hadoop102 kafka]$
kafka-topics.sh --list --bootstrap-server hadoop102:9092
kafka-topics.sh --list --bootstrap-server(链接kafka集群) hadoop102:9092(是kafka端口)
2)创建topic
[test@hadoop102 kafka]$ kafka-topics.sh --create --bootstrap-server hadoop102:9092 --topic first --partitions 2 --replication-factor 2
选项说明:
–topic 定义topic名
–replication-factor 定义副本数
–partitions 定义分区数
3)删除topic
[test@hadoop102 kafka]$ kafka-topics.sh --delete --bootstrap-server hadoop102:9092 --topic first
4)发送消息
[test@hadoop102 kafka]$ kafka-console-producer.sh --broker-list hadoop102:9092,hadoop103:9092,hadoop104:9092 --topic first
>hello world
>test test
5)消费消息:分区内消息有序,分区外部无序
[test@hadoop103 kafka]$ kafka-console-consumer.sh --bootstrap-server hadoop102:9092 --topic first --from-beginning
--from-beginning:会把主题中以往所有的数据都读取出来。
kafka-console-consumer.sh --bootstrap-server hadoop102:9092 --topic first
6)查看某个Topic的详情
[test@hadoop102 kafka]$kafka-topics.sh --bootstrap-server hadoop102:9092 --describe
7)修改分区数:增加分区可以,但是不能减少分区
[test@hadoop102 kafka]$kafka-topics.sh --bootstrap-server hadoop102:9092 --alter --topic first --partitions 3