You will find a series of example about how to use dataclasses-avroschema and the integration with python drivers like aiokafka, kafka-python in the case of kafka
and walrus and redisgears-py in the case of redis
.
python 3.8+ docker docker-compose
python3.8 -m venv venv
source venv/bin/activate
- For kafka examples:
pip install -r kafka-examples/requirements.txt
- For redis examples:
pip install -r redis-examples/requirements.txt
Kafka:
cd kafka-examples
make kafka-cluster
to start the kafka cluster (kafka and zookeeper)- In the same path but in a different terminal execute
make aiokafka-example
in order to start the producer/consumer foraiokafka
driver (async) - To stop the producer/consumer use
Ctrl + c
Redis:
cd redis-examples
make redis-cluster
to start the redis cluster- In the same path but in a different terminal execute
make redis-stream-example
As examples you will find how to serialize/deserialize python dataclasses using the AvroModel
provided by dataclasses-avroschema
and how to produce/consume events using specifics python kafka drivers.
The model is for the examples is the following:
class FavoriteColor(enum.Enum):
BLUE = "BLUE"
YELLOW = "YELLOW"
GREEN = "GREEN"
@dataclass
class UserModel(AvroModel):
"An User"
name: str
age: int
favorite_colors: FavoriteColor = FavoriteColor.BLUE
country: str = "Argentina"
address: str = None
class Meta:
namespace = "User.v1"
aliases = ["user-v1", "super user"]
In the file kafka-examples/aiokafka_example.py you will find the simplest async
example where the model instance is serialize and send it throught kafka. A consumer receives the event and deserialized it returning a new model instance. Run it with make aiokafka-example
In the file kafka-examples/kafka_python_example.py you will find a a similar example as with aiokafka
but in this case synchronous
and using avro-json
serialization instead of avro
serialization. Run it with make kafka-python-example
In the file kafka-examples/schema_evolution_example.py you will find an example of how the schema evolution works. In this case we show the FULL
compatibility, user the previous model and performing the deserialization with the User.v2
which has an extra optional field called has_pets
. Run it with make schema-evolution-example
In the file redis-examples/redis_stream_example.py you will find a the simplest use case of redis streams
using the driver walrus
. We create a consumer group and messages are read one by one. Run it with make redis-stream-example
In the file redis-examples/redis_gears_example.py you will find an example of using streaming
with RedisGears
. As use case we produce records with random values and the consumer filters them by the age
property. Run it with make redis-gears-example
.