Apache Kafka Foundation Course - How to compile Kafka Code


Welcome back to Apache Kafka Tutorial. This video is an update for Apache Kafka 1.x. In all my tutorial videos, I am using SBT to compile and execute my examples. Many of my viewers are new to SBT. SBT is Scala build tool. However, It is equally useful for Java as well. I keep getting complains from many of my viewers about SBT installation.

Installing SBT for Kafka

In this video, I will show you step by step process to install, compile and execute one of my examples in GCP Kafka VM. So, let’s start.

  1. SSH to your Kafka VM.
  2. Check if you already have a Java compiler. Just type javac on the command prompt. You might see a message as command not found. You can fix this problem by installing open JDK.
  3. Use below command to install JDK.
  1. Next step is to install SBT. Start your browser and visit Scala SBT home page
  2. Navigate to SBT documentation and visit latest SBT documentation.
  3. Look for installing SBT on Linux. Your GCP Kafka machine is using Debian OS. So, we follow the installation procedure for Debian.
  4. First step is to setup SBT repository.
  1. Update the apt-get, and finally install SBT.

That's it. You are ready to start SBT for the first time. Create a working directory, change your working directory and start SBT by typing sbt command.


Compile and Execute Kafka Producer

Now let's compile and execute our first example. Follow these steps.

  1. Create a build.sbt file, and place following content in the file.
  1. Create a source code file and copy the below code in the file.

The source code is same as we used in one of my tutorial video. The code is also available in Learning Journal GitHub repository.

  1. You are now ready to compile our simple producer example. Execute the command as shown below to compile your program.
  1. You should see a success message in the end. You can execute your program using sbt run command.

Great. If you want to see the message, you can start a console consumer. Use below command to execute the consumer.

We sent just one message, so your consumer shows a single message.
Great. That's it for this session.
Thank you for watching learning journal. Keep learning and keep growing.


You will also like: