简体   繁体   English

如何使用avro IDL为枚举指定默认值?

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

I haven't found anything in the documentation about this, only generic bla about default values. 我没有在文档中找到任何关于此的内容,只有关于默认值的泛型bla。 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. 不幸的是,Java中的GenericDatumReader抱怨他正在寻找一个字符串,但期望一个MyEnum。

Can anyone confirm that this is the correct way to use a enum with a default value using avro IDL? 任何人都可以确认这是使用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. 更多细节:我在这里与Pig合作,而不是直接Java。

Ok, turns out this is indeed the correct way to specify a default value for an enum in an avro IDL. 好的,事实证明这确实是在avro IDL中为枚举指定默认值的正确方法。 In my case a union {null, string} had been replaced by an enum causing all the trouble. 在我的例子中,union {null,string}已被枚举替换,导致所有麻烦。 So remember: do not change the type of a field in avro ! 所以请记住: 不要在avro中更改字段的类型

我不知道avro IDL,但如果您确定'record`语法,您可以使用此代码将您的String转换为您的枚举类型

MyEnum e = Enum.valueOf(MyEnum.class, "UNSPECIFIED")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM