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
- Open a Command Line Interface (CLI): Navigate to the Kafka installation directory on your local machine where you can access the
kafka-console-consumerscript. - Run the Consumer: Use the following command structure to start consuming messages:
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:
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.propertieswith contents likebootstrap.servers=192.168.1.100:9092and 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
--formattercan 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
| Parameter | Description | Example |
--bootstrap-server | The Kafka broker's IP and port | 192.168.1.100:9092 |
--topic | Topic to consume messages from | logs |
--from-beginning | Consumes messages from the topic's start | Applicable as a flag |
--consumer.config | Optional properties file for configurations | consumer.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.

