apache avro-read二进制流

lfapxunr  于 2021-06-04  发布在  Kafka
关注(0)|答案(1)|浏览(259)

我试图建立一个Kafka消费者谁摄取avro流消息。这是我用来阅读avro消息文件并工作的资源:(https://avro.apache.org/docs/current/gettingstartedpython.html)

import avro.schema
from avro.datafile import DataFileReader, DataFileWriter
from avro.io import DatumReader, DatumWriter

reader = DataFileReader(open("weather.avro", "rb"), DatumReader(avro.schema.parse(open("mySchema.avsc").read())))
for user in reader:
    print (user)
reader.close()

但是,我的问题是,如何读取avro流而不是文件?
我尝试了类似的方法,但返回了错误的值。

def avro_decoder(msg_value):
    schema = avro.schema.parse(open("mySchema.avsc").read())
    reader = DatumReader(schema)
    message_bytes = io.BytesIO(msg_value)
    message_bytes.seek(5)
    decoder = BinaryDecoder(message_bytes)
    event_dict = reader.read(decoder)
    return event_dict

这是模式

schema = {
              "type": "record",
              "name": "Weather",
              "namespace": "test",
              "doc": "A weather reading.",
              "fields": [
                {
                  "name": "station",
                  "type": "string"
                },
                {
                  "name": "time",
                  "type": "long"
                },
                {
                  "name": "temp",
                  "type": "int"
                }
              ]
            }

这是avro的信息:

Objavro.codecnullavro.schemaú{"type": "record", "doc": "A weather reading.", "name": "test.Weather", "fields": [{"name": "station", "type": "string"}, {"name": "time", "type": "long"}, {"name": "temp", "type": "int"}]} ñKœV^¤‹ÿŸÕ>Z:v&011990-99999˜ÒïÖ
 ñKœV^¤‹ÿŸÕ>Z:v

有人能帮忙吗?

gmol1639

gmol16391#

我才知道问题出在哪里。所以问题是我发错了信息!我在.avro文件中发送了消息,但实际上,正确的消息必须是这样的:123111

相关问题