【问题标题】:Avro schema evolution in c#. Specify default value in datamemberc# 中的 Avro 模式演变。在数据成员中指定默认值
【发布时间】:2017-12-20 17:43:04
【问题描述】:

我目前正在使用 AvroDataContractResolver 将 POCO 从 C# 序列化到模式注册表中。我正在尝试改进模型以包含一个额外的字段,即枚举。当前模型如下所示。

[DataContract]
public class Person
{
    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public int Age { get; set; }
}

它的架构

{
"type": "record",
"name": "Person",
"namespace": "Core.Poco",
"fields": [
  {
    "name": "Name",
    "type": [
      "null",
      "string"
    ]
  },
  {
    "name": "Age",
    "type": "int"
  }
]
}

当我将枚举 Gender 添加到 POCO 的人时,我需要为枚举添加一个默认值,以使架构向后兼容。我正在查看https://github.com/welly87/Microsoft-Avro-Core 的代码,但我看不到任何为 avro 模式指定默认值的方法。有什么方法可以指定默认值还是我遗漏了什么?

【问题讨论】:

    标签: c# avro confluent-platform


    【解决方案1】:

    avro 规范https://avro.apache.org/docs/1.8.2/spec.html 使用默认关键字。假设微软遵循规范。否则从https://www.nuget.org/packages/blachniet.Avro/ 哪个dotnet 核心兼容fork https://github.com/blachniet/avro 下载nuget 包 例如

        {
    "type": "record",
    "name": "Person",
    "namespace": "Core.Poco",
    "fields": [
      {
        "name": "Name",
        "type": [
          "null",
          "string"
        ]
      },
      {
        "name": "Age",
        "type": "int"
      },
        "name": "Gender",
        "type": "int",
        "default" : null,
    ]
    }
    

    【讨论】:

      猜你喜欢
      • 2013-02-27
      • 2018-01-26
      • 1970-01-01
      • 2018-10-11
      • 2015-09-15
      • 1970-01-01
      • 2012-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多