Kafka命令行使用方法

命令行操作

kafka命令行操作主要分为三大类:

  • 主题命令行操作
  • 生产者命令行操作
  • 消费者命令行操作

下面以Kafka 2.3.0版本为例介绍常用的脚本使用方法,如果要下载其它Kafka版本,访问该链接Kakfa Download

主题命令行

查看主题命令行参数

进出kafka部署路径下的bin目录,执行kafka-topics.sh脚本,所有执行参数如下所示:

Create, delete, describe, or change a topic.
Option                                   Description
------                                   -----------
--alter                                  Alter the number of partitions, replica assignment, and/or
                                           configuration for the topic.
--at-min-isr-partitions          if set when describing topics, only
                                           show partitions whose isr count is
                                           equal to the configured minimum. Not
                                           supported with the --zookeeper
                                           option.
--bootstrap-server <String: server to    REQUIRED: The Kafka server to connect
  connect to>                              to. In case of providing this, a
                                           direct Zookeeper connection won't be
                                           required.
--command-config <String: command        Property file containing configs to be
  config property file>                    passed to Admin Client. This is used
                                           only with --bootstrap-server option
                                           for describing and altering broker
                                           configs.
--config <String: name=value>            A topic configuration override for the
                                           topic being created or altered.The
                                           following is a list of valid
                                           configurations:
                                                cleanup.policy
                                                compression.type
                                                delete.retention.ms
                                                file.delete.delay.ms
                                                flush.messages
                                                flush.ms
                                                follower.replication.throttled.
                                           replicas
                                                index.interval.bytes
                                                leader.replication.throttled.replicas
                                                max.compaction.lag.ms
                                                max.message.bytes
                                                message.downconversion.enable
                                                message.format.version
                                                message.timestamp.difference.max.ms
                                                message.timestamp.type
                                                min.cleanable.dirty.ratio
                                                min.compaction.lag.ms
                                                min.insync.replicas
                                                preallocate
                                                retention.bytes
                                                retention.ms
                                                segment.bytes
                                                segment.index.bytes
                                                segment.jitter.ms
                                                segment.ms
                                                unclean.leader.election.enable
                                         See the Kafka documentation for full
                                           details on the topic configs.It is
                                           supported only in combination with --
                                           create if --bootstrap-server option
                                           is used.
--create                                 Create a new topic.
--delete                                 Delete a topic
--delete-config <String: name>           A topic configuration override to be
                                           removed for an existing topic (see
                                           the list of configurations under the
                                           --config option). Not supported with
                                           the --bootstrap-server option.
--describe                               List details for the given topics.
--disable-rack-aware                     Disable rack aware replica assignment
--exclude-internal                       exclude internal topics when running
                                           list or describe command. The
                                           internal topics will be listed by
                                           default
--force                                  Suppress console prompts
--help                                   Print usage information.
--if-exists                              if set when altering or deleting or
                                           describing topics, the action will
                                           only execute if the topic exists.
                                           Not supported with the --bootstrap-
                                           server option.
--if-not-exists                          if set when creating topics, the
                                           action will only execute if the
                                           topic does not already exist. Not
                                           supported with the --bootstrap-
                                           server option.
--list                                   List all available topics.
--partitions <Integer: # of partitions>  The number of partitions for the topic
                                           being created or altered (WARNING:
                                           If partitions are increased for a
                                           topic that has a key, the partition
                                           logic or ordering of the messages
                                           will be affected
--replica-assignment <String:            A list of manual partition-to-broker
  broker_id_for_part1_replica1 :           assignments for the topic being
  broker_id_for_part1_replica2 ,           created or altered.
  broker_id_for_part2_replica1 :
  broker_id_for_part2_replica2 , ...>
--replication-factor <Integer:           The replication factor for each
  replication factor>                      partition in the topic being created.
--topic <String: topic>                  The topic to create, alter, describe
                                           or delete. It also accepts a regular
                                           expression, except for --create
                                           option. Put topic name in double
                                           quotes and use the '\' prefix to
                                           escape regular expression symbols; e.
                                           g. "test\.topic".
--topics-with-overrides                  if set when describing topics, only
                                           show topics that have overridden
                                           configs
--unavailable-partitions                 if set when describing topics, only
                                           show partitions whose leader is not
                                           available
--under-min-isr-partitions               if set when describing topics, only
                                           show partitions whose isr count is
                                           less than the configured minimum.
                                           Not supported with the --zookeeper
                                           option.
--under-replicated-partitions            if set when describing topics, only
                                           show under replicated partitions
--version                                Display Kafka version.
--zookeeper <String: hosts>              DEPRECATED, The connection string for
                                           the zookeeper connection in the form
                                           host:port. Multiple hosts can be
                                           given to allow fail-over.

上面比较常用的有 --bootstrap-server(连接的Kafka Broker主机名称和端口号),–topic(操作的topic名称), --create(创建主题), --delete(删除主题), --alter(修改主题), --list(查看所有主题), --describle(查看主题详细描述)

查看当前主机的所有主题

防止kafka宕机可以使用两台
[root@hadoop102 ~]# /opt/module/kafka/bin/kafka-topics.sh --bootstrap-server hadoop102:9092,hadoop103:9092 --list
测试一般使用一台即可
[root@hadoop102 ~]# /opt/module/kafka/bin/kafka-topics.sh --bootstrap-server hadoop102:9092 --list

如果产生下图的报错,说明Kafka服务没有启动,需要检查Kafka服务,此处注意要先启动ZK,然后在启动Kafka服务。
在这里插入图片描述

创建主题

  • 创建一个first主题并指定分区为1,分区副本3份。

      [root@hadoop102 ~]# /opt/module/kafka/bin/kafka-topics.sh --bootstrap-server hadoop102:9092 --topic first --create --partitions 1 --replication-factor 3 
    
  • 查看所有的主题

     [root@hadoop102 ~]# /opt/module/kafka/bin/kafka-topics.sh --bootstrap-server hadoop102:9092 --list
    
  • 查看指定主题的详细信息

      [root@hadoop102 ~]# /opt/module/kafka/bin/kafka-topics.sh --bootstrap-server hadoop102:9092 --topic first --describe
    

修改分区数

注意: 分区数只能增加,不能减少

[root@hadoop102 ~]# /opt/module/kafka/bin/kafka-topics.sh --bootstrap-server hadoop102:9092 --alter --topic first --partitions 3

查看当前分区详情
在这里插入图片描述

删除主题

[root@hadoop102 ~]# /opt/module/kafka/bin/kafka-topics.sh --bootstrap-server hadoop102:9092 --delete --topic first

生产者与消费者命令操作

查看命令参数

  • 查看生产者命令参数(/kafka-console-producer.sh)
参数描述
–bootstrap-server <String: server toconnect to>连接的 Kafka Broker 主机名称和端口号
–topic <String: topic>操作的 topic 名称
  • 查看消费者命令参数(kafka-console-consumer.sh)
参数描述
–bootstrap-server <String: server toconnect to>连接的 Kafka Broker 主机名称和端口号
–topic <String: topic>操作的 topic 名称
–from-beginning从头开始消费
–group <String: consumer group id>指定消费者组名称

发送消息与消费消息

使用hadoop102生产消息,hadoop103消费消息。

  • 生产消息
    一次只能发送一条,多发送会报错,等消费者消费完可以发送之后的消息。

      [root@hadoop102 kafka]# bin/kafka-console-producer.sh --bootstrap-server hadoop102:9092 --topic first
    

在这里插入图片描述

开启后需要等待,等待消费者开启

  • 消费消息
    消费 first 主题中的数据

注意:新创建消费者无法消费之前的历史记录,只能消费生产者新产生的记录

[root@hadoop103 kafka]# bin/kafka-console-consumer.sh --bootstrap-server hadoop102:9092 --topic first
  • 测试
    生产者一条消息
    在这里插入图片描述

    消费一条消息
    在这里插入图片描述

  • 查询消费者消费记录

把主题中所有的数据都读取出来
[root@hadoop102 kafka]# bin/kafka-console-consumer.sh --bootstrap-server hadoop102:9092 --from-beginning --topic first

  • 查询脚本
    下面提供自己写的一个脚本,方便管理维护相关配置,读者可以根据自己需要进行修改
#!/bin/bash

CMD=$1
TOPIC=$2

if [ $CMD == "start" ]; then
        /root/services/kafka_2.11/bin/kafka-server-start.sh -daemon /root/services/kafka_2.11/config/server.properties
elif [ $CMD == "stop" ]; then
        /root/services/kafka_2.11/bin/kafka-server-stop.sh
elif [ $CMD == "init" ]; then
        /root/services/kafka_2.11/bin/init_kafka.sh
elif [ $CMD == "topic" ]; then
        /root/services/kafka_2.11/bin/kafka-topics.sh --bootstrap-server 172.17.0.3:9092 --list
elif [ $CMD == "producer" ]; then
        /root/services/kafka_2.11/bin/kafka-console-producer.sh --broker-list 172.17.0.3:9092 --topic $2
elif [ $CMD == "consumer" ]; then
        /root/services/kafka_2.11/bin/kafka-console-consumer.sh --bootstrap-server 172.17.0.3:9092 --from-beginning --topic $2
else
        echo "Unknown command"
        exit
fi
<think>好的,用户现在问的是如何下载Kafka命令行工具。之前他询问的是如何在Mac版Offset Explorer中查看JSON格式的Kafka数据,现在转向了Kafka命令行工具,可能他需要更底层的操作或者自动化脚本处理。 首先,我需要确定用户的使用场景。他可能已经使用了图形化工具Offset Explorer,但遇到了一些限制,比如需要批量处理、自动化任务,或者想要更灵活地操作Kafka,所以转向命令行工具。用户可能是开发者或运维人员,需要直接与Kafka集群进行交互,进行生产消费测试、管理topic等操作。 接下来,用户的身份可能是中级用户,对Kafka有一定了解,但可能不熟悉如何获取和配置命令行工具。他可能已经知道Kafka的基本概念,但需要指导如何下载和设置环境。 真实需求是获取Kafka命令行工具,可能包括kafka-topics、kafka-console-producer等脚本。深层需求可能还包括如何配置环境变量,或者在不同操作系统下的安装方法。用户可能希望有一个简单直接的步骤,不需要复杂的配置。 我需要考虑如何回答这个问题。通常,Kafka命令行工具是包含在Kafka的官方发行包中的,所以下载Kafka即可获得这些工具。需要告诉用户从Apache官网下载,然后解压,配置环境变量。可能还需要提到使用包管理器如Homebrew(Mac)或APT(Linux)的方法,以及Docker方式,这样用户可以根据自己的情况选择最方便的安装方式。 另外,用户可能遇到的问题包括版本选择、环境变量配置是否正确,或者权限问题。需要提醒用户注意下载对应版本的Kafka,并确保Java环境已安装,因为Kafka依赖Java。 最后,给出验证安装的方法,比如运行kafka-topics.sh --version,确保用户确认安装成功。同时,提醒常用命令,帮助用户快速开始使用。</think>以下是Kafka命令行工具的下载与使用方法详解: --- ### **一、命令行工具来源** Kafka命令行工具(如`kafka-topics.sh`、`kafka-console-producer.sh`等)**集成在Kafka官方发行包中**,无需单独下载。您可通过以下方式获取: --- ### **二、下载方法** #### **方法1:官网直接下载(推荐)** 1. 访问Apache Kafka官网:[kafka.apache.org/downloads](https://kafka.apache.org/downloads) 2. 选择与服务器匹配的版本(建议选**Binary下载**,例如`kafka_2.13-3.6.1.tgz`) 3. 解压压缩包: ```bash tar -xzf kafka_2.13-3.6.1.tgz cd kafka_2.13-3.6.1/bin ``` #### **方法2:包管理器安装(Mac/Linux)** - **Mac (Homebrew)**: ```bash brew install kafka # 工具路径:/usr/local/Cellar/kafka/版本号/bin ``` - **Linux (APT)**: ```bash sudo apt-get install kafka ``` #### **方法3:Docker快速获取** ```bash docker run -it --rm wurstmeister/kafka /opt/kafka/bin/kafka-topics.sh --version ``` --- ### **三、环境准备** 1. **安装Java**(Kafka依赖Java 8+): ```bash java -version # 验证是否已安装 ``` 2. **配置环境变量**(可选): ```bash export KAFKA_HOME=/path/to/kafka export PATH=$PATH:$KAFKA_HOME/bin ``` --- ### **四、验证安装** ```bash # 查看工具列表 ls $KAFKA_HOME/bin/*.sh # 检查版本 kafka-topics.sh --version ``` --- ### **五、常用工具速查** | 工具名称 | 功能描述 | 示例命令片段 | |---------------------------|----------------------------|------------------------------| | `kafka-topics.sh` | Topic管理 | `--create --topic test` | | `kafka-console-producer.sh` | 控制台生产者 | `--broker-list localhost:9092` | | `kafka-console-consumer.sh` | 控制台消费者 | `--bootstrap-server localhost:9092` | | `kafka-configs.sh` | 动态配置修改 | `--alter --entity-type topics` | --- ### **注意事项** 1. Windows用户需使用`.bat`后缀的批处理文件(如`kafka-topics.bat`) 2. 生产环境建议使用Kafka服务端**相同版本**的命令行工具 3. 通过`--help`参数查看具体用法: ```bash kafka-topics.sh --help ``` 建议从官网下载完整包,可获得最完整的工具集。如果仅需轻量级工具,可考虑使用第三方封装工具(如`kcat`),但功能会有所限制。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大张哥儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值