Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate Avro Schema from certain Java Object

Apache Avro provides a compact, fast, binary data format, rich data structure for serialization. However, it requires user to define a schema (in JSON) for object which need to be serialized.

In some case, this can not be possible (e.g: the class of that Java object has some members whose types are external java classes in external libraries). Hence, I wonder there is a tool can get the information from object's .class file and generate the Avro schema for that object (like Gson use object's .class information to convert certain object to JSON string).

like image 257
Richard Le Avatar asked Apr 09 '14 06:04

Richard Le


People also ask

Is Avro faster than JSON?

Apache Avro is especially useful while dealing with big data. It offers data serialization in binary as well as JSON format which can be used as per the use case. The Avro serialization process is faster, and it's space efficient as well.


1 Answers

Take a look at the Java reflection API.

Getting a schema looks like:

Schema schema = ReflectData.get().getSchema(T); 

See the example from Doug on another question for a working example.

Credits of this answer belong to Sean Busby.

like image 82
MoustafaAAtta Avatar answered Sep 23 '22 09:09

MoustafaAAtta