gpt4 book ai didi

hadoop - 测试 avro 架构 (.avsc) 并将其转换为 .avro。 AttributeError、数组和编码

转载 作者:可可西里 更新时间:2023-11-01 16:38:18 24 4
gpt4 key购买 nike

我刚刚开始使用 hadoop,我正在使用 Avro (fastavro)。

1- 我想验证架构并转换为 .avro 文件。

{
"type": "record",
"name": "Node",
"fields": [
{
"name": "nom",
"type": "string"
},
{
"name": "zone",
"type": {
"type": "map",
"values": "string"
}
},
{
"name": "price",
"type": "float"
},
{
"name": "type",
"type": "string"
}
]
}

我的测试文件(验证模式):

#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import json
import fastavro

schema = json.load(open("myschema.avsc"))
records = [
{
"nom": "blabla",
"zone": ["north", "south", "east"],
"prix": 4.0,
"type": "geoloc"
}
]

fastavro.writer(open("myschema.avro", "wb"), schema, records)

我有这个错误:

Traceback (most recent call last):
File "test-schema.py", line 17, in <module>
fastavro.writer(open("myschema.avro", "wb"), schema, records)
File "/var/www/data-machine/HDFS/env/lib/python3.5/site-packages/fastavro/writer.py", line 614, in writer
output.write(record)
File "/var/www/data-machine/HDFS/env/lib/python3.5/site-packages/fastavro/writer.py", line 537, in write
write_data(self.io, record, self.schema)
File "/var/www/data-machine/HDFS/env/lib/python3.5/site-packages/fastavro/writer.py", line 432, in write_data
return fn(fo, datum, schema)
File "/var/www/data-machine/HDFS/env/lib/python3.5/site-packages/fastavro/writer.py", line 363, in write_record
name, field.get('default')), field['type'])
File "/var/www/data-machine/HDFS/env/lib/python3.5/site-packages/fastavro/writer.py", line 432, in write_data
return fn(fo, datum, schema)
File "/var/www/data-machine/HDFS/env/lib/python3.5/site-packages/fastavro/writer.py", line 232, in write_map
for key, val in iteritems(datum):
File "/var/www/data-machine/HDFS/env/lib/python3.5/site-packages/fastavro/six.py", line 27, in py3_iteritems
return obj.items()
AttributeError: 'list' object has no attribute 'items'

2- 而且,如果我添加一个数组:

{
"name": "ingredients",
"type": ["string"]
},

错误:

  File "/var/www/data-machine/HDFS/env/lib/python3.5/site-packages/fastavro/writer.py", line 345, in write_union
raise ValueError(msg)
ValueError: ["north", "south", "east"] (type <class 'list'>) do not match ['string']

最后,我想让“区域”字段可选...

谢谢 :)法布里斯

最佳答案

您的 map 记录信息有误。它期待类似的东西

"zone":{"key1":"val1","key2":"val2","key3":"val3"},

这是一张 map ,不是一个集合。如果你想要像你的例子一样的东西,你需要使用数组而不是 map

关于hadoop - 测试 avro 架构 (.avsc) 并将其转换为 .avro。 AttributeError、数组和编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47155464/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com