kafka 常用命令行操作(查看主题,生产数据,消费数据)

您所在的位置:网站首页 kafka消费者命令 kafka 常用命令行操作(查看主题,生产数据,消费数据)

kafka 常用命令行操作(查看主题,生产数据,消费数据)

2023-09-28 02:20| 来源: 网络整理| 查看: 265

一 topic相关操作 1.1 查看 主题命令参数

[root@localhost kafka_2.12-2.1.0]#  bin/kafka-topics.sh

1.2 查看集群中所有topic主题

[root@localhost kafka_2.12-2.1.0]# bin/kafka-topics.sh  --list --zookeeper 192.168.152.138 kafka_test [root@localhost kafka_2.12-2.1.0]# 

1.3 创建主题

[root@localhost kafka_2.12-2.1.0]# bin/kafka-topics.sh --create --zookeeper 192.168.152.138:2181 --replication-factor 2 --partitions 3 --topic kafka_test 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 "kafka_test".

1.4 查看主题描述

[root@localhost kafka_2.12-2.1.0]# bin/kafka-topics.sh --describe --zookeeper 192.168.152.140:2181 --topic kafka_test Topic:kafka_test    PartitionCount:3    ReplicationFactor:2    Configs:     Topic: kafka_test    Partition: 0    Leader: 1    Replicas: 1,2    Isr: 2,1     Topic: kafka_test    Partition: 1    Leader: 2    Replicas: 2,0    Isr: 2,0     Topic: kafka_test    Partition: 2    Leader: 0    Replicas: 0,1    Isr: 1,0

解释:

第一行给出了所有分区的摘要。后面以下每一行给出一个partition中的信息,如果我们只有一个partition,则只显示一行。这里有3个分区,显示3行分区的详情信息。

leader 是在给出的所有partitons中负责读写的节点(borker的id),每个节点都有可能成为leader

replicas 显示给定partiton所有副本所存储节点的节点列表(broker的id列表),不管该节点是否是leader或者是否存活。  

isr 副本都已同步的的节点集合,这个集合中的所有节点都是存活状态,并且跟leader同步。

举例:

    Topic: kafka_test    Partition: 0    Leader: 1    Replicas: 1,2    Isr: 2,1     Topic: kafka_test    Partition: 1    Leader: 2    Replicas: 2,0    Isr: 2,0     Topic: kafka_test    Partition: 2    Leader: 0    Replicas: 0,1    Isr: 1,0

测试Kafka集群一共三个节点,第一行这个主题“kafka_test" 的编号为0的分区,leader(分区为0的的主分区)在borker的id为1的这个节点上,副本在borker的id为1,2的这两个节点上,并且所有副本都是存活的,并跟主节点broker.id=1这个节点进行同步。

leader:理解为分区 的主分区,replicas为分区的副本

leader,replicas后面的数字为borker的id值,节点的编号

(注意:分区数只能增加,不能减少)  1.5 生产者

[root@localhost kafka_2.12-2.1.0]# bin/kafka-console-producer.sh --broker-list 192.168.152.136:9092,192.168.152.140:9092,192.168.152.138:9092 --topic kafka_test >nihao changqingteng! >Kafka是由Apache软件基金会开发的一个开源流处理平台 >

1.6 消费者 

[root@localhost kafka_2.12-2.1.0]# bin/kafka-console-consumer.sh --from-beginning --topic kafka_test  --bootstrap-server  192.168.152.136:9092,192.168.152.138:9092,192.168.152.140:9092 nihao changqingteng! Kafka是由Apache软件基金会开发的一个开源流处理平台  

 1.7 修改主题 (注意:分区数只能增加,不能减少)

[root@localhost kafka_2.12-2.1.0]# bin/kafka-topics.sh --zookeeper 192.168.152.126:2181 --alter --topic kafka_test --partitions 5 ^C[root@localhost kafka_2.12-2.1.0]# bin/kafka-topics.sh --zookeeper 192.168.152.136:2181 --alter --topic kafka_test --partitions 5 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@localhost kafka_2.12-2.1.0]# bin/kafka-topics.sh --describe --zookeeper 192.168.152.140:2181 --topic kafka_test

Topic:kafka_test    PartitionCount:5    ReplicationFactor:2    Configs:     Topic: kafka_test    Partition: 0    Leader: 1    Replicas: 1,2    Isr: 2,1     Topic: kafka_test    Partition: 1    Leader: 2    Replicas: 2,0    Isr: 2,0     Topic: kafka_test    Partition: 2    Leader: 0    Replicas: 0,1    Isr: 1,0     Topic: kafka_test    Partition: 3    Leader: 1    Replicas: 1,2    Isr: 1,2     Topic: kafka_test    Partition: 4    Leader: 2    Replicas: 2,0    Isr: 2,0 [root@localhost kafka_2.12-2.1.0]#   

  1.8 删除主题

[root@localhost kafka_2.12-2.1.0]# bin/kafka-topics.sh --zookeeper 192.168.152.136:2181 --delete --topic kafka_test Topic kafka_test is marked for deletion. Note: This will have no impact if delete.topic.enable is not set to true. [root@localhost kafka_2.12-2.1.0]# bin/kafka-topics.sh  --list --zookeeper 192.168.152.138

__consumer_offsets [root@localhost kafka_2.12-2.1.0]#  

可以看到删除topic后,进行查看,查看不到了!  



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3