Skip to content

Commit f998fdf

Browse files
Adding support for Kafka Consumer - first commit
1 parent 3a9a0e8 commit f998fdf

File tree

5 files changed

+545
-464
lines changed

5 files changed

+545
-464
lines changed

aws_lambda_powertools/utilities/kafka_consumer/__init__.py

Whitespace-only changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from __future__ import annotations
2+
3+
from typing import Any
4+
5+
from aws_lambda_powertools.utilities.data_classes.kafka_event import KafkaEventRecord
6+
7+
8+
class ConsumerRecord(KafkaEventRecord):
9+
"""
10+
A Kafka Consumer Record
11+
"""
12+
13+
def __init__(self, data: dict[str, Any], json_deserializer=None):
14+
super().__init__(data, json_deserializer=json_deserializer)
15+
self._json_deserializer = json_deserializer
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from __future__ import annotations
2+
3+
import io
4+
5+
from avro.errors import SchemaResolutionException
6+
from avro.io import BinaryDecoder, DatumReader
7+
8+
9+
def deserialize_avro(avro_bytes, reader_schema: str | None = None):
10+
"""
11+
Deserialize Avro binary data to Python objects
12+
13+
Parameters
14+
----------
15+
avro_bytes: bytes
16+
Avro binary data
17+
reader_schema: str, Optional
18+
Schema to use for reading
19+
20+
Returns
21+
-------
22+
dict
23+
Deserialized Python object
24+
25+
Raises
26+
------
27+
ValueError
28+
If reader_schema schema is None or if deserialization fails
29+
"""
30+
try:
31+
reader = DatumReader(reader_schema)
32+
33+
decoder = BinaryDecoder(io.BytesIO(avro_bytes))
34+
return reader.read(decoder)
35+
except SchemaResolutionException as e:
36+
raise ValueError(f"Schema mismatch: {e}") from e
37+
except Exception as e:
38+
raise ValueError(f"Failed to deserialize Avro data: {e}") from e

poetry.lock

Lines changed: 490 additions & 464 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ nox = "^2024.4.15"
125125
mkdocstrings-python = "^1.13.0"
126126
datadog-lambda = "^6.106.0"
127127
mkdocs-llmstxt = "^0.2.0"
128+
avro = "^1.12.0"
129+
protobuf = "^6.30.2"
128130

129131
[tool.coverage.run]
130132
source = ["aws_lambda_powertools"]

0 commit comments

Comments
 (0)