avro.net序列化程序忽略属性

4c8rllxm  于 2021-06-02  发布在  Hadoop
关注(0)|答案(1)|浏览(337)

我正在使用.net库进行avro我有c的下一个类#

namespace Test.Avro.Model
{
    [DataContract(Name = "SensorDataValue", Namespace = "Sensors")]
    public class TestNm
    {
        [DataMember(Name = "name")]
        public string name { get; set; }

        [DataMember(Name = "surname", IsRequired = true)] //test to see if IsRequired works
        public string surname { get; set; }

        [DataMember(Name = "country", IsRequired = false)]  //test to see if IsRequired works
        public string country { get; set; }   

    }
}

当我使用sequentialwriter作为avro文件编写类时,我使用对象容器文件进行序列化,并使用反射进行序列化(http://azure.microsoft.com/en-us/documentation/articles/hdinsight-dotnet-avro-serialization/ )我在avro文件中得到以下json

Objavro.codecdeflateavro.schemaÆ{
  "type": "record",
  "name": "Sensors.SensorDataValue",
  "fields": [
    {
      "name": "name",
      "type": "string"
    },
    {
      "name": "surname",
      "type": "string"
    },
    {
      "name": "country",
      "type": "string"
    }
  ]
} ...

我想要的是

Objavro.codecdeflateavro.schemaÆ{
  "type": "record",
  "name": "Sensors.SensorDataValue",
  "fields": [
    {
      "name": "name",
      "type": "string"
    },
    {
      "name": "surname",
      "type": [
               "string",
                "null"
              ]
    },
    {
      "name": "country",
      "type": [
               "string",
                "null"
              ]
    }
  ]
}...

非常感谢你的帮助。ps:我能找到的唯一相关信息是https://hadoopsdk.codeplex.com/workitem/53

piv4azn7

piv4azn71#

可以通过添加属性使属性为null [NullableSchema] (在avro库中定义)。这个 IsRequired 价值 DataMember 被avro库忽略。

相关问题