最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to handle circular type references in Avro schemas? - Stack Overflow

programmeradmin3浏览0评论

I'm using Apache.Avro for .NET for serialization and am trying to parse .avsc schema files that contain circular references between 2 records. However, I can't figure out how I'm meant to parse the schemas since whichever one is parsed first will throw an exception since the other type is not defined yet. From what I can tell, support for circular references was added with the AVRO-695 request, and commited with AVRO-1692, so I believe this usecase should be supported.

The schemas I'm working with are structured like this:

Foo.avsc

{
  "name": "MyNamespace.Foo",
  "type": "record",
  "fields": [
    {
      "name": "Name",
      "type": "string"
    },
    {
      "name": "Bar",
      "type": "MyNamespace.Bar"
    }
  ]
}

Bar.avsc

{
  "name": "MyNamespace.Bar",
  "type": "record",
  "fields": [
    {
      "name": "Name",
      "type": "string"
    },
    {
      "name": "Foo",
      "type": "MyNamespace.Foo"
    }
  ]
}

I'm trying to parse them to create a union schema with both types defined:

var fooSchemaJson = File.ReadAllText("./Foo.avsc");
var barSchemaJson = File.ReadAllText("./Bar.avsc");

var fooSchema = Schema.Parse(fooSchemaJson);
var barSchema = Schema.Parse(barSchemaJson);

var combinedSchema = UnionSchema.Create(new List<Schema> { fooSchema, barSchema }, null);

This throws the below error:

Avro.SchemaParseException: Undefined name: MyNamespace.Bar at 'fields[1].type'
   at Avro.Schema.ParseJson(JToken jtok, SchemaNames names, String encspace)
   at Avro.RecordSchema.createField(JToken jfield, Int32 pos, SchemaNames names, String encspace)
   at Avro.RecordSchema.NewInstance(Type type, JToken jtok, PropertyMap props, SchemaNames names, String encspace)
   at Avro.NamedSchema.NewInstance(JObject jo, PropertyMap props, SchemaNames names, String encspace)
   at Avro.Schema.ParseJson(JToken jtok, SchemaNames names, String encspace)
   at Avro.Schema.Parse(String json, SchemaNames names, String encspace)
   at Avro.Schema.Parse(String json)

Is there another method I'm missing in which I can parse multiple schemas at once so all types will be found?

发布评论

评论列表(0)

  1. 暂无评论