Prerequisites:
Java Installation:
Apache Kafka requires Java to be installed. Make sure you have Java installed on your machine. You can download Java from Oracle's website or use OpenJDK.
Kafka Installation Steps:
Download Apache Kafka:
Visit the Apache Kafka downloads page and download the latest stable version.
Extract the Archive:
Extract the downloaded Kafka archive to a directory of your choice.
tar -xzf kafka_2.x-x.x.x.tgz
cd kafka_2.x-x.x.x
Start the Zookeeper Server:
Kafka uses Zookeeper for distributed coordination. Start Zookeeper first.
bin/zookeeper-server-start.sh config/zookeeper.properties
Modify Kafka Configuration (if needed):
Kafka configuration is usually present in the config/server.properties file. You might need to adjust configuration settings based on your requirements.
nano config/server.properties
Start Kafka Broker:
Open a new terminal and start the Kafka broker.
bin/kafka-server-start.sh config/server.properties
Create a Topic:
Open another terminal and create a Kafka topic. Replace "my_topic" with the desired topic name.
bin/kafka-topics.sh --create --topic my_topic --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1
Produce and Consume Messages:
Open two separate terminals to run a producer and a consumer.
Producer:
bin/kafka-console-producer.sh --topic my_topic --bootstrap-server localhost:9092
Consumer:
bin/kafka-console-consumer.sh --topic my_topic --bootstrap-server localhost:9092 --from-beginning
You can now produce and consume messages in the respective terminals.
Cleanup:
Stop Kafka and Zookeeper:
To stop Kafka and Zookeeper, go to their respective terminals and press Ctrl+C.
Note:
These steps are for a single-node Kafka setup. In a production environment or for more advanced scenarios, you may need a multi-node setup, additional configurations, and adjustments to Zookeeper and Kafka configurations.
Make sure to consult the official Apache Kafka documentation for more details and specific configurations based on your use case.