查看当前服务器中的所有的topic,创建topic,删除topic,通过shell命令发送消息,通过shell消费消息,查看topic详情,对分区数进行修改...

一、 Kafka常用操作命令

 查看当前服务器中的所有topic

[root@hadoop3 kafka]# bin/kafka-topics.sh --list --zookeeper hadoop11:2181
[root@hadoop3 kafka]#
  • 1
  • 2

信息写入到 
 创建topic

[root@hadoop3 kafka]# bin/kafka-topics.sh --create --zookeeper hadoop11:2181 --replication-factor 1 -partitions 1 --topic test
Created topic "test".
[root@hadoop3 kafka]# bin/kafka-topics.sh --list --zookeeper hadoop11:2181
test
  • 1
  • 2
  • 3
  • 4

通过上面,可以看到已经创建了一个test的topic

 删除topic

[root@hadoop2 kafka]# bin/kafka-topics.sh --create --zookeeper hadoop11:2181 --replication-factor 1 -partitions 1 --topic test2
Created topic "test2".
[root@hadoop2 kafka]# bin/kafka-topics.sh --list --zookeeper hadoop11:2181
itheima
test
test2
[root@hadoop2 kafka]# bin/kafka-topics.sh --delete --zookeeper hadoop11:2181 --topic test2
Topic test2 is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.
[root@hadoop2 kafka]# bin/kafka-topics.sh --list --zookeeper hadoop11:2181
itheima
test
[root@hadoop2 kafka]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

需要server.properties中设置delete.topic.enable=true否则只是标记删除或者直接重启。 
 通过shell命令发送消息 
要注意的是要指定topic,表示要在哪个topic中生产消息,这里的topic需要时上面创建的topic

[root@hadoop3 kafka]# bin/kafka-console-producer.sh --broker-list hadoop1:9092 --topic test
asdfasdfasd
asdfasdf
asdfasdf
toto test
tuto test2
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

注意命令中指定的–block-list hadoop1:9092,当改成hadoop2:9092时,也可以。

 通过shell消费消息 
要指明的是要使用哪个topic中的数据,这里的topic需要时上面创建的topic:

[root@hadoop3 kafka]# sh bin/kafka-console-consumer.sh --zookeeper hadoop11:2181 --from-beginning --topic test
asdfasdfasd
asdfasdf
asdfasdf
toto test
tuto test2
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

注意:这里要指定消费那个topic,这里使用的是test.

 查看消费位置

[root@hadoop3 kafka]# sh bin/kafka-run-class.sh kafka.tools.ConsumerOffsetChecker --zookeeper hadoop11:2181 --group testGroup
  • 1

 查看某个Topic的详情

[root@hadoop3 kafka]# sh bin/kafka-topics.sh --topic test --describe --zookeeper hadoop11:2181
Topic:test  PartitionCount:1    ReplicationFactor:1 Configs:
    Topic: test Partition: 0    Leader: 0   Replicas: 0 Isr: 0
[root@hadoop3 kafka]#
  • 1
  • 2
  • 3
  • 4

 对分区数进行修改

[root@hadoop3 kafka]# bin/kafka-topics.sh --zookeeper hadoop11:2181 -alter --partitions 15 --topic test
WARNING: If partitions are increased for a topic that has a key, the partition logic or ordering of the messages will be affected
Adding partitions succeeded!
[root@hadoop3 kafka]#

转载于:https://my.oschina.net/lsl1991/blog/1576516

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值