使用'SchemaRegistryClient'在Python中反序列化AVRO消息

时间:2020-08-01 18:05:05

标签: python schema avro

我们正在尝试使用来自其他系统的AVRO消息。 使用以下代码将架构指定为文件(.avsc)时,我能够读取AVRO消息,

import avro.schema
from avro.io import DatumReader, BinaryDecoder
...
schema = avro.schema.Parse(open("schema.avsc", "rb").read())
...
bytes_reader = io.BytesIO(element) # element is the serialized message
decoder = BinaryDecoder(bytes_reader)
reader = DatumReader(schema)
rec = reader.read(decoder)

但是,我现在需要改为从架构注册表URL中读取架构,

http://<IP>:<PORT>/subjects/<SUBJECT>/versions/<VERSION>/schema

我正在从传入消息自定义属性“模式”中提取URL。现在,我要使用以下代码从url获取架构,

def fetch_schema(IP, subject, version):
    sr = SchemaRegistryClient(IP)
    schema = sr.get_schema(subject, version=version).schema
    return schema

使用上面用于反序列化消息的相同代码,我现在得到以下错误

AttributeError: 'AvroSchema' object has no attribute 'type' 

在线

rec = reader.read(decoder) 

我已经比较了从文件读取时和从URL提取时的“模式”变量的类型,

from file, the schema type is : <class 'avro.schema.RecordSchema'>
from URL, the schema type is : <class 'schema_registry.client.schema.AvroSchema'>

它们是不同的,因此可能是问题所在。在这里寻找方向。谢谢!

0 个答案:

没有答案