如何根据现有的Avro schema生成样本数据?

问题描述 投票:0回答:1

我正在做Kafka的性能测试,需要测试不同的大型模式。目前,我正在研究基于 Avro 的负载测试。

通常,在使用 Kafka 时,您拥有数据并从中生成模式。我必须在这种情况下测试多个模式,而我不拥有这些模式的数据。我需要根据现有架构生成示例 Avro 数据。

有哪些可能的解决方案?

尝试过的解决方案:

  • 我尝试过手动制作数据,但太繁琐了。
  • 搜索自动发电机但没有成功
  • 搜索Kafka官方文档寻找可能的解决方案
  • 我尝试自己编写,但对 Avro 的经验很少,所以它看起来太定制且不可维护,无法继续

如何根据现有的Avro schema生成样本数据?

apache-kafka avro data-generation stub-data-generation
1个回答
0
投票

如果您熟悉 Python,

fastavro
库提供了从架构生成数据的实用程序:https://fastavro.readthedocs.io/en/latest/utils.html

举个例子:

from fastavro.utils import generate_many

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

print(list(generate_many(schema, 5)))
© www.soinside.com 2019 - 2024. All rights reserved.