Exercise: Developing Kafka Producer
Procedure
- Read the javadoc of KafkaProducer to know how to use the Producer API (to send messages to Kafka)
- Create a new Scala/sbt project in IntelliJ IDEA
- Leave the defaults for the versions of sbt and Scala
- They usually are the latest versions
- Define dependency for Kafka Clients API library
- Use mvnrepository to know the proper entry for
kafka-clients
dependency
- Write the code of a Kafka producer
- Name of the object: KafkaProducerApp
- Start with an empty
Properties
object and fill out the missing properties per exceptions at runtime
- Use ProducerConfig constants (not string values for properties)
- Don’t forget to
close
the producer (so the messages are actually sent out to the broker)
- Run the producer
- Use
kafka-console-consumer
to receive the messages
- Fix the slf4j logging errors
- Define dependencies for
slf4j-api
and slf4j-log4j12
in build.sbt
- Create
src/main/resources/log4j.properties
as the configuration file
- Use Apache Kafka’s
config/log4j.properties
as the sample (4 first non-comment lines)
- Duration: 30 mins