I want to store a uuid using avro. below is the schema i am using
{
"namespace": "somethingImportant",
"type": "record",
"name": "GoodObject",
"fields": [
{
"name": "pk",
"type": {"type": "string", "logicalType": "uuid"}
}
]
}
i am using maven plugin 1.10.2 plugin with the below config
<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<version>1.10.2</version>
<executions>
<execution>
<id>schemas</id>
<phase>generate-sources</phase>
<goals>
<goal>schema</goal>
<goal>protocol</goal>
<goal>idl-protocol</goal>
</goals>
<configuration>
<stringType>String</stringType>
<customConversions>org.apache.avro.Conversions$UUIDConversion</customConversions>
<enableDecimalLogicalType>true</enableDecimalLogicalType>
<sourceDirectory>${project.basedir}/src/main/resources/</sourceDirectory>
<outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
the generated class the id field is generated as UUID, and i can do a get and set of uuid.
But uuid is stored as a string(36 bytes) which defeats the purpose. I came across another solution to define seomthing like
{
"namespace": "somethingImportant",
"type": "record",
"name": "GoodObject",
"fields": [
{
"name": "pk",
"type": {"type": "string", "logicalType": "uuid", "CustomEncoding":"UUIDAsBytesEncoding"}
}
]
}
but this does not do anything on its own. This stackoverflow answer talks about implementation, but i am not sure how we can make it work with maven plugin. This blog also talks about similar thing but i could not generate @AvroEncode(using = InstantAsStringAvroEncoding.class).
Has someone used uuid with avro so that
- we can set a uuid
- it gets stored as byte array
- The Get method returns a uuid
I don't want to do the marshelling and unmarshelling by myself obviously because that is a custom code to maintain