main
HuangHai 3 weeks ago
parent 7d4a3d2632
commit e80c398488

@ -1,12 +1,6 @@
from Config.Config import * from Neo4j.Neo4jExecutor import Neo4jExecutor
from Neo4j.Neo4jExecutor import *
# 使用示例
if __name__ == '__main__': if __name__ == '__main__':
executor = Neo4jExecutor( executor = Neo4jExecutor.create_default()
uri=NEO4J_URI, executor.graph.run("MATCH (n) DETACH DELETE n")
auth=NEO4J_AUTH
)
# 清库
clear(executor.graph)
print("清库成功") print("清库成功")

@ -1,6 +1,7 @@
import json import json
from Config.Config import *
from Neo4j.Neo4jExecutor import * from Neo4j.Neo4jExecutor import Neo4jExecutor
def json_to_cypher(data): def json_to_cypher(data):
"""将知识体系JSON转换为Cypher插入脚本""" """将知识体系JSON转换为Cypher插入脚本"""
@ -47,12 +48,9 @@ MERGE (parent)-[:HAS_SUB_POINT]->(child);""")
# 使用示例 # 使用示例
if __name__ == '__main__': if __name__ == '__main__':
executor = Neo4jExecutor( executor = Neo4jExecutor.create_default()
uri=NEO4J_URI,
auth=NEO4J_AUTH
)
# 清库 # 清库
clear(executor.graph) executor.graph.run("MATCH (n) DETACH DELETE n")
# 这里替换成你的JSON数据变量 # 这里替换成你的JSON数据变量
with open('小学数学知识点体系.json', 'r',encoding='utf-8') as f: with open('小学数学知识点体系.json', 'r',encoding='utf-8') as f:

@ -1,8 +1,8 @@
import re import re
from typing import List, Tuple from typing import List, Tuple
from Config.Config import *
from py2neo import Graph, Node, Relationship, Subgraph
from py2neo import Graph, Node, Relationship, Subgraph
from Config import Config
def clear(db): def clear(db):
# 清空数据 # 清空数据
@ -22,7 +22,6 @@ def clear(db):
except Exception as e: except Exception as e:
print(f"删除操作失败: {e}") print(f"删除操作失败: {e}")
def create_subgraph(db: Graph, nodes: List[Node], relations: List[Tuple[Node, str, Node]]) -> None: def create_subgraph(db: Graph, nodes: List[Node], relations: List[Tuple[Node, str, Node]]) -> None:
"""统一创建子图""" """统一创建子图"""
subgraph = Subgraph( subgraph = Subgraph(
@ -47,8 +46,19 @@ def tx_create(db: Graph, nodes: List[Node], relations: List[Tuple[Node, str, Nod
raise raise
class Neo4jExecutor: class Neo4jExecutor:
def __init__(self, uri, auth): # 添加类变量存储连接配置
self.graph = Graph(uri, auth=auth) NEO4J_URI = Config.NEO4J_URI
NEO4J_AUTH = Config.NEO4J_AUTH
def __init__(self, uri=None, auth=None):
# 使用默认配置或传入参数
self.graph = Graph(uri or self.NEO4J_URI,
auth=auth or self.NEO4J_AUTH)
@classmethod
def create_default(cls):
"""使用默认配置创建执行器"""
return cls(cls.NEO4J_URI, cls.NEO4J_AUTH)
# 新增文本执行方法 # 新增文本执行方法
def execute_cypher_text(self, cypher_text: str) -> dict: def execute_cypher_text(self, cypher_text: str) -> dict:
@ -97,11 +107,3 @@ class Neo4jExecutor:
except Exception as e: except Exception as e:
print(f"文件错误: {str(e)}") print(f"文件错误: {str(e)}")
return stats return stats
def init():
executor = Neo4jExecutor(
uri=NEO4J_URI,
auth=NEO4J_AUTH
)
# 清库
clear(executor.graph)
Loading…
Cancel
Save