Coding the validator
The next recipe involves the evolution from a simple Kafka producer-consumer to a Kafka message stream processor that includes validation and routing.
Getting ready
The execution of the previous recipes in this chapter are needed.
How to do it...
Good architecture implies flexibility. As shown in the Writting recipe, theWriter
class implements theProducer
interface. The idea is to start with thatWriter
and build a more sophisticated class with minimum effort. Let's recall the goals for ourValidator
:
- Read the Kafka messages from the source-messages topic
- Validate the messages, sending the defective messages to a different topic
- Write the good messages to the good-messages topic
For simplicity, the definition of a valid message is a message that is as follows:
- In JSON format
- Contains the four required fields:
event
,customer
,currency
, andtimestamp
If these conditions aren't met, a new error message in JSON format is generated, sending it to the bad events Kafka topic. The schema of...