Generate Avro Schemas from a Python class
Project description
Dataclasses Avro Schema Generator
Generate Avro Schemas from a Python class
Requirements
python 3.7+
Installation
pip install dataclasses-avroschema
Documentation
https://marcosschroh.github.io/dataclasses-avroschema/
Usage
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"}
]
}'
and serialization
import typing
from dataclasses_avroschema.schema_generator import SchemaGenerator
@dataclass
class Address:
"An Address"
street: str
street_number: int
@dataclass
class User:
"User with multiple Address"
name: str
age: int
addresses: typing.List[Address]
address_data = {
"street": "test",
"street_number": 10,
}
# create an Address instance
address = Address(**address_data)
data_user = {
"name": "john",
"age": 20,
"addresses": [address],
}
# create an User instance
user = User(**data_user)
schema = SchemaGenerator(user)
schema.serialize()
# >>> b"\x08john(\x02\x08test\x14\x00"
schema.serialize(serialization_type="avro-json")
# >>> b'{"name": "john", "age": 20, "addresses": [{"street": "test", "street_number": 10}]}'
Features
- 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
- Instance serialization correspondent to
avro schema
generated - Data deserialization
Development
- Create a
virtualenv
:python3.7 -m venv venv && source venv/bin/activate
- Install requirements:
pip install -r requirements.txt
- Code linting:
./scripts/lint
- Run tests:
./scripts/test
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Close
Hashes for dataclasses-avroschema-0.11.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea4e85ca34823c0f7e15351a88cff6c881d9671d01960557d708f4a6cea75ba6 |
|
MD5 | 9b25e0bd34b2e975735f7a640110920c |
|
BLAKE2b-256 | b469bb60ed84185ef3f0e4a5a0844a2a042817cd7faccbaf8e5b37fc1cfeeb49 |