Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a avro Schema from a python dict

Tags:

python

avro

I have a python dict that defines the properties of an avro schema:


schema_dict = {
    'type': 'record',
    'name': 'the_name',
    'fields': [{'name':'id', 'type':'string'}, {'name':'value', 'type':'integer'}]
}

How do I create an instance of an avro.schema.Schema from this?

The examples use avro.schema.parse which assumes that the schema is defined as aJSON format string. I don't have that. I could go through the rigamarole of writing the dict to JSON and parsing it, but is there a more direct way of constructing the schema from this data?

like image 487
Dave Avatar asked Nov 26 '25 12:11

Dave


1 Answers

If you already have it in a dictionary format there a make_avsc_object (which is internally called by parse after it does a json.loads()).

So you would just do the following:

from avro.schema import make_avsc_object
parsed_schema = make_avsc_object(schema_dict)
like image 176
Scott Avatar answered Nov 29 '25 01:11

Scott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!