• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

kafka常用脚本

武飞扬头像
tuhoooo
帮助1

原文地址: kafka常用脚本

欢迎访问: 我的博客

引言

本文是转载的, 但是原文已经找不到了.

Kafka安装目录下 ($KAFKA_HOME/bin), 提供了很多内置的脚本供我们使用, 使用脚本可以测试 Kafka 的大多数功能, 下面我们就脚本的使用作出说明.

启动 broker

bin/kafka-server-start.sh 脚本提供了启动 broker 的功能.

前台启动

bin/kafka-server-start.sh config/server.properties

后台启动:

bin/kafka-server-start.sh -daemon config/server.properties

停止 broker

bin/kafka-server-stop.sh 脚本提供了停止 broker 的功能.

bin/kafka-server-stop.sh

创建topic

bin/kafka-topic.sh脚本提供了创建 topic 的功能. 创建 topic 时, 需要指定两个参数:

  • 分区数量
  • 副本数量

注意, 副本数量最多不能超过当前集群中 broker 节点的数量.

下面创建一个名为 test 的 topic, 具有 3 个分区, 副本数量为 2.

bin/kafka-topics.sh --zookeeper localhost:2181 --create --partitions 3 --replication-factor 2 --topic test

查看topic列表

bin/kafka-topic.sh脚本提供了查看 topic 列表的功能. 通过 --list 参数即可查看当前集群所有的 topic 列表.

bin/kafka-topics.sh --zookeeper localhost:2181 --list
__consumer_offsets
test

查看某个topic的详细信息

bin/kafka-topic.sh 脚本提供了查看某个 topic 详细信息的功能. 通过 --describe 参数和 --topic 参数指定 topic 名称即可.

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

删除topic

bin/kafka-topic.sh脚本提供了删除 topic 的功能. 通过--delete 参数和--topic参数指定 topic 名称即可.

将名称为 test 的 topic 删除:

bin/kafka-topics.sh --zookeeper localhost:2181/kafka --delete --topic test
Topic test is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.

注意, broker 配置中必须设置 delete.topic.enable=true, 否则删除操作不会生效.

命令行生产者

bin/kafka-console-producer.sh 脚本能够通过命令行输入向指定的 topic 中发送数据.

向名称为 test 的 topic 中发送 3 条数据:

bin/kafka-console-producer.sh --broker-list hostname:9092 --topic test
This is a message
This is another message
hello kafka

命令行消费者

bin/kafka-console-consumer.sh脚本能够消费 topic 中的数据并打印到控制台.

消费名称为 test 的 topic 中的数据:

bin/kafka-console-consumer.sh --bootstrap-server hostname:9092 --topic test --from-beginning
This is a message
This is another message
hello kafka
^CProcessed a total of 3 messages

注意, –from-beginning 参数表示从 topic 的最开始位置进行消费, 如果没有指定该参数, 表示从末尾开始消费, 只有当启动消费者后, 有新的数据写入, 才能够显示到控制台.

查看消费组

bin/kafka-consumer-groups.sh 脚本能够查看集群中消费组的信息. 通过--list 参数能够列出当前消费组列表.

bin/kafka-consumer-groups.sh --bootstrap-server hostname:9092 --new-consumer --list
console-consumer-54336

通过 --describe 参数可以查看消费组的消费详情:

bin/kafka-consumer-groups.sh --bootstrap-server company01:9092 --new-consumer --describe --group console-consumer-54336

TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
test 0 1 1 0 consumer-1-860f59b3-ebe9-4cda-a074-d108b1fec7bf /192.168.100.109 consumer-1
test 1 1 1 0 consumer-1-860f59b3-ebe9-4cda-a074-d108b1fec7bf /192.168.100.109 consumer-1
test 2 1 1 0 consumer-1-860f59b3-ebe9-4cda-a074-d108b1fec7bf /192.168.100.109 consumer-1

CURRENT-OFFSET 表示消费者当前消费到该分区的偏移量.

LOG-END-OFFSET 表示当前分区最后的偏移量.

LAG 表示消费者 “落后” 的消费进度.

原文地址: kafka常用脚本

欢迎访问: 我的博客

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhhhekkf
系列文章
更多 icon
同类精品
更多 icon
继续加载