I'm trying to implement the outbox pattern, but I'm running into an issue with Avro and Json.
I have a java quarkus application with an avro schema containing a union of objects. What I want to do is:
- create the class object (message to be sent)
- save it to the database in a human readable format such as json
- then retrieve it from the database and send it to kafka
This is an anonymised example of my schema.:
{
"namespace": "com.acme.kafka",
"type": "record",
"name": "ValidatedUpdate",
"fields": [
{
"name": "update",
"type": {
"type": "record",
"name": "Update",
"fields": [
{
"name": "createDate",
"type": {
"type": "long",
"logicalType": "timestamp-millis"
}
},
{
"name": "details",
"type": [
{
"name": "OtherUpdateDetails",
"type": "record",
"fields": [
{
"name": "stuff",
"type": "string"
},
{
"name": "otherNumber",
"type": "int"
}
]
},
{
"name": "SpecialUpdateDetails",
"type": "record",
"fields": [
{
"name": "stuff",
"type": "string"
},
{
"name": "isYes",
"type": "boolean"
}
]
}
]
}
]
}
}
]
}
However when I read the json from the database, map it with jackson to the generated avro message object, all seems fine, but when I try to send it to kafka, it complains about not being able to send it on account of containing a union. What is the best way to fix this?
I've looked into jackson dataformat dependency, but I couldn't figure it out how to fix it. I've checked out other stackoverflow posts, but couldn't find exactly what I'm running into. I've tried to rebuild parts of the kafka message, but that didn't work either. Thank you all in advance for the help.
UPDATE: I've found a solution, based on: https://www.baeldung.com/java-apache-avro
From Avro Class to JSON
From JSON (String) to Avro Class
This works for me, but feel free to suggest more elegant solutions.
Imports