This source demonstrates how to use a simple Avro logical type Conversion.
In this example we use
java.time.ZonedDateTime
with an underlying Avro string.
As an example, we use a simple Java class with an optional event name and a time:
public final class AvroEvent {
@org.apache.avro.reflect.Nullable
public String name;
public ZonedDateTime time;
}
Via
getSchema(AvroEvent.class)
we get the Avro schema in JSON format:
{
"type" : "record",
"name" : "AvroEvent",
"namespace" : "com.fillmore_labs.avro.logicaltypes",
"doc" : "Type documentation",
"fields" : [ {
"name" : "name",
"type" : [ "null", "string" ],
"default" : null
}, {
"name" : "time",
"type" : {
"type" : "string",
"logicalType" : "zoneddatetime-string"
}
} ]
}
and can now serialize and deserialize this class to Avro format.
bazel run //:main
in the resulting log we first see the schema, then the original, encoded and decoded value.
Since the encoded value is binary, non-ASCII characters are replaced by dots.
For a more elaborate example see Kafka Serialization Playground.