-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer.py
61 lines (51 loc) · 1.9 KB
/
consumer.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
import os
import time
import logging
from kafka import KafkaConsumer
from kafka.errors import KafkaError
logging.basicConfig(level=logging.INFO)
class Consumer:
def __init__(self, bootstrap_servers: list, topics: list, group_id: str) -> None:
self.bootstrap_servers = bootstrap_servers
self.topics = topics
self.group_id = group_id
self.consumer = self.create()
def create(self):
return KafkaConsumer(
*self.topics,
bootstrap_servers=self.bootstrap_servers,
security_protocol="SASL_SSL",
ssl_check_hostname=False,
ssl_cafile="pem/ca-root.pem",
sasl_mechanism="SCRAM-SHA-256",
sasl_plain_username=os.environ["SASL_USERNAME"],
sasl_plain_password=os.environ["SASL_PASSWORD"],
auto_offset_reset="earliest",
enable_auto_commit=True,
group_id=self.group_id,
key_deserializer=lambda v: v.decode("utf-8"),
value_deserializer=lambda v: v.decode("utf-8"),
)
def process(self):
try:
while True:
msg = self.consumer.poll(timeout_ms=1000)
if msg is None:
continue
self.print_info(msg)
time.sleep(1)
except KafkaError as error:
logging.error(error)
def print_info(self, msg: dict):
for t, v in msg.items():
for r in v:
logging.info(
f"key={r.key}, value={r.value}, topic={t.topic}, partition={t.partition}, offset={r.offset}, ts={r.timestamp}"
)
if __name__ == "__main__":
consumer = Consumer(
bootstrap_servers=os.getenv("BOOTSTRAP_SERVERS", "localhost:29092").split(","),
topics=os.getenv("TOPIC_NAME", "orders").split(","),
group_id=os.getenv("GROUP_ID", "orders-group"),
)
consumer.process()