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

前端之家收集整理的这篇文章主要介绍了查看当前服务器中的所有的topic,创建topic,删除topic,通过shell命令发送消息,通过shell消费消息,查看topic详情,对分区数进行修改前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一、 Kafka常用操作命令

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

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

信息写入到
 创建topic

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

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

删除topic

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

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

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

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

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

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

 查看消费位置

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

 查看某个Topic的详情

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

 对分区数进行修改

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

猜你在找的Bash相关文章