首页 > 解决方案 > 如何在 Windows 10 上使用 avro-python3 解析文件?

问题描述

我已经从 Microsoft Azure 下载了一个 AVRO 文件(带有 JSON 有效负载)到我的 Windows 10 计算机:

Azure 事件中心

然后通过 pip 安装 python 3.8.5 和 avro 1.10.0,我尝试运行以下脚本:

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

reader = DataFileReader(open("48.avro", "rb"), DatumReader())
for d in reader:
    print(d)
reader.close()

不幸的是,脚本没有打印任何内容。

然后我四处搜索并尝试添加如下模式:

schema_str = """
{
  "type" : "record",
  "name" : "EventData",
  "namespace" : "Microsoft.ServiceBus.Messaging",
  "fields" : [ {
    "name" : "SequenceNumber",
    "type" : "long"
  }, {
    "name" : "Offset",
    "type" : "string"
  }, {
    "name" : "EnqueuedTimeUtc",
    "type" : "string"
  }, {
    "name" : "SystemProperties",
    "type" : {
      "type" : "map",
      "values" : [ "long", "double", "string", "bytes" ]
    }
  }, {
    "name" : "Properties",
    "type" : {
      "type" : "map",
      "values" : [ "long", "double", "string", "bytes", "null" ]
    }
  }, {
    "name" : "Body",
    "type" : [ "null", "bytes" ]
  } ]
}
"""
schema = avro.schema.parse(schema_str)
reader = DataFileReader(open("48.avro", "rb"), DatumReader(schema, schema))
for d in reader:
    print(d)
reader.close()

但这没有帮助,仍然没有打印任何内容。

虽然我期待会打印一个字典对象列表......

更新:

我在邮件列表中收到了不推荐使用 avro-python3 的回复。

我对原始 avro 的问题仍然存在,没有打印任何内容。

更新 2:

我必须道歉 - 我使用的 avro 文件不包含任何有用的数据。我感到困惑的原因是一位同事在为我测试时使用了同名的不同文件。

现在我已经在不同的 avro 文件上尝试了 avro 和 fastavro 模块,并且都可以工作。我也会看看 PySpark。

标签: pythonpython-3.xavro

解决方案


正如 OneCricketeer 建议的那样,使用 PySpark 来读取 EventHub 生成的 avro 文件。在这里,PySpark:反序列化包含在 eventthub 捕获 avro 文件中的 Avro 序列化消息就是一个这样的例子。


推荐阅读