Apache Kafka is a popular distributed streaming platform that is used for building real-time data pipelines and streaming applications. In this article, we will guide you through the process of installing Apache Kafka on Ubuntu 20.04.
Before we begin, make sure that you have a fresh installation of Ubuntu 20.04. You will also need to be logged in as a user with sudo
privileges.
Install Java
Apache Kafka is written in Java, so we need to install the Java Runtime Environment (JRE) on our system. To do this, run the following command in the terminal:
sudo apt-get update
sudo apt-get install default-jre
This will download and install the default JRE on your system. You can verify that Java is installed by running the following command:
java -version
This command should print the version of Java that is installed on your system.
Download Apache Kafka
Next, we need to download Apache Kafka. To do this, visit the Apache Kafka website and download the latest version of Apache Kafka.
Once the download is complete, extract the files from the Apache Kafka archive using the following command:
tar -xzf kafka_2.13-2.6.0.tgz
This will extract the files into a directory called "kafka_2.13-2.6.0".
Start Apache Kafka
Now we are ready to start Apache Kafka. To do this, open a terminal and navigate to the Apache Kafka directory using the following commands:
cd kafka_2.13-2.6.0
Next, start the Apache Kafka server using the following command:
bin/kafka-server-start.sh config/server.properties
This command will start the Apache Kafka server and print some log messages to the terminal.
Create a topic
Next, we need to create a topic in Apache Kafka. A topic is a named stream of data that is stored in Apache Kafka. To create a topic, run the following command in the terminal:
bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test
This command will create a topic called "test"
with a single partition and a replication factor of 1.
Send messages to the topic
Now that we have a topic, we can start sending messages to it. To do this, run the following command in the terminal:
bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic test
This will open a console where you can type messages and press Enter to send them to the "test"
topic.
Receive messages from the topic
To receive messages from the "test"
topic, open another terminal and run the following command:
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
This will open a console where you can see the messages that are being sent to the "test"
topic.
Congratulations! You have successfully installed Apache Kafka on Ubuntu 20.04 and sent and received some messages.