I am curious to understand the best practice for encoding one very specific type of data within Avro: UUIDs.
So far, the only way I found is defining a custom UUID:
{
"namespace" : "your.namespace",
"type" : "fixed",
"name" : "UUID",
"size" : 16
}
I'm using Scala, so I also defined an implicit conversion between java.util.UUID
and my custom UUID, so it's not that much of a hassle to use it.
Here's how I've been doing it:
{
"name": "user_id",
"type": "string",
"logicalType": "UUID"
}
At the time of writing the logicalType
for UUIDs is not documented but it is nonetheless supported, you can check the code here and verify so yourself: https://github.com/apache/avro/blob/branch-1.8/lang/java/avro/src/main/java/org/apache/avro/LogicalTypes.java#L71
And here the docs: https://avro.apache.org/docs/1.10.0/spec.html#UUID
"logicalType": "uuid"
with lowercase. The section of the documentation says UUID
, but then notes uuid
. Same for the Date
, which has an example using date
. It's also in the lowercase in the LogicalTypes.java
. –
Silverfish uuid
and not UUID, but also what you showed is element definition. Logical type goes into type definition. so you need to have it as: "type": {"type": "string", "logicalType": "uuid"}
what you have will probably work in c++, but will produce warning in java and will be ignored. (try it out). –
Igniter So far, the only way I found is defining a custom UUID:
{
"namespace" : "your.namespace",
"type" : "fixed",
"name" : "UUID",
"size" : 16
}
I'm using Scala, so I also defined an implicit conversion between java.util.UUID
and my custom UUID, so it's not that much of a hassle to use it.
I had to create a custom converter as I couldn't get the UUID logicalType working in my project on version 1.8. Only took 2 steps --
1) Created a new class as below -
public class UUIDCustomConverter extends CustomEncoding<UUID> {
@Override
protected void write(Object datum, Encoder out) throws IOException {
out.writeString(((UUID)datum).toString());
}
@Override
protected UUID read(Object reuse, Decoder in) throws IOException {
return UUID.fromString(in.readString());
}
}
2) Added the following annotation to the UUID property
//annotation for handling custom converter
@AvroEncode(using=UUIDCustomConverter.class)
protected UUID id;
© 2022 - 2024 — McMap. All rights reserved.