-
Notifications
You must be signed in to change notification settings - Fork 0
/
kafka_class.py
52 lines (41 loc) · 1.52 KB
/
kafka_class.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
import json
from uuid import uuid1
from kafka import KafkaConsumer, KafkaProducer
from kafka.errors import KafkaError
class NoProducerError(KafkaError):
message = "No producer error"
description = "No producer available in Kafka class."
class NoConsumerError(KafkaError):
message = "No consumer error"
description = "No consumer available in Kafka class."
class Kafka:
def __init__(self, bootstrap_server: str, topic_name: str, group_id: str = None) -> None:
self.bootstrap_server = bootstrap_server
self.topic_name = topic_name
if group_id:
self.group_id = group_id
else:
self.group_id = str(uuid1())
self.consumer = None
self.producer = None
def make_producer(self) -> KafkaProducer:
self.producer = KafkaProducer(
bootstrap_servers=self.bootstrap_server,
acks="all",
value_serializer=lambda v: json.dumps(v).encode("utf-8"),
)
return self.producer
def make_consumer(self) -> KafkaConsumer:
self.consumer = KafkaConsumer(
self.topic_name,
group_id=self.group_id,
bootstrap_servers=self.bootstrap_server,
auto_offset_reset="earliest",
value_deserializer=json.loads,
)
return self.consumer
def send_message(self, message: dict) -> None:
if not self.producer:
raise NoProducerError()
future = self.producer.send(self.topic_name, message)
_ = future.get(timeout=3)