Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define byte[] and LocalDateTime in avro schema?

Im new to Avro schema. I try to publish/consumer my java objects using kafka. I have java bean classes, which contains fields with LocalDateTime and byte[] . How can i define both in avro schema primitive types? What is the best primitive type i can use for LocalDateTime?

private LocalDateTime timestamp; 
private byte[] content; 

I defined something like this; but getting

 {
            "name": "content", "type": "bytes"
         },

Class cast exception[1]

[1] Caused by: java.lang.ClassCastException: [B cannot be cast to java.nio.ByteBuffer at org.apache.avro.generic.GenericDatumWriter.writeBytes(GenericDatumWriter.java:219) at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:77) at org.apache.avro.generic.GenericDatumWriter.writeField(GenericDatumWriter.java:114) at org.apache.avro.generic.GenericDatumWriter.writeRecord(GenericDatumWriter.java:104) at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:66) at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:58)

like image 994
Ratha Avatar asked Apr 14 '16 05:04

Ratha


1 Answers

For the byte[] you can use the bytes primitive as miguno said. For the LocalDateTime object you can store it as a long primitive, by converting it to milliseconds. Avro also supports Logical Types that you can use to directly deserialize into something that is not part of the primitive types. See here for more details and an example similar to what you want to achieve.

like image 139
cpard Avatar answered Oct 04 '22 19:10

cpard