当消息包含多个消息时编译avro架构(python)

时间:2012-01-30 22:41:29

标签: python avro

我使用import avro等来编译python中的avro架构。有时,我的avro架构看起来像:

{ name: "Message1" ..... }

{ name: "Message2", "fields": [ { "type": "Message1", "name": "previous_avro_schema" } ] } ...

请忽略错别字。我只是希望得到消息。要点是我有两个avro架构。其中一个avro架构使用第二个avro架构作为其字段之一。如何为这些avro消息调用avro.schema.parse(....)以便正确编译它们?

1 个答案:

答案 0 :(得分:0)

Avro的Python支持非常可怜,但您可以获得一些工作,包括同时使用多个模式。您只需将模式文件合并到一个文件中,并且需要确保它们以正确的顺序合并,因此依赖性首先出现,您需要将名称替换为实际模式这是我使用的脚本合并它们:

def resolve(path):
    "fully resolve a schema that includes other schemas"
    data = open(path).read()
    # fill in any while they remain
    while True:
        beg = data.find('`')
        end = data.find('`', beg + 1)
        if beg < 0:
            break
        path = os.path.join(os.path.dirname(path), data[beg+1:end] + '.avsc')
        data = data[:beg] + resolve(path) + data[end+1:]
    return data