Chapter 2
第 2 章
Kafka Console Tool: - Kafka offers command-line tools to manage topics, consumer groups, to consume and publish Messages and so forth.
Kafka 控制台工具:- Kafka 提供命令行工具来管理主题、消费者组,以及消费和发布消息等。
Kafka console scripts are different for Unix-based and Windows platforms.
Kafka 控制台脚本在基于 Unix 和 Windows 的平台上是不同的。
• Apache Kafka uses zookeeper to store metadata about the Kafka cluster.
• Apache Kafka 使用 Zookeeper 来存储关于 Kafka 集群的元数据。
A Kafka broker receives messages from producers and stores them on disk keyed by unique offset.
Kafka 代理(Broker)接收来自生产者的消息,并以唯一的偏移量(offset)为键将其存储在磁盘上。
Every Kafka broker must have an integer identifier, which is set using the broker.id configuration in server.properties.
每个 Kafka 代理必须有一个整数标识符,该标识符在 server.properties 中使用 broker.id 配置进行设置。
num.partitions parameter determines how many partitions a new topic is created with, primarily when automatic topic creation is enabled (which is the default setting).
num.partitions 参数决定了新主题创建时的分区数量,主要用于启用了自动创建主题的情况(这是默认设置)。
Kafka-console-consumer is a consumer command line that: read data from a Kafka topic.
Kafka-console-consumer 是一个消费者命令行工具,用于:从 Kafka 主题读取数据。
When using kafka-console-consumer.sh command, --group option can be used to specify the consumer group ID.
当使用 kafka-console-consumer.sh 命令时,--group 选项可用于指定消费者组 ID。
A Kafka broker receives messages from producer and stores them on disk.
Kafka 代理接收来自生产者的消息并将其存储在磁盘上。
In a kafka cluster, each Kafka broker must have an unique Integer identifier called broker.id.
在 Kafka 集群中,每个 Kafka 代理必须有一个唯一的整数标识符,称为 broker.id。
The total number of bytes of messages retained value is set using the log.retention.bytes parameter.
保留消息的总字节数值是使用 log.retention.bytes 参数设置的。
The metadata for a Kafka broker stores into zookeeper.
Kafka 代理的元数据存储在 Zookeeper 中。
Listeners property we can use in the server.properties file to change the port to the Kafka broker.
我们可以在 server.properties 文件中使用 Listeners 属性来更改 Kafka 代理的端口。
Create Topics.
创建主题。
- To Create Topics, we need to execute commands.
要创建主题,我们需要执行命令。
1 | kafka-topics.sh --bootstrap-server localhost:9092 --create --topic myTopic --partitions 1 |
- Now topic is created, we want to check how many topics we have.
现在主题已创建,我们要检查有多少个主题。
1 | kafka-topics.sh --bootstrap-server localhost:9092 -list |
- To see detail information of topics.
查看主题的详细信息。
1 | kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic myTopic |
- Delete a topic
删除一个主题
1 | kafka-topics.sh --bootstrap-server localhost:9092 --topic myTopic --delete |
Kafka Cli Producer
Kafka 命令行生产者
This tool is used to write messages to a topic. It is typically not as useful as the console consumer, but it can be useful when the messages are in a text-based format.
该工具用于向主题写入消息。它通常不如控制台消费者有用,但当消息是基于文本的格式时,它会很有用。
- Want to publish some message with the help of producer.
想要借助生产者发布一些消息。
1 | kafka-console-producer.sh --bootstrap-server localhost:9092 --topic myTopic |
- Publish message with the help of file.
借助文件发布消息。
1 | kafka-console-producer.sh --bootstrap-server localhost:9092 --topic myTopic < file.log |
Kafka Cli Consumer
Kafka 命令行消费者
- Want to subscribe message with the help of consumer.
想要借助消费者订阅消息。
1 | kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic myTopic --from-beginning |
Kafka Cli Consumer Group
Kafka 命令行消费者组
All consumers are part of consumer group. If we do not create group, it will create automatically.
所有消费者都是消费者组的一部分。如果我们不创建组,它会自动创建。
- List of all the consumers which are consuming messages.
列出所有正在消费消息的消费者。
1 | kafka-consumer-groups.sh --bootstrap-server localhost:9092 -list |
- To check the description about the consumers
查看关于消费者的描述信息
1 | kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group console-consumer-46555 |
- To create multiple producers on same node with same topic
在同一节点上为同一主题创建多个生产者
1 | kafka-console-producer.sh --bootstrap-server localhost:9092 --topic myTopic |
- To create multiple consumers on same node with same topic
在同一节点上为同一主题创建多个消费者
1 | kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic myTopic |
- check the consumer groups for all consumers using the following command
使用以下命令检查所有消费者的消费者组
1 | kafka-consumer-groups.sh --bootstrap-server localhost:9092 -list |
- create a consumer with pre-defined consumer group
创建一个具有预定义消费者组的消费者
1 | kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic myTopic --group myBDGroup |
- read messages from a specific partition
从特定分区读取消息
1 | kafka-console-consumer.sh --bootstrap-server niit01:9093 --topic b5-topic --partition 0 --from-beginning |
- Produce message with <Key, Value>
生产带有 <Key, Value>(键,值)的消息
1 | kafka-console-producer.sh --bootstrap-server niit01:9093 --property parse.key=true --property key.separator=, --topic test1 |
Eg.-key-1,niit
例如:key-1,niit
- Consumer Message with <Key, Value>
消费带有 <Key, Value>(键,值)的消息
1 | kafka-console-consumer.sh --bootstrap-server niit01:9092 --property print.key=true --property key.separator="~~" --topic B2-Topic |