-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.py
56 lines (48 loc) · 1.61 KB
/
app.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
"""This is a generic Kafka Consumer and an example Python code on how to query
Memgraph.
"""
import logging
import csv
from gqlalchemy import Memgraph
from kafka import KafkaConsumer
def process(message: str, db: Memgraph):
"""Prints the number of neighbors."""
logging.info(f"Received `{message}`")
payload = next(csv.reader([message], delimiter="|"))
command, *payload = payload
if command == "node":
label, unique_fields, fields = payload
neighbors = next(
db.execute_and_fetch(
f"match (a:{label} {unique_fields}) return a.neighbors as n"
)
)["n"]
if neighbors is None:
print(
"The neighbors variable isn't set. "
"Memgraph triggers are probably not set up properly."
)
else:
print(f"(node:{label} {unique_fields}) has {neighbors} neighbors.")
elif command == "edge":
pass
else:
raise ValueError(f"Command `{command}` not recognized.")
if __name__ == "__main__":
logging.basicConfig(
level=logging.INFO,
format="%(levelname)s: %(asctime)s %(message)s",
)
db = Memgraph(host="localhost", port=7687)
db.drop_database()
consumer = KafkaConsumer("topic", bootstrap_servers=["localhost:9092"])
try:
for message in consumer:
message = message.value.decode("utf-8")
try:
process(message, db)
except Exception as error:
logging.error(f"`{message}`, {repr(error)}")
continue
except KeyboardInterrupt:
pass