Kafka How to connect kafka-console-consumer to fetch remote broker topic content?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
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.
Related reading
- Kafka How to consume data based on Timestamp
- Kafka how to consume one topic parallel
- Kafka How to delete records from a topic using Java API?
- Kafka How to Display Offsets
- Kafka How to get last modified time for a topic i.e. last message added to any partition of the topic
- Kafka how to read from __consumer_offsets topic
- Kafka How to retrieve a response from consumer?
- Kafka how to set producer retries to Infinity

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.