-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: deserializer deprecation warning added. Examples with deserializ…
…ation updated to middlewares
- Loading branch information
1 parent
2a0732c
commit a072563
Showing
13 changed files
with
338 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
examples/confluent-example/confluent_example/middlewares.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from typing import Dict, Optional | ||
|
||
from schema_registry.client import AsyncSchemaRegistryClient, schema | ||
from schema_registry.serializers import AsyncAvroMessageSerializer | ||
|
||
from kstreams import ConsumerRecord, middleware | ||
|
||
|
||
class ConfluentMiddlewareDeserializer( | ||
middleware.BaseMiddleware, AsyncAvroMessageSerializer | ||
): | ||
def __init__( | ||
self, | ||
*, | ||
schema_registry_client: AsyncSchemaRegistryClient, | ||
reader_schema: Optional[schema.AvroSchema] = None, | ||
return_record_name: bool = False, | ||
**kwargs, | ||
): | ||
super().__init__(**kwargs) | ||
self.schemaregistry_client = schema_registry_client | ||
self.reader_schema = reader_schema | ||
self.return_record_name = return_record_name | ||
self.id_to_decoder_func: Dict = {} | ||
self.id_to_writers: Dict = {} | ||
|
||
async def __call__(self, cr: ConsumerRecord): | ||
""" | ||
Deserialize the event to a dict | ||
""" | ||
data = await self.decode_message(cr.value) | ||
cr.value = data | ||
return await self.next_call(cr) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 3 additions & 4 deletions
7
examples/confluent-example/confluent_example/streaming/engine.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
from schema_registry.client import AsyncSchemaRegistryClient | ||
|
||
from confluent_example import serializers | ||
from confluent_example.serializers import AvroSerializer | ||
from kstreams import create_engine | ||
|
||
client = AsyncSchemaRegistryClient("http://localhost:8081") | ||
schema_registry_client = AsyncSchemaRegistryClient("http://localhost:8081") | ||
|
||
stream_engine = create_engine( | ||
title="my-stream-engine", | ||
serializer=serializers.AvroSerializer(client), | ||
deserializer=serializers.AvroDeserializer(client), | ||
serializer=AvroSerializer(schema_registry_client), | ||
) |
15 changes: 11 additions & 4 deletions
15
examples/confluent-example/confluent_example/streaming/streams.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,23 @@ | ||
from kstreams import ConsumerRecord | ||
from confluent_example.middlewares import ConfluentMiddlewareDeserializer | ||
from kstreams import ConsumerRecord, middleware | ||
|
||
from .engine import stream_engine | ||
from .engine import schema_registry_client, stream_engine | ||
|
||
deployment_topic = "local--deployment" | ||
country_topic = "local--country" | ||
|
||
middlewares = [ | ||
middleware.Middleware( | ||
ConfluentMiddlewareDeserializer, schema_registry_client=schema_registry_client | ||
) | ||
] | ||
|
||
@stream_engine.stream(deployment_topic) | ||
|
||
@stream_engine.stream(deployment_topic, middlewares=middlewares) | ||
async def deployment_stream(cr: ConsumerRecord): | ||
print(f"Event consumed on topic {deployment_topic}. The user is {cr.value}") | ||
|
||
|
||
@stream_engine.stream(country_topic) | ||
@stream_engine.stream(country_topic, middlewares=middlewares) | ||
async def country_stream(cr: ConsumerRecord): | ||
print(f"Event consumed on topic {country_topic}. The Address is {cr.value}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
examples/dataclasses-avroschema-example/dataclasses_avroschema_example/middlewares.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from dataclasses_avroschema import AvroModel | ||
|
||
from kstreams import ConsumerRecord, middleware | ||
|
||
|
||
class AvroDeserializerMiddleware(middleware.BaseMiddleware): | ||
def __init__(self, *, model: AvroModel, **kwargs) -> None: | ||
super().__init__(**kwargs) | ||
self.model = model | ||
|
||
async def __call__(self, cr: ConsumerRecord): | ||
""" | ||
Deserialize a payload to an AvroModel | ||
""" | ||
if cr.value is not None: | ||
data = self.model.deserialize(cr.value) | ||
cr.value = data | ||
return await self.next_call(cr) |
18 changes: 0 additions & 18 deletions
18
examples/dataclasses-avroschema-example/dataclasses_avroschema_example/serializers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,9 @@ | ||
from dataclasses_avroschema import AvroModel | ||
|
||
from kstreams import ConsumerRecord | ||
|
||
|
||
class AvroSerializer: | ||
async def serialize(self, instance: AvroModel, **kwargs) -> bytes: | ||
""" | ||
Serialize an AvroModel to avro-binary | ||
""" | ||
return instance.serialize() | ||
|
||
|
||
class AvroDeserializer: | ||
def __init__(self, *, model: AvroModel) -> None: | ||
self.model = model | ||
|
||
async def deserialize( | ||
self, consumer_record: ConsumerRecord, **kwargs | ||
) -> ConsumerRecord: | ||
""" | ||
Deserialize a payload to an AvroModel | ||
""" | ||
if consumer_record.value is not None: | ||
data = self.model.deserialize(consumer_record.value) | ||
consumer_record.value = data | ||
return consumer_record |
Oops, something went wrong.