I have written the Kafka Avro Deserializer using the Avro schema file with generated Java source code. The requirement is not to use the POJO's. How can I make the below code not to use POJOs and generic schema transformation.
import java.util.Arrays;
import java.util.Map;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.Decoder;
import org.apache.avro.io.DecoderFactory;
import org.apache.kafka.common.errors.SerializationException;
import org.apache.kafka.common.serialization.Deserializer;
import com.example.org.model.Person;
public class AvroDeserializer implements Deserializer<GenericRecord> {
@Override
public void close() {
}
@Override
public void configure(Map<String, ?> configs, boolean isKey) {
}
@Override
public GenericRecord deserialize(String topic, byte[] data) {
try {
GenericRecord result = null;
if (data != null) {
DatumReader<Person> reader = new SpecificDatumReader<>
(Person.getSchema());
Decoder decoder = DecoderFactory.get().binaryDecoder(data, null);
result = (GenericRecord) reader.read(null, decoder);
}
return result;
} catch (Exception ex) {
throw new SerializationException(
"Can't deserialize data '" + Arrays.toString(data) + "' from topic '" + topic + "'", ex);
}
}
}
How do I make this code not to use the POJO's.