栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

kafka命令总结

kafka命令总结

目录
    • 一、开始
    • 二、kafka-topics.sh
      • 1. 创建
      • 2. 查看 Topic
        • 查看 Topic 列表
        • 查看指定 Topic 明细
      • 3. 删除 Topic
    • 三、kafka-console-producer.sh
      • 1. 用producer生产数据(无key型消息)
    • 四、kafka-console-consumer.sh
      • 1. 用consumer消费数据
        • 从开始位置消费
        • 消息消费
      • 2.查看topic内容

代码主要来自http://dblab.xmu.edu.cn/blog/1358-2/

一、开始
cd /usr/local/kafka
./bin/zookeeper-server-start.sh config/zookeeper.properties
./bin/kafka-server-start.sh config/server.properties
二、kafka-topics.sh

参考博客:
https://blog.csdn.net/qq_29116427/article/details/80202392

1. 创建

用create创建自定义名称为“wordsendertest”的topic,确定新建 Topic 中有多少个分区及每个分区中的副本如何分配,可使用 replica-assignment 参数手动分配,也支持使用 partitions 参数和 replication-factor 参数指定分区个数和副本个数进行自动分配。

kafka2.1 及以下版本使用–zookeeper, 2.2 及以上版本用–bootstrap-server

./bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic wordsendertest

//这个topic叫wordsendertest,2181是zookeeper默认的端口号,partition是topic里面的分区数,replication-factor是备份的数量,在kafka集群中使用,这里单机版就不用备份了
2. 查看 Topic 查看 Topic 列表

用list列出所有创建的topics,来查看上面创建的topic是否存在。

./bin/kafka-topics.sh --list --zookeeper localhost:2181
查看指定 Topic 明细

用describe显示详细信息,例如新主题的分区数。

./bin/kafka-topics.sh --describe --topic wordsendertest --zookeeper localhost:2181
Topic:wordsendertest	PartitionCount:1	ReplicationFactor:1	Configs:
	Topic: wordsendertest	Partition: 0	Leader: 0	Replicas: 0	Isr: 0
3. 删除 Topic

用delete删除topic

./bin/kafka-topics.sh --delete --zookeeper localhost:2181 --topic wordsendertest

彻底删除topic及数据见博客,注意必须在 server.properties 里设置 delete.topic.enable=true。

三、kafka-console-producer.sh

参考博客:
https://blog.csdn.net/qq_29116427/article/details/105912397

1. 用producer生产数据(无key型消息)
./bin/kafka-console-producer.sh --broker-list localhost:9092 --topic wordsendertest

执行以上命令后,就会在控制台等待键入消息体,可以在当前终端内用键盘输入消息值,每行表示一条消息。

四、kafka-console-consumer.sh

参考博客:
https://blog.csdn.net/qq_29116427/article/details/80206125

1. 用consumer消费数据

启动一个消费者,来查看刚才producer产生的数据。

从开始位置消费

from-beginning代表从开始位置消费,表示从指定主题中有效的起始位移位置开始消费所有分区的消息,而不是从最新消息开始。

./bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic wordsendertest --from-beginning
消息消费

offset,表示执行消费的起始offset位置,默认值是latest。因此没有参数,表示从 latest 位移位置开始消费该主题的所有分区消息,即仅消费正在写入的消息。

./bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic wordsendertest
2.查看topic内容
./bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic wordsendertest --from-beginning
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/336044.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号