-
Notifications
You must be signed in to change notification settings - Fork 419
/
Copy pathkafka_event.py
162 lines (133 loc) · 4.47 KB
/
kafka_event.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import base64
from functools import cached_property
from typing import Any, Dict, Iterator, List, Optional, overload
from aws_lambda_powertools.utilities.data_classes.common import DictWrapper
from aws_lambda_powertools.utilities.data_classes.shared_functions import (
get_header_value,
)
class KafkaEventRecord(DictWrapper):
@property
def topic(self) -> str:
"""The Kafka topic."""
return self["topic"]
@property
def partition(self) -> int:
"""The Kafka record parition."""
return self["partition"]
@property
def offset(self) -> int:
"""The Kafka record offset."""
return self["offset"]
@property
def timestamp(self) -> int:
"""The Kafka record timestamp."""
return self["timestamp"]
@property
def timestamp_type(self) -> str:
"""The Kafka record timestamp type."""
return self["timestampType"]
@property
def key(self) -> str:
"""The raw (base64 encoded) Kafka record key."""
return self["key"]
@property
def decoded_key(self) -> bytes:
"""Decode the base64 encoded key as bytes."""
return base64.b64decode(self.key)
@property
def value(self) -> str:
"""The raw (base64 encoded) Kafka record value."""
return self["value"]
@property
def decoded_value(self) -> bytes:
"""Decodes the base64 encoded value as bytes."""
return base64.b64decode(self.value)
@cached_property
def json_value(self) -> Any:
"""Decodes the text encoded data as JSON."""
return self._json_deserializer(self.decoded_value.decode("utf-8"))
@property
def headers(self) -> List[Dict[str, List[int]]]:
"""The raw Kafka record headers."""
return self["headers"]
@property
def decoded_headers(self) -> Dict[str, bytes]:
"""Decodes the headers as a single dictionary."""
return {k: bytes(v) for chunk in self.headers for k, v in chunk.items()}
@overload
def get_header_value(
self,
name: str,
default_value: str,
case_sensitive: bool = True,
) -> str:
...
@overload
def get_header_value(
self,
name: str,
default_value: Optional[str] = None,
case_sensitive: bool = True,
) -> Optional[str]:
...
def get_header_value(
self,
name: str,
default_value: Optional[str] = None,
case_sensitive: bool = True,
) -> Optional[str]:
"""Get a decoded header value by name."""
return get_header_value(
headers=self.decoded_headers,
name=name,
default_value=default_value,
case_sensitive=case_sensitive,
)
class KafkaEvent(DictWrapper):
"""Self-managed or MSK Apache Kafka event trigger
Documentation:
--------------
- https://docs.aws.amazon.com/lambda/latest/dg/with-kafka.html
- https://docs.aws.amazon.com/lambda/latest/dg/with-msk.html
"""
def __init__(self, data: Dict[str, Any]):
super().__init__(data)
self._records: Optional[Iterator[KafkaEventRecord]] = None
@property
def event_source(self) -> str:
"""The AWS service from which the Kafka event record originated."""
return self["eventSource"]
@property
def event_source_arn(self) -> Optional[str]:
"""The AWS service ARN from which the Kafka event record originated, mandatory for AWS MSK."""
return self.get("eventSourceArn")
@property
def bootstrap_servers(self) -> str:
"""The Kafka bootstrap URL."""
return self["bootstrapServers"]
@property
def decoded_bootstrap_servers(self) -> List[str]:
"""The decoded Kafka bootstrap URL."""
return self.bootstrap_servers.split(",")
@property
def records(self) -> Iterator[KafkaEventRecord]:
"""The Kafka records."""
for chunk in self["records"].values():
for record in chunk:
yield KafkaEventRecord(data=record, json_deserializer=self._json_deserializer)
@property
def record(self) -> KafkaEventRecord:
"""
Returns the next Kafka record using an iterator.
Returns
-------
KafkaEventRecord
The next Kafka record.
Raises
------
StopIteration
If there are no more records available.
"""
if self._records is None:
self._records = self.records
return next(self._records)