Best way to represent a UUID in Avro?
Asked Answered
W

3

10

I am curious to understand the best practice for encoding one very specific type of data within Avro: UUIDs.

Whopper answered 2/5, 2013 at 13:34 Comment(0)
N
8

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.

Nurmi answered 31/10, 2013 at 11:21 Comment(1)
Hello Marius. Could you post a small code example for the conversion?Mauri
C
11

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

Churl answered 31/10, 2018 at 13:45 Comment(3)
It's now mentioned in the docs avro.apache.org/docs/1.10.0/spec.html#UUIDVercelli
I think, it should be "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
@Francesco this is wrong. Not only it's 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
N
8

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.

Nurmi answered 31/10, 2013 at 11:21 Comment(1)
Hello Marius. Could you post a small code example for the conversion?Mauri
H
2

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;
Henghold answered 9/12, 2018 at 1:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.