In this section, you will learn how to consume messages from a Kafka topic using the kafka-console-consumer script. This script
allows you to read messages from a topic and display them on the terminal.
Before you start, you need to have the following:
Notice that the script does not exit after reading all the messages. It keeps running and waiting for more messages to arrive. As soon as a new message is sent to the topic, the consumer will read it and display it on the terminal. You can start more consumers and run the same command to consume messages from the same topic. Each consumer will read all messages from the topic, regardless of whether they have been consumed by other consumers or not. This is because Kafka does not delete messages from a topic after they are consumed. It keeps them in the topic for a configurable period of time, so that other consumers can read them as well. To stop the consumer script, press Ctrl+C on the terminal. This will terminate the script and disconnect from the Kafka cluster.
In this section, you will learn how to consume only new messages from a Kafka topic using the kafka-console-consumer script.
This means that you will not read any messages that were sent to the topic before you start the script.
To consume only new messages from a Kafka topic, follow these steps:
In this section, you will learn how to consume messages that are sent as key:value pairs from a Kafka topic. A key:value pair is a message that has two parts: a key and a value, separated by a delimiter. To consume key:value pair messages, you need to run the kafka-console-consumer script with the same parameters as for regular messages, plus two optional parameters to print the key and the value:
By default, the consumer will only print the value part of the message, not the key. You can change this by setting the print.key and print.value properties to true or false as you wish. The key:value pair messages are stored in the topic with the delimiter that you specified when you produced them. For example, if you used a colon (:) as the delimiter, the messages will be stored as key:value in the topic. The consumer will display the key and the value separated by a tab on the terminal. For example:
In this section, you will learn how to store and consume messages in Kafka topic, so that they are read in the same order they were sent. This is useful when you want to preserve the sequence of events or transactions in your messages.
To store messages in order, you need to use the same key for all the messages that belong to the same sequence. The key can be any string that identifies or groups the messages, such as a user ID or a product ID. Kafka will use the key to determine which partition to store the message in. Messages with the same key will be stored in the same partition and will be read in order.
To produce messages with a key, run the kafka-console-producer script with the following parameters:
For example, to produce messages with a key and a colon (:) as the separator to a topic called messages-order, run this command:
./kafka-console-producer.sh --bootstrap-server localhost:9092 --topic messages-order --property parse.key=true --property key.separator=:
Press Enter to run the command. You are ready to start sending messages. Type a message in the format key:value and press Enter to send it.
For example, to send a message with the key 1 and the value First message, type this:
Notice that you are using the same key for all the messages. This means that they will be stored in the same partition and will be read
in order. To test this, open another terminal window and run the kafka-console-consumer script to consume messages from the same topic.
For example, run this command:
./kafka-console-consumer.sh --topic messages-order --bootstrap-server localhost:9092 --from-beginning --property print.key=true --property print.value=true
Press Enter to run the command. You should see the messages from the topic displayed on the terminal, one per line, with the key and the value
separated by a tab. For example:
Notice that the messages are displayed in the same order they were sent. This is because they have the same key and are stored in the same partition. To stop the producer or the consumer script, press Ctrl+C on the terminal. This will terminate the script and disconnect from the Kafka cluster.