-
Notifications
You must be signed in to change notification settings - Fork 0
/
Consumer_Avro.py
69 lines (54 loc) · 2.09 KB
/
Consumer_Avro.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
from confluent_kafka import Consumer
from confluent_kafka.schema_registry import SchemaRegistryClient
from confluent_kafka.serialization import MessageField, SerializationContext, StringSerializer
from confluent_kafka.schema_registry.avro import AvroDeserializer
import os
from dotenv import load_dotenv
load_dotenv()
def schema_config():
return {
'url': os.getenv("ENDPOINT_URL"),
'basic.auth.user.info': f"{os.getenv('SCHEMA_REGISTRY_API_KEY')}:{os.getenv('SCHEMA_REGISTRY_API_SECRET_KEY')}"
}
def sasl_config():
return {
"bootstrap.servers": os.getenv("BOOTSTRAP_SERVER"),
"security.protocol": os.getenv("SECURITY_PROTOCOL"),
"sasl.mechanisms": os.getenv("SSL_MECHANISM"),
"sasl.username": os.getenv("API_KEY"),
"sasl.password": os.getenv("API_SECRET_KEY"),
"group.id": os.getenv("GROUP_ID"),
"auto.offset.reset": os.getenv("AUTO_OFFSET_RESET")
}
class Stock:
def __init__(self, record):
for k, v in record.items():
setattr(self, k, v)
self.record = record
@staticmethod
def dict_to_car(record, ctx):
return Stock(record)
def __str__(self):
print(f"{self.record}")
def main():
myTopic = 'topic_1'
consumer = Consumer(sasl_config())
schema_registry_client = SchemaRegistryClient(schema_config())
avro_schema = schema_registry_client.get_latest_version(myTopic + "-" + MessageField.VALUE)
schema = avro_schema.schema
print(schema)
avrodeserializer = AvroDeserializer(schema_registry_client, schema, from_dict=Stock.dict_to_car)
consumer.subscribe([myTopic])
while True:
try:
msg = consumer.poll(1.0)
if msg is not None:
value = avrodeserializer(msg.value(), SerializationContext(myTopic, MessageField.VALUE))
if value is not None:
print(value)
elif msg is None:
continue
except Exception as e:
print("Error: ", e)
consumer.close()
main()