Generate Avro Schemas from a Python class
python 3.7+
pip install dataclasses-avroschema
https://marcosschroh.github.io/dataclasses-avroschema/
from dataclasses_avroschema.schema_generator import SchemaGenerator
class User:
"An User"
name: str
age: int
pets: typing.List[str]
accounts: typing.Dict[str, int]
favorite_colors: typing.Tuple[str] = ("BLUE", "YELLOW", "GREEN")
country: str = "Argentina"
address: str = None
SchemaGenerator(User).avro_schema()
'{
"type": "record",
"name": "User",
"doc": "An User",
"fields": [
{"name": "name", "type": "string"},
{"name": "age", "type": "int"},
{"name": "pets", "type": "array", "items": "string"},
{"name": "accounts", "type": "map", "values": "int"},
{"name": "favorite_colors", "type": "enum", "symbols": ["BLUE", "YELLOW", "GREEN"]},
{"name": "country", "type": ["string", "null"], "default": "Argentina"},
{"name": "address", "type": ["null", "string"], "default": "null"}
]
}'
- Primitive types: int, long, float, boolean, string and null support
- Complex types: enum, array, map, fixed, unions and records support
- Logical Types: date, time, datetime, uuid support
- Schema relations (oneToOne, oneToMany)
- Recursive Schemas
- Generate Avro Schemas from
faust.Record