【发布时间】:2020-09-29 18:15:48
【问题描述】:
我已将 AVRO 文件(带有 JSON 有效负载)从 Microsoft Azure 下载到我的 Windows 10 计算机:
然后通过 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()
但这没有帮助,仍然没有打印任何内容。
虽然我期待会打印字典对象列表...
更新:
我在mailing list 上收到了 avro-python3 已被弃用的回复。
我的原始 avro 问题仍然存在,没有打印任何内容。
更新 2:
我必须道歉 - 我使用的 avro 文件不包含任何有用的数据。我感到困惑的原因是一位同事在为我测试时使用了同名的不同文件。
现在我已经在不同的 avro 文件上尝试了 avro 和 fastavro 模块,并且都可以正常工作。我也会看看 PySpark。
【问题讨论】:
-
fwiw,
fastavro应该会更好 -
你也可以使用 PySpark 直接从 Azure / WASB 读取
标签: python python-3.x avro