Publish and Subscribe with Kafka CLI

  1. Create Topic.
    • ./kafka-topics.sh --create --topic example-topic --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1
  2. Check is the topic created.
    • ./kafka-topics.sh --list --bootstrap-server localhost:9092
  3. In the second window, run the Console Consumer.
    • ./kafka-console-consumer.sh --topic example-topic --bootstrap-server localhost:9092
  4. Go back to the previous window and run the message producer.
    • ./kafka-console-producer.sh --topic example-topic --bootstrap-server localhost:9092
  5. Publish a few messages, confirming each of them with the Enter button.
  6. The messages should be logged in the terminal window launched in point 4.
  7. Close the Kafka Consumer process launched in point 4.
  8. Try to read the published messages again.
    • ./kafka-console-consumer.sh --topic example-topic --bootstrap-server localhost:9092
  9. After running the command, you will not see any result because the messages were published to the topic before the Consumer was connected. To read a topic from the beginning, you need to call the same command adding the --from-beginning parameter.
  10. Read all the additional message attributes, you need to add few parameters to the command from the previous point: --property print.offset=true --property print.partition=true --property print.headers=true --property print.timestamp=true --property print.key=true --property key.separator="-".