Apache Kafka Message Producer API Tutorial - vulabvulab

Posted on 11 October 2014 by Srinivas Nelakuditi

Apache Kafka Message Producer API Tutorial

In this tutorial we will learn using Apache Kafka High Level Producer API to send messages using Java Program.

Please make sure you have completed Apache Kafka Installation tutorial before you start this tutorial.

Step 1: Install JDK

Make sure you have JDK 1.7 or JDK 1.8 installed on your machine. You can download and install JKD 1.8 from this link.

Step 2: Install Eclipse IDE

Please install Eclipse IDE for Java EE Developers from this link.

Download and install Eclipse IDE

 

Step 3: Create Maven Project with Eclipse

Please create a maven project in eclipse. File >> New >> Other >> Maven >> Maven Project. In the New Maven Project wizard select the check box for creating a simple project.

eclipse_create_maven_project

In the next screen populate the groupId and ArtificatId for your maven project.

eclipse_create_maven_project_2

 

Step 4: Configure Maven for Kafka in Pom.xml

Add the following dependency jar files to your Maven Pom.xml file

Step 5: Create Producer Code using Kafka API

code snippets as below. Please use both the classes below in the same package.

Step 6: Set Run Configurations to produce the messages

run_configurations_eclipse

 

Run the Java Program and you will see the following output on the console:

console

Step 7: Check via command line if messages are available on the topic.

 

Success. Let us create a Kafka Consumer to read messages in the next tutorial.

1 Comment

One Response to “Apache Kafka Message Producer API Tutorial”

  1. baba 9 February 2016 at 10:54 am Permalink

    Hello,

    I try to do the same code but it does’t work.
    I alwayes got :
    log4j:WARN No appenders could be found for logger (kafka.utils.VerifiableProperties).
    log4j:WARN Please initialize the log4j system properly.
    log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
    creating event 0
    Exception in thread “main” kafka.common.FailedToSendMessageException: Failed to send messages after 3 tries.
    at kafka.producer.async.DefaultEventHandler.handle(DefaultEventHandler.scala:90)
    at kafka.producer.Producer.send(Producer.scala:76)
    at kafka.javaapi.producer.Producer.send(Producer.scala:33)
    at com.vulab.kafka.training.VulabKafkaProducer.main(VulabKafkaProducer.java:27)

    Can you help me please.


Leave a Reply