python-avro-json-serializer icon indicating copy to clipboard operation
python-avro-json-serializer copied to clipboard

Unable to run the examples

Open blueRegen opened this issue 4 years ago • 2 comments

@0x26res thank you for your guidance.

I am trying to run the sample code in the read me . Not able to. both code. (simple or "another"

import avro.io from avro import schema from avro.schema import SchemaFromJSONData as make_avsc_object from avro_json_serializer import AvroJsonSerializer

PS C:\a\Python\W\CSV> python dumpsa.py Traceback (most recent call last): File "dumpsa.py", line 44, in avro_schema = avro.schema.make_avsc_object(schema_dict, avro.schema.Names()) AttributeError: module 'avro.schema' has no attribute 'make_avsc_object'

PS C:\a\Python\W\CSV> python --version Python 3.8.6

blueRegen avatar Jan 08 '21 23:01 blueRegen

was it in examples? given how you imported

from avro.schema import SchemaFromJSONData as make_avsc_object

you should do

make_avsc_object(schema_dict, avro.schema.Names())

without avro.schema

romaninozemtsev avatar Jan 08 '21 23:01 romaninozemtsev

Still unable to run.

#########Code################

import avro.io from avro import schema from avro.schema import SchemaFromJSONData as make_avsc_object from avro_json_serializer import AvroJsonSerializer

need to serialize this data

data = { "ffloat": 1.0, "funion_null": None, "flong": 1, "fdouble": 2.0, "ffixed": "1234567890123456", "fint": 1, "fstring": "hi there", "frec": { "subfint": 2 } }

according to this schema:

schema_dict = { "fields": [{"name": "fint", "type": "int"}, {"name": "flong", "type": "long"}, {"name": "fstring", "type": "string"}, {"name": "ffixed", "size": 16, "type": {"name": "fixed_16", "size": 16, "type": "fixed"}}, {"name": "frec", "type": {"fields": [{"name": "subfint", "type": "int"}], "name": "Rec", "type": "record"}}, {"name": "funion_null", "type": ["int", "null"]}, {"name": "ffloat", "type": "float"}, {"name": "fdouble", "type": "double"}], "name": "all_field", "namespace": "com.some.thing", "type": "record" }

avro_schema = make_avsc_object(schema_dict, avro.schema.Names())

serializer = AvroJsonSerializer(avro_schema) json_str = serializer.to_json(data)

print (json_str)

################## ERROR ##################

PS C:\a\Python\ven\ven1\w\json> python jdumps1.py Traceback (most recent call last): File "jdumps1.py", line 47, in json_str = serializer.to_json(data) File "C:\a\Python\lib\site-packages\avro_json_serializer_init_.py", line 226, in to_json result = self.to_ordered_dict(datum) File "C:\a\Python\lib\site-packages\avro_json_serializer_init_.py", line 223, in to_ordered_dict return self._process_data(self.avro_schema, datum) File "C:\a\Python\lib\site-packages\avro_json_serializer_init.py", line 140, in _process_data raise AvroTypeException(schema, datum) avro.io.AvroTypeException: The datum {'ffloat': 1.0, 'funion_null': None, 'flong': 1, 'fdouble': 2.0, 'ffixed': '1234567890123456', 'fint': 1, 'fstring': 'hi there', 'frec': {'subfint': 2}} is not an example of the schema { "type": "record", "name": "all_field", "namespace": "com.some.thing", "fields": [ { "type": "int", "name": "fint" }, { "type": "long", "name": "flong" }, { "type": "string", "name": "fstring" }, { "size": 16, "type": { "type": "fixed", "name": "fixed_16", "namespace": "com.some.thing", "size": 16 }, "name": "ffixed" }, { "type": { "type": "record", "name": "Rec", "namespace": "com.some.thing", "fields": [ { "type": "int", "name": "subfint" } ] }, "name": "frec" }, { "type": [ "int", "null" ], "name": "funion_null" }, { "type": "float", "name": "ffloat" }, { "type": "double", "name": "fdouble" } ] }

blueRegen avatar Jan 24 '21 07:01 blueRegen