-
Notifications
You must be signed in to change notification settings - Fork 2
/
my_api_graphDB.py
54 lines (46 loc) · 1.91 KB
/
my_api_graphDB.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
################################
# @author: Qianru Zhou
# @email: [email protected]
# All rights reserved
#################################
from neo4j.v1 import GraphDatabase, basic_auth
class findPath(object):
def __init__(self, **kwargs):
self.driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("your neo4j username", "your password"))
self.session = self.driver.session()
self.path = []
self.from_node = kwargs["start"]
self.to_node = kwargs["end"]
def __del__(self):
self.session.close()
def findShortestPath(self):
queryStr = "MATCH (start:Node {name:'"+ self.from_node +"'}), (end:Node {name:'"+ self.to_node +"'}), p = shortestPath((start)-[:CONNECT*]-(end)) RETURN p"
result = self.session.run(queryStr)
for p in result:
count = 0
for i in p["p"]:
start = self.findNodeById(i.start)
end = self.findNodeById(i.end)
if count == 0:
if start == self.from_node:
self.path.append(end)
else:
self.path.append(start)
count += 1
print self.path
def findNodeById(self, node_id):
n = self.session.run("MATCH (n:Node) WHERE id(n) = "+ str(node_id) +" RETURN n.name, n.isSwitch")
for i in n:
print i["n.name"], i["n.isSwitch"]
name = i["n.name"]
return name
def dumpAllLink(self):
result = self.session.run("MATCH (n)-[c]-() RETURN n.name AS name,ID(n) AS id, c, ID(c) AS c_id")
for r in result:
print r["name"], r["id"], r["c"], r["c_id"]
def dumpAllNode(self):
result = self.session.run("MATCH (n) RETURN n.name AS name, ID(n) AS id, n")
for r in result:
print r["name"], r["id"], r["n"]
#p1 = findPath(start ='h1', end ='h5')
#p1.findShortestPath()