Kafka 命令行工具
掌握 Kafka 内置命令行工具的使用,用于主题管理、消费者组检查等操作
Kafka 命令行工具 是 CoddyKit 上的免费 Apache Kafka & Stream Processing Fundamentals 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Apache Kafka & Stream Processing Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Apache Kafka & Stream Processing Fundamentals 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Kafka CLI Tools: Your Admin Toolkit
Welcome! Kafka command-line interface (CLI) tools are essential for managing your Kafka cluster. They allow you to interact directly with brokers, topics, and consumer groups.
Mastering these tools is a crucial skill for any Kafka administrator or developer, enabling quick setup, debugging, and monitoring.
How to Use Kafka CLI Scripts
Kafka's CLI tools are shell scripts located in the bin/ directory of your Kafka installation. You typically execute them directly from your terminal.
Most commands follow a similar structure: bin/kafka-tool.sh --bootstrap-server <broker_address> --<action> [options].
Listing All Kafka Topics
The kafka-topics.sh tool is your primary utility for managing topics. Let's start by listing all topics currently available in your Kafka cluster.
The --list option does exactly that, showing you an overview of your data streams.
bin/kafka-topics.sh --bootstrap-server localhost:9092 --listCreating New Topics
To store data, you need to create a topic. When creating one, you must specify its name, the number of partitions, and the replication factor.
- Partitions: How many segments a topic is divided into. More partitions allow for greater parallelism.
- Replication Factor: The number of copies of each partition kept across different brokers for fault tolerance.
bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic my_first_topic --partitions 1 --replication-factor 1Inspecting Topic Details
Once a topic is created, you might want to check its configuration and status. The --describe option provides detailed information, including partition assignments, leader, and replica status.
bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic my_first_topicDeleting Topics
You can remove topics that are no longer needed. Be cautious, as deleting a topic will permanently remove all its data!
For topic deletion to work, ensure delete.topic.enable=true is set in your Kafka broker's configuration.
bin/kafka-topics.sh --bootstrap-server localhost:9092 --delete --topic my_first_topicSending Messages from Console
The kafka-console-producer.sh tool lets you send messages directly from your terminal. It's perfect for quick tests or manually injecting data into a topic.
After running the command, simply type your message and press Enter to send it.
bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic my_first_topicReceiving Messages from Console
To read messages from a topic, use kafka-console-consumer.sh. You can specify whether to read from the beginning of the topic or only new messages that arrive after the consumer starts.
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic my_first_topic --from-beginningManaging Consumer Groups
Consumers typically operate within a "consumer group" to enable distributed message processing. The kafka-consumer-groups.sh tool is vital for inspecting and managing these groups.
You can use it to list all active consumer groups and check their offsets.
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --listCLI Tool Quiz
You need to view the detailed configuration, including partitions, replication factor, leader, and replica assignments, for a Kafka topic named app_events. Which command-line option for kafka-topics.sh would you use?
Recap: Kafka CLI Power
You've learned how Kafka's command-line tools are indispensable for daily administration and quick interactions with your cluster. We covered:
- Listing, creating, describing, and deleting topics using
kafka-topics.sh. - Sending messages with
kafka-console-producer.sh. - Receiving messages with
kafka-console-consumer.sh. - Inspecting consumer groups with
kafka-consumer-groups.sh.
These powerful tools are your first line of interaction for managing and monitoring Kafka!
常见问题解答
「Kafka 命令行工具」课时是免费的吗?
是的 — 「Kafka 命令行工具」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Apache Kafka & Stream Processing Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Apache Kafka & Stream Processing Fundamentals 课程共包含 4 节课。
「Kafka 命令行工具」这节课中我会学到什么?
掌握 Kafka 内置命令行工具的使用,用于主题管理、消费者组检查等操作 你通过在浏览器中直接运行的动手代码来练习 Apache Kafka & Stream Processing Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Apache Kafka & Stream Processing Fundamentals 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Apache Kafka & Stream Processing Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「Kafka 命令行工具」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Apache Kafka & Stream Processing Fundamentals 课中编写并运行代码吗?
能。每节 Apache Kafka & Stream Processing Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Kafka 命令行工具
- 使用 JMX 和工具监控 Kafka
- 安全性:身份验证与授权
- 消费者滞后跟踪与告警