Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify a default value for an enum using avro IDL?

Tags:

java

enums

avro

idl

I haven't found anything in the documentation about this, only generic bla about default values. My assumption was that it should work like this:

enum MyEnum {
   UNSPECIFIED,
   SPECIFIED
}

record Test {
   MyEnum e = "UNSPECIFIED";
}

The GenericDatumReader in Java unfortunately complains that he is finding a String but expects a MyEnum.

Can anyone confirm that this is the correct way to use a enum with a default value using avro IDL? In that case I have a bug elsewhere. Can anyone confirm that this is not the way to do it and correct me? Any input is appreciated!

Update: In my real world version of this, it seems that a newly added enum to the record is causing the issue even though it has a default value. This means that my reader schema expects an enum, whereas the record does not contain one. Schema evolution should be able to resolve this, but seems to fail. More detail: I am working with Pig here, not direct Java.

like image 790
LiMuBei Avatar asked Jul 25 '13 09:07

LiMuBei


People also ask

Can an enum have a default value?

The default value for an enum is zero.

What is default in Avro schema?

In this case, the type of the key1 field is Union (type: [null, string] in Avro Schema). If the key1 field in the source data is not transferred or the transferred value is null, null is automatically filled as the default value.

What is IDL in Avro?

Usage. Each Avro IDL file defines a single Avro Protocol, and thus generates as its output a JSON-format Avro Protocol file with extension . avpr. To convert a . avdl file into a .


1 Answers

Ok, turns out this is indeed the correct way to specify a default value for an enum in an avro IDL. In my case a union {null, string} had been replaced by an enum causing all the trouble. So remember: do not change the type of a field in avro!

like image 93
LiMuBei Avatar answered Oct 21 '22 01:10

LiMuBei