7

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.

4

2 回答 2

7

好的,事实证明这确实是为 avro IDL 中的枚举指定默认值的正确方法。在我的情况下,联合 {null, string} 已被枚举替换,导致所有麻烦。所以请记住:不要更改 avro 中字段的类型

于 2013-07-26T07:58:01.940 回答
0

我不知道 avro IDL 但如果您确定“记录”语法,您可以使用此代码将您的字符串转换为您的枚举类型

MyEnum e = Enum.valueOf(MyEnum.class, "UNSPECIFIED")
于 2013-07-25T10:06:19.993 回答