-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumer.py
68 lines (58 loc) · 2.06 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
61
62
63
64
65
66
67
68
#
# National Rail Open Data client demonstrator
# Copyright (C)2024 OpenTrainTimes Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
import confluent_kafka as kafka
import logging
import json
logging.basicConfig(format='%(asctime)s %(levelname)s\t%(message)s', level=logging.INFO)
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
GROUP_ID = ''
if CONSUMER_KEY == '' or CONSUMER_SECRET == '':
logging.error("Credentials not set - please set CONSUMER_KEY and CONSUMER_SECRET to the values shown in the RDM")
exit(1)
if GROUP_ID == '':
logging.error("Group ID not set - please set GROUP_ID to a unique identifier for this client")
exit(1)
config = {
'bootstrap.servers': 'pkc-l6wr6.europe-west2.gcp.confluent.cloud:9092',
'sasl.username': CONSUMER_KEY,
'sasl.password': CONSUMER_SECRET,
'security.protocol': 'SASL_SSL',
'sasl.mechanism': 'PLAIN',
'group.id': GROUP_ID,
'auto.offset.reset': 'earliest',
}
consumer = kafka.Consumer(config)
topic = 'prod-1010-Darwin-Train-Information-Push-Port-IIII1_1-JSON'
consumer.subscribe([topic])
try:
while True:
msg = consumer.poll(timeout=10)
if msg is None:
logging.info("Waiting")
elif msg.error():
logging.error(msg.error())
else:
parsed_msg = msg.value().decode('utf-8')
obj = json.loads(parsed_msg)
payload = json.loads(obj['bytes'])
logging.info(payload)
except KeyboardInterrupt:
pass
finally:
consumer.close()