1

我的avsc文件如下:

{"type":"record",
    "namespace":"testing.avro",
    "name":"product",
    "aliases":["items","services","plans","deliverables"],
    "fields":
        [   
            {"name":"id", "type":"string" ,"aliases":["productid","itemid","item","product"]},
            {"name":"brand", "type":"string","doc":"The brand associated", "default":"-1"},
            {"name":"category","type":{"type":"map","values":"string"},"doc":"the list of categoryId, categoryName associated, send Id as key, name as value" },
            {"name":"keywords", "type":{"type":"array","items":"string"},"doc":"this helps in long run in long run analysis, send the search keywords used for product"},
            {"name":"groupid", "type":["string","null"],"doc":"Use this to represent or flag value of group to which it belong, e.g. it may be variation of same product"},
            {"name":"price", "type":"double","aliases":["cost","unitprice"]},
            {"name":"unit", "type":"string", "default":"Each"},
            {"name":"unittype", "type":"string","aliases":["UOM"], "default":"Each"},
            {"name":"url", "type":["string","null"],"doc":"URL of the product to return for more details on product, this will be used for event analysis. Provide full url"},
            {"name":"imageurl","type":["string","null"],"doc":"Image url to display for return values"},
            {"name":"updatedtime", "type":"string"},
            {"name":"currency","type":"string", "default":"INR"},
            {"name":"image", "type":["bytes","null"] , "doc":"fallback in case we cant provide the image url, use this judiciously and limit size"},
            {"name":"features","type":{"type":"map","values":"string"},"doc":"Pass your classification attributes as features in key-value pair"}

        ]}

我能够解析这个,但是当我尝试如下写这个时,我一直遇到问题。我错过了什么?这是在python3中。我验证它也是格式正确的 json。

from avro import schema as sc
from avro import datafile as df
from avro import io as avio
import os

_prodschema = 'product.avsc'
_namespace = 'testing.avro'

dirname =  os.path.dirname(__file__)
avroschemaname = os.path.join( os.path.dirname(__file__),_prodschema)
sch = {}

with open(avroschemaname,'r') as f:
    sch= f.read().encode(encoding='utf-8')
    f.close()


proschema = sc.Parse(sch)

print("Schema processed")
writer =  df.DataFileWriter(open(os.path.join(dirname,"products.json"),'wb'),
                             avio.DatumWriter(),proschema)
print("Just about to append the json")


writer.append({ "id":"23232",
            "brand":"Relaxo",
            "category":[{"123":"shoe","122":"accessories"}],
            "keywords":["relaxo","shoe"],
            "groupid":"",
            "price":"799.99",
            "unit":"Each",
            "unittype":"Each",
            "url":"",
            "imageurl":"",
            "updatedtime": "03/23/2017",
            "currency":"INR",
            "image":"",
            "features":[{"color":"black","size":"10","style":"contemperory"}]
            })
writer.close()

我在这里想念什么?

4

0 回答 0