我尝试在我的 Mac M1 笔记本电脑上安装 fastavro,但收到错误:
Headers -c fastavro/_read.c -o build/temp.macosx-10.9-universal2-cpython-39/fastavro/_read.o
fastavro/_read.c:6:10: fatal error: 'Python.h' file not found
#include "Python.h"
^~~~~~~~~~
1 error generated.
error: command '/usr/bin/clang' failed with exit code 1
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for fastavro
ERROR: Could not build wheels for fastavro, which is required to install pyproject.toml-based projects
Run Code Online (Sandbox Code Playgroud)
我的Python和系统信息是:
Python 3.9.6
virtualenv==20.16.6
% sw_vers
ProductName: macOS
ProductVersion: 12.6
BuildVersion: …Run Code Online (Sandbox Code Playgroud) 我正在构建一个从 Kafka 接收数据的应用程序。使用 Apache ( https://pypi.org/project/avro-python3/ )提供的标准 avro 库时,结果是正确的,但是,反序列化过程非常缓慢。
class KafkaReceiver:
data = {}
def __init__(self, bootstrap='192.168.1.111:9092'):
self.client = KafkaConsumer(
'topic',
bootstrap_servers=bootstrap,
client_id='app',
api_version=(0, 10, 1)
)
self.schema = avro.schema.parse(open("Schema.avsc", "rb").read())
self.reader = avro.io.DatumReader(self.schema)
def do(self):
for msg in self.client:
bytes_reader = io.BytesIO(msg.value)
decoder = BinaryDecoder(bytes_reader)
self.data = self.reader.read(decoder)
Run Code Online (Sandbox Code Playgroud)
在阅读为什么这么慢时,我发现fastavro哪个应该快得多。我是这样使用的:
def do(self):
schema = fastavro.schema.load_schema('Schema.avsc')
for msg in self.client:
bytes_reader = io.BytesIO(msg.value)
bytes_reader.seek(0)
for record in reader(bytes_reader, schema):
self.data = record
Run Code Online (Sandbox Code Playgroud)
而且,由于在使用 Apache 的库时一切正常,我希望一切都将与fastavro. 但是,在运行它时,我得到
File …Run Code Online (Sandbox Code Playgroud)