1
+ """This is a generic Kafka Consumer and an example Python code on how to query
2
+ Memgraph.
1
3
"""
2
- This is a generic Kafka Consumer that will enable you to store data from Kafka
3
- to Memgraph.
4
- The data pushed to Kafka has to be in the following format:
5
- command|label|unique_fields|fields
6
- command|label1|unique_fields1|edge_type|edge_fields|label2|unique_fields2
7
-
8
- command - string: "edge", or "node"
9
- label - string: type(s) of a node e.g. "Person", or "Machine:Vehicle:Car"
10
- edge_type - string: type of an edge e.g. "CONNECTED_WITH"
11
- fields - string in form of a json/python dictionary representing the
12
- properties of a node or edge:
13
- `{age: 53}` or `{id: 4, name: "hero", alive: true}`
14
- """
15
- import csv
16
4
import logging
5
+ import csv
17
6
18
7
from gqlalchemy import Memgraph
19
8
from kafka import KafkaConsumer
20
9
21
10
22
11
def process (message : str , db : Memgraph ):
23
- """Takes graph database `db` and a string message in the following format:
24
-
25
- command|label|unique_fields|fields
26
- command|label1|unique_fields1|edge_type|edge_fields|label2|unique_fields2
27
-
28
- command - string: "edge", or "node"
29
- label - string: type of a node e.g. "Person" or "Machine:Vehicle:Car"
30
- edge_type - string: type of an edge e.g. "CONNECTED_WITH"
31
- fields - string in form of a json/python dictionary representing the
32
- properties of a node or edge:
33
- `{age: 53}` or `{id: 4, name: "hero", alive: true}`
34
-
35
- Throws a ValueError if the command isn't recognised.
36
- """
12
+ """Prints the number of neighbors."""
13
+ logging .info (f"Received `{ message } `" )
37
14
payload = next (csv .reader ([message ], delimiter = "|" ))
38
15
command , * payload = payload
39
16
40
17
if command == "node" :
41
18
label , unique_fields , fields = payload
42
- db .execute_query (f"merge (a:{ label } { unique_fields } ) set a += { fields } " )
43
- neighbors = next (db .execute_and_fetch (
44
- f"match (a:{ label } { unique_fields } ) return a.neighbors as n"
45
- ))['n' ]
19
+ neighbors = next (
20
+ db .execute_and_fetch (
21
+ f"match (a:{ label } { unique_fields } ) return a.neighbors as n"
22
+ )
23
+ )["n" ]
46
24
if neighbors is None :
47
25
print (
48
26
"The neighbors variable isn't set. "
@@ -51,32 +29,20 @@ def process(message: str, db: Memgraph):
51
29
else :
52
30
print (f"(node:{ label } { unique_fields } ) has { neighbors } neighbors." )
53
31
elif command == "edge" :
54
- (
55
- label1 ,
56
- unique_fields1 ,
57
- edge_type ,
58
- edge_fields ,
59
- label2 ,
60
- unique_fields2 ,
61
- ) = payload
62
- db .execute_query (
63
- f"merge (a:{ label1 } { unique_fields1 } ) "
64
- f"merge (b:{ label2 } { unique_fields2 } ) "
65
- f"merge (a)-[:{ edge_type } { edge_fields } ]->(b)"
66
- )
32
+ pass
67
33
else :
68
34
raise ValueError (f"Command `{ command } ` not recognized." )
69
- logging .info (f"`{ message } `, Successfully entered { command } in Memgraph." )
70
35
71
36
72
37
if __name__ == "__main__" :
73
38
logging .basicConfig (
74
- filename = "info.log" ,
75
39
level = logging .INFO ,
76
40
format = "%(levelname)s: %(asctime)s %(message)s" ,
77
41
)
42
+
78
43
db = Memgraph (host = "localhost" , port = 7687 )
79
44
db .drop_database ()
45
+
80
46
consumer = KafkaConsumer ("topic" , bootstrap_servers = ["localhost:9092" ])
81
47
try :
82
48
for message in consumer :
@@ -86,6 +52,5 @@ def process(message: str, db: Memgraph):
86
52
except Exception as error :
87
53
logging .error (f"`{ message } `, { repr (error )} " )
88
54
continue
89
-
90
55
except KeyboardInterrupt :
91
56
pass
0 commit comments