Kafka
Kafka-console-consumer
Remote Broker
Fetching Data
Topic Content

Kafka How to connect kafka-console-consumer to fetch remote broker topic content?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Apache Kafka is a distributed streaming platform capable of handling trillions of events a day. Initially conceived as a messaging queue, Kafka is based on an abstraction of a distributed commit log. Since being open-sourced by LinkedIn in 2011, Kafka has quickly evolved from messaging queue to a full-fledged event streaming platform.

Connecting Kafka Console Consumer to a Remote Broker

The kafka-console-consumer is a command-line tool that comes with Kafka and allows you to consume messages from Kafka topics. Connecting this tool to a remote Kafka broker involves specifying the broker's IP address and the topic you want to consume from.

Prerequisites

Before connecting the kafka-console-consumer to a remote broker, ensure the following:

  • Kafka and Zookeeper are properly installed and running on the remote broker.
  • The topic from which messages are to be consumed exists on the remote broker.
  • Network accessibility from the machine running the console consumer to the remote Kafka broker.

Steps to Connect

  1. Open a Command Line Interface (CLI): Navigate to the Kafka installation directory on your local machine where you can access the kafka-console-consumer script.
  2. Run the Consumer: Use the following command structure to start consuming messages:
 
   bin/kafka-console-consumer.sh --bootstrap-server <remote_broker_ip>:<port> --topic <topic_name> --from-beginning

Replace <remote_broker_ip> with the IP address of the Kafka broker, <port> with the port number (usually 9092 unless configured otherwise), and <topic_name> with the name of the topic.

Adding --from-beginning tells the consumer to read messages from the beginning of the topic. Omitting this option will only show new messages sent after the consumer starts.

Example

If your Kafka broker is running at IP 192.168.1.100 and the topic you want to consume from is named logs, the command would be:

 
bin/kafka-console-consumer.sh --bootstrap-server 192.168.1.100:9092 --topic logs --from-beginning

Security Considerations

If the Kafka broker is secured (which it should be in a production environment), additional parameters are needed such as --consumer.config pointing to a properties file containing the security settings like SSL/TLS configuration or SASL/PLAIN authentication details.

Enhancements and Utilities

  • Using Properties File: Instead of putting all configurations on the command line, you can use a properties file. For example, create a file consumer.properties with contents like bootstrap.servers=192.168.1.100:9092 and then use the file in the command with --consumer.config consumer.properties.
  • Filtering and Formatting Messages: Kafka console consumer allows for message formatting and key-value filters. Parameters like --formatter can customize the output format.
  • Integration with Monitoring Tools: Integrate Kafka consumer metrics (like lag, throughput, etc.) with monitoring tools such as Prometheus and Grafana for better visibility and alerts.

Summary Table

ParameterDescriptionExample
--bootstrap-serverThe Kafka broker's IP and port192.168.1.100:9092
--topicTopic to consume messages fromlogs
--from-beginningConsumes messages from the topic's startApplicable as a flag
--consumer.configOptional properties file for configurationsconsumer.properties

This approach allows you to flexibly and securely connect to and consume messages from a Kafka topic residing on a remote broker, providing powerful insights and data flows essential for modern distributed applications.


Course illustration
Course illustration

All Rights Reserved.