简体   繁体   中英

How to covert Simple Java POJO to avro schema .avsc file and then to autogenerated avro records to finally push it into Kafka topic?

I have Java POJO as this

class LibraryEvent {
   String name ;
   int id ;
   Book book;
 
}

class Book{
  String name;
  String author ;
}

How to covert it to avro schema and then to avro record programmatically? Is there a way to do this by having avro schema and class autogenerated in out folder using annotations? I'm trying to avoid generating schema as string and then filling avro record explicitly.

avoid generating schema as string

You could use SchemaBuilder to avoid that problem and give you a type-safe way to programmatically define a schema.


If there is not a specific reason to start with Java (which can be done with @AvroEncode annotation or ReflectData ), it is far easier to start with an IDL file

A direct translation of those POJO classes would look like this

protocol EventProtocol {

  record Book {
    union {null, string} name;
    union {null, string} author;
  }  
 
  record LibraryEvent {
    union {null, string} name;
    int id;
    union {null, Book} book;
  }
  
}

Then the Maven plugin mentioned in the docs would create those two classes with the AVSC schema embedded within each class as a static field.

You still need to "fill record explicitly" since this just creates the classes, doesn't call any setter methods


If you are still against being schema-first, and if you are using Kafka with the Confluent Avro Serializers, they have a section on using reflection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM