可以使用包含枚举成员值的 Avro 模式吗?

huangapple go评论93阅读模式
英文:

Can I have an Avro Schema with an enum that includes values as enum members?

问题

这是我想转换为Avro模式的Java枚举:

public enum ApplicationCode {
    APP_A("MY-APP-A"),
    APP_B("MY-APP-B");

    private final String code;

    ApplicationCode(String code) {
        this.code = code;
    }

    public String getCode() {
        return code;
    }
}

由于枚举通常作为Avro中的类型可用,我提出了以下方案:

{
  "type": "enum",
  "name": "ApplicationCode",
  "namespace": "com.example",
  "symbols": ["APP_A", "APP_B"]
}

然后在我的主要Avro中引用它:

"fields": [
    {
      "name": "masterApplicationCode",
      "type": "ApplicationCode"
    }
]

这种方法可以正常工作,但不幸的是,使用这种方法会丢失应用程序代码(例如“MY-APP-A”)。我正在寻找一种方法,可以同时包含代码和标签,类似于以下内容:

{
  "type": "enum",
  "name": "ApplicationCode",
  "namespace": "com.example",
  "symbols": ["APP_A(MY-APP-A)", "APP_B(MY-APP-B)"]
}

是否有可能拥有这种复杂的枚举,或者是否有任何解决方法可以实现这一点?

英文:

This is the Java enum that I want to transform into an Avro Schema:

public enum ApplicationCode {
    APP_A("MY-APP-A"),
    APP_B("MY-APP-B");


    private final String code;

    ApplicationCode(String code) {
        this.code = code;
    }

    public String getCode() {
        return code;
    }
}

Since enums are generally available as Types in Avro, I came up with following:

{
  "type" : "enum",
  "name" : "ApplicationCode",
  "namespace" : "com.example",
  "symbols" : [ "APP_A", "APP_B" ]
}

And referenced it in my main Avro like this:

"fields": [
    {
      "name": "masterApplicationCode",
      "type": "ApplicationCode"
    }, 

It works like that but unfortunately I am losing the Application Codes (e.g. "MY-APP-A") using this approach. I'm looking for something, that allows me to include both, the code and the label. Something like

{
  "type" : "enum",
  "name" : "ApplicationCode",
  "namespace" : "com.example",
  "symbols" : [ "APP_A("MY-APP-A")", "APP_B("MY-APP-B")" ]
}

Is it even possible to have this kind of complex enums or is there any workaround to achieve this?

答案1

得分: 0

我相信 Avro 模式在内部将其转换为 JSON 字符串。因此,我认为问题更多地涉及枚举的序列化。请参考这里的内容 - https://www.baeldung.com/jackson-serialize-enums

我认为如果您使用类似这样的 JsonFormat 注释,它应该会返回代码 -

@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum ApplicationCode {

否则,您需要为枚举添加自定义序列化器。

英文:

I believe the avro schema is internally transforming it into a JSON String. So, I think the question is more about serializing enums. Reference from here - https://www.baeldung.com/jackson-serialize-enums

I think it should return the code if you use JsonFormat annotation like this -

@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum ApplicationCode {

Otherwise you will need to add a Custom Serializer for enum.

答案2

得分: 0

我通过编写自定义的序列化器/反序列化器来解决了我的问题,将具有复杂类型字段的对象映射到一个使用字符串而不是枚举发送的对象。以下是自定义序列化器的示例:

public class CustomSerializer implements Serializer<ApplicationObject> {

    @Override
    public byte[] serialize(String topic, ApplicationObject ApplicationObjectDto) {
        com.example.avro.ApplicationObject applicationObject = com.example.avro.ApplicationObject.newBuilder()
                .setApplicationCode(ApplicationObjectDto.getApplicationCode().getCode())
                .build();
        return SerializationUtils.serialize(applicationObject);
    }
}
英文:

I solved my problem by writing custom serializer / deserializer that map an object with complex typed fields to one that is being sent with e.g. Strings instead of enums.

Here an example of the custom serializer:

public class CustomSerializer implements Serializer&lt;ApplicationObject&gt; {

    @Override
    public byte[] serialize(String topic, ApplicationObject ApplicationObjectDto) {
        com.example.avro.ApplicationObject applicationObject = com.example.avro.ApplicationObject.newBuilder()
                .setApplicationCode(ApplicationObjectDto.getApplicationCode().getCode())
                .build();
        return SerializationUtils.serialize(applicationObject);
    }
}

huangapple
  • 本文由 发表于 2020年8月11日 19:55:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63357603.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定