-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconsumer.py
More file actions
54 lines (40 loc) · 1.36 KB
/
consumer.py
File metadata and controls
54 lines (40 loc) · 1.36 KB
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
from confluent_kafka import KafkaError
from confluent_kafka.avro import AvroConsumer
from confluent_kafka.avro.serializer import SerializerError
import os
import time
"""
This is an example consumer intended to demonstrate basic
functionality of the pipeline.
This consumer subscribes to a topic, deserializes events (messages),
and prints them out.
You must set the TOPIC_NAME environment variable.
This image does not have a default TOPIC_NAME set, to avoid
potentially confusing errors.
Reference: https://github.com/confluentinc/confluent-kafka-python
"""
BROKER = os.environ['BROKER']
SCHEMA_REGISTRY_URL = os.environ['SCHEMA_REGISTRY_URL']
TOPIC_NAME = os.environ['TOPIC_NAME']
c = AvroConsumer({
'bootstrap.servers': BROKER,
'group.id': 'groupid',
'schema.registry.url': SCHEMA_REGISTRY_URL})
c.subscribe([TOPIC_NAME])
while True:
try:
# block for 10s max waiting for message
msg = c.poll(timeout=10)
except SerializerError as e:
# Report malformed record, discard results, continue polling
print("Message deserialization failed for {}: {}".format(msg, e))
break
# There were no messages on the queue, continue polling
if msg is None:
continue
if msg.error():
print("AvroConsumer error: {}".format(msg.error()))
continue
print(msg.value())
print(msg.key())
c.close()