As I said in my last post I am looking to set up some Kafka infrastructure on my own terms and really get to know how it works.
So I decided to start at the “front” of a Kafka pipeline, by making a Producer of messages.
I used my Scalatra Starter Pack to create a quick, http mini-service that accepts JSON and publishes that input as Kafka messages.
I started checking out the Class ProducerRecord docs on the Apache site, and noticed that it is possible to publish Kafka records both with a key, and without a key. One use case for a key would be a unique ID, or some type of numbering system so consumers can organize the records. Useful, but not required.
So here’s a super basic Message class and Kafka Producer wrapper class written in Scala, that allows the user to send a message with a key or without:
An Scalatra endpoint could then be written like this:
and it could accept JSON in either format, not caring either way: