编辑
2024-01-16
CS_杂项
0
请注意,本文编写于 247 天前,最后修改于 246 天前,其中某些信息可能已经过时。

在Neo4j Desktop中提供了APOC插件的直接安装,并且会直接匹配与当前使用的neo4j版本match的插件版本。当然也可能会和我一样报错:Plugin failed to install.Error: net:ERR_CONNNECTION_TIMED_OUT,这是因为上网的方式不够科学。

1280X1280.PNG

可以在设置界面设置在自己的proxy,这样便可以继续上面的安装步骤。安装成功后可以把proxy给reset回来。 image.png 安装完成后插件界面如下所示:

image.png 现在因为我们要使用export的功能,并且使得当前的知识图谱通过.cypher文件的方式保存至指定路径下,需要修改配置文件。之前的版本是通过neo4j.conf进行设置,但现在是单独通过apoc.conf进行控制,我们需要新建一个apoc.conf文件,并加入以下设置:

plain
apoc.export.file.enabled=true apoc.import.file.use_neo4j_config=false

line1 是为了使得apoc能够执行export功能 line2 是因为后续指定路径为file:path/1.cypher如果不设置为false的话,会自动在路径前加上当前环境的路径。

image.png

image.png

之后通过以下命令便可以保存模型,如我现在想要把文件保存到E:/cypher/1.cypher中,命令如下: CALL apoc.export.cypher.all("file:E:/cypher/1.cypher", {format: "plain"}) 如果想要通过.cypher文件重现知识图谱,只需要把.cypher文件中的所有内容复制到Neo4j Desktop中运行即可。

同时因为我是使用python写的脚本,顺带附上,帮助参考。

python
def save(self, path:str): r""" Save the current knowledge graph to a specified path. If you want to use absolute path, please make sure to set apoc.conf as follows: apoc.export.file.enabled=true apoc.import.file.use_neo4j_config=false Args: path (str): the specified path to save the knowledge graph Examples: >>> KG = nekg("bolt://localhost:7687", "neo4j", "password") >>> KG.save(r"E:\cypher\30.cypher") """ with self.driver.session() as session: cypher_query = ( f"CALL apoc.export.cypher.all(\"file:{path}\", {{format: \"plain\"}})" ) result = session.run(cypher_query) return result.single()[0] def load(self, path:str): r""" Load a knowledge graph from a specified cypher file path. Redo all cypher in the file. Args: path (str): the specified path to save the knowledge graph Examples: >>> KG = nekg("bolt://localhost:7687", "neo4j", "password") >>> KG.load(r"E:\cypher\30.cypher") """ with open(path, 'r', encoding='utf-8') as file: cypher_content = file.read() # Split the contents of a file into individual statements. statements = [stmt.strip() for stmt in cypher_content.split(';') if stmt.strip()] with self.driver.session() as session: # Execute each statement individually for stmt in statements: session.run(stmt)

本文作者:Geaming

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!