Reputation: 1461
I'm just starting hadoop, and I'm using Avro (fastavro).
1- I want to validate schema and convert to .avro file.
{
"type": "record",
"name": "Node",
"fields": [
{
"name": "nom",
"type": "string"
},
{
"name": "zone",
"type": {
"type": "map",
"values": "string"
}
},
{
"name": "price",
"type": "float"
},
{
"name": "type",
"type": "string"
}
]
}
My test file (validate schema) :
#!/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)
I've this error :
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- And, if I add an array :
{
"name": "ingredients",
"type": ["string"]
},
The error :
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']
And, finishing, I want to make the "zone" field optionnal...
Thanks :) Fabrice
Upvotes: 0
Views: 2381
Reputation: 7947
Your record information for the map is wrong. It is expecting something like
"zone":{"key1":"val1","key2":"val2","key3":"val3"},
it is a map, not a set. If you want something like your example, you will need to use an array instead of a map
Upvotes: 1