To set up a Kafka consumer to receive data through the command-line interface (CLI), you can use the Kafka command-line tools provided by Apache Kafka. Here's a step-by-step guide.
- Install Kafka: If you haven't already installed Kafka, you can do so by downloading it from the Apache Kafka website and following the installation instructions for your operating system.
-
Start Kafka and Zookeeper:
Before setting up a consumer, you need to start Kafka and Zookeeper. Navigate to your Kafka installation directory and run Zookeeper and Kafka servers using the following commands.
bin/zookeeper-server-start.sh config/zookeeper.properties
bin/kafka-server-start.sh config/server.properties
-
Create a Kafka Topic:
If you haven't already created a Kafka topic, you can do so using the following command:
bin/kafka-topics.sh --create --topic my-topic --bootstrap-server localhost:9092
-
Start a Kafka Consumer:
Now, you can start a Kafka consumer to receive data from the specified topic. Use the following command:
bin/kafka-console-consumer.sh --topic my-topic --bootstrap-server localhost:9092
Replace
my-topic
with the name of the Kafka topic you want to consume messages from. The--bootstrap-server
option specifies the Kafka broker to connect to. By default, it'slocalhost:9092
. - Receive Data: Once the Kafka consumer is running, it will start receiving messages published to the specified topic. You can see the messages displayed in the terminal in real-time.
-
Interact with the Consumer:
-
To stop the consumer, press
Ctrl+C
. - You can specify additional options to customize the consumer behavior, such as setting a group ID, configuring message offset, or specifying a property file for consumer configuration.
-
To stop the consumer, press
That's it! You've set up a Kafka consumer to receive data through the command-line interface. You can now consume messages from Kafka topics and process them as needed.