-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgraph_database.py
99 lines (78 loc) · 2.98 KB
/
graph_database.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from neo4j import GraphDatabase
from config import neo4j_config
class GraphData:
"""
图数据库类
"""
def __init__(self, uri, user, password):
self.driver = GraphDatabase.driver(uri, auth=(user, password))
def close(self):
"""关闭数据库连接
"""
self.driver.close()
def execute_query(self,query) ->list:
''' 执行数据库查询
query: 查询语句
return: node list
'''
with self.driver.session() as session:
nodes = session.read_transaction(self.get_node,query)
return nodes
@staticmethod
def get_node(tx,query):
result = tx.run(query)
return result.data()
#获取图schema,该项目实际不跑该代码,需要用户自己根据readme.md中的说明生成schema
def schema_text(self,node_props, rel_props, rels) -> str:
return f"""
This is the schema representation of the Neo4j database.
Node properties are the following:
{node_props}
Relationship properties are the following:
{rel_props}
The relationships are the following
{rels}
"""
def get_schema(
self,
) -> str:
"""获取图schema
"""
#获取图schema,该项目实际不跑该代码,需要用户自己根据readme.md中的说明生成schema
print("loading graph schema")
node_props = [el["output"] for el in self.execute_query(node_properties_query)]
rel_props = [el["output"] for el in self.execute_query(rel_properties_query)]
rels = [el["output"] for el in self.execute_query(rel_query)]
schema = self.schema_text(node_props, rel_props, rels)
schema = schema.replace("{","<").replace("}",">")
print("finished")
return schema
node_properties_query = """
CALL apoc.meta.data()
YIELD label, other, elementType, type, property
WHERE NOT type = "RELATIONSHIP" AND elementType = "node"
WITH label AS nodeLabels, collect({property:property, type:type}) AS properties
RETURN {labels: nodeLabels, properties: properties} AS output
"""
rel_properties_query = """
CALL apoc.meta.data()
YIELD label, other, elementType, type, property
WHERE NOT type = "RELATIONSHIP" AND elementType = "relationship"
WITH label AS nodeLabels, collect({property:property, type:type}) AS properties
RETURN {type: nodeLabels, properties: properties} AS output
"""
rel_query = """
CALL apoc.meta.data()
YIELD label, other, elementType, type, property
WHERE type = "RELATIONSHIP" AND elementType = "node"
RETURN "(:" + label + ")-[:" + property + "]->(:" + toString(other[0]) + ")" AS output
"""
if __name__ == '__main__':
'''
运行以生成图谱schema
'''
graph = GraphData(uri=neo4j_config.url,
user=neo4j_config.username,
password=neo4j_config.password)
print(graph.get_schema())
graph.close()