kafka命令行操作

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值