2017-07-26 83 views
0

我已經發現了一些代碼在這裏https://docs.microsoft.com/en-us/azure/hdinsight/hdinsight-dotnet-avro-serialization#Scenario2,它的扭轉我需要什麼:我可以將C#模型序列化爲AVRO JSON模式嗎?

//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); 

我想利用我已經創建了一個模型:

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

...和將此C#模型序列化爲JSON AVRO架構字符串。

原因:

我只是想保持C#模型和自動會合的模式註冊登記這些模型。要註冊模式註冊表,模式需要採用JSON AVRO格式(就像上面的Schema一樣)。

我不希望既定義JSON也定義C#模型。如果我不得不主持一個,我寧願有一個C#模型。

回答

1

我在Microsoft.Hadoop.Avro.AvroSerializer找到了我要找的東西。

AvroSerializer.Create<Demo>().WriterSchema.ToString(); 
// > {"type":"record","name":"pubsub.demo.Demo","fields"[{"name":"value","type":"long"}]} 
相關問題