From 2aec88d41d90936752d51dacee5d411ce3c59aa1 Mon Sep 17 00:00:00 2001
From: Thomas Kluyver <thomas@kluyver.me.uk>
Date: Thu, 18 Mar 2021 16:57:13 +0000
Subject: [PATCH] Add script to print Kafka events

---
 webservice/listen_kafka.py | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
 create mode 100644 webservice/listen_kafka.py

diff --git a/webservice/listen_kafka.py b/webservice/listen_kafka.py
new file mode 100644
index 000000000..2c5e155ab
--- /dev/null
+++ b/webservice/listen_kafka.py
@@ -0,0 +1,24 @@
+"""Print Kafka events sent by the webservice.
+"""
+import json
+import os.path as osp
+
+import yaml
+from kafka import KafkaConsumer
+
+conf_file = osp.join(osp.dirname(__file__), 'webservice.yaml')
+
+with open(conf_file, "r") as f:
+    config = yaml.safe_load(f)
+
+topic = config['kafka']['topic']
+brokers = config['kafka']['brokers']
+print(f"topic: {topic!r}")
+print(f"brokers: {brokers!r}")
+
+kc = KafkaConsumer(topic, bootstrap_servers=brokers)
+
+print("Waiting for messages (Ctrl-C to quit)...")
+for record in kc:
+    msg = json.loads(record.value.decode())
+    print(msg)
-- 
GitLab