C# 模型可以序列化为 AVRO JSON 模式吗?

joe*_*net 6 c# avro confluent-schema-registry

我在这里找到了一些代码https://docs.microsoft.com/en-us/azure/hdinsight/hdinsight-dotnet-avro-serialization#Scenario2与我需要的相反

//Define the schema in JSON
const string Schema = @"{
    ""type"":""record"",
    ""name"":""Microsoft.Hadoop.Avro.Specifications.SensorData"",
    ""fields"":
        [
            {
                ""name"":""Location"",
                ""type"":
                    {
                        ""type"":""record"",
                        ""name"":""Microsoft.Hadoop.Avro.Specifications.Location"",
                        ""fields"":
                            [
                                { ""name"":""Floor"", ""type"":""int"" },
                                { ""name"":""Room"", ""type"":""int"" }
                            ]
                    }
            },
            { ""name"":""Value"", ""type"":""bytes"" }
        ]
}";

//Create a generic serializer based on the schema
var serializer = AvroSerializer.CreateGeneric(Schema);
Run Code Online (Sandbox Code Playgroud)

我想采用我创建的模型:

[DataContract(Name = "Demo", Namespace = "pubsub.demo")]
public class Demo
{
    [DataMember(Name = "value")]
    public long Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

...并将此 C# 模型序列化为 JSON AVRO 架构字符串。

原因:

我只想维护 C# 模型并使用 Confluent 的 Schema Registry 自动注册这些模型。要向架构注册表注册,架构需要采用 JSON AVRO 格式(就像Schema上面一样)。

我不想同时定义 JSON 和 C# 模型。如果我必须维护一个,我宁愿拥有一个 C# 模型。

joe*_*net 6

我在Microsoft.Hadoop.Avro.AvroSerializer.

AvroSerializer.Create<Demo>().WriterSchema.ToString();
// > {"type":"record","name":"pubsub.demo.Demo","fields"[{"name":"value","type":"long"}]}
Run Code Online (Sandbox Code Playgroud)

  • MS似乎不再支持这个库https://github.com/Azure/azure-sdk-for-net/issues/2321#issuecomment-505963259,nuget包也没有在几年内更新https:/ /www.nuget.org/packages?q=Microsoft.Hadoop.Avro (2认同)