知识库

将(子)图导出为 Cypher 脚本并再次导入

通常您希望将完整(或部分)数据库导出到文件并再次导入,而无需复制实际的数据库文件。如果您想执行后者,请使用 neo4j-admin dump/load

这里有两种方法,可以从数据库或 Cypher 语句创建 Cypher 脚本文件。这也可以用于降级(较小)数据库。

格式

关于这些工具所写格式的一些说明

  • 重建索引和约束

  • 每个节点(CREATE)/关系(2x MATCH + CREATE)的简单语句

  • 您可以选择将导出配置为使用 MERGE 而不是 CREATE

  • 分批(默认为 40k)创建数据,可选择用 :begin, :commit 包围

  • 使用现有约束进行节点查找

  • 如果该标签上不存在约束,则在节点创建时使用人工约束 + 属性 (UNIQUE IMPORT LABEL.UNIQUE IMPORT ID),其中属性值是节点 ID

  • 最后分批清理人工标签 + 属性 + 约束

APOC

您可以安装 APOC 过程库。

然后使用 apoc.export.cypher.* 过程从您的图或数据创建 export.cypher 文件。更多信息请参阅文档,下面是一些示例。

这些过程具有配置选项,可以生成不同输出格式,还可以将节点、关系和模式脚本拆分到不同的文件中。

请注意,您必须首先在 neo4j.conf 中启用读写文件的能力。
apoc.export.file.enabled=true
apoc.import.file.enabled=true
// exports the whole database incl. indexes as cypher statements to the provided file
CALL apoc.export.cypher.all('/tmp/export.cypher',{format:'cypher-shell'})

// exports given nodes and relationships incl. indexes as cypher statements to the provided file
MATCH path = (p1:Person)-[r:KNOWS]->(p2:Person)
WITH collect(p1)+collect(p2) as export_nodes, collect(r) as export_rels
CALL apoc.export.cypher.data(export_nodes,export_rels,'/tmp/export.cypher',{format:'cypher-shell'})
YIELD file, source, format, nodes, relationships, properties, time
RETURN nodes, relationships, time;

// exports given graph object incl. indexes as cypher statements to the provided file
...
CALL apoc.graph.fromPaths([paths],'export_graph',{}) YIELD graph
CALL apoc.export.cypher.graph(graph,'/tmp/export.cypher',{format:'cypher-shell'}) YIELD time
RETURN time;

// exports nodes and relationships from the cypher statement incl. indexes as cypher statements to the provided file
CALL apoc.export.cypher.query(
'MATCH (p1:Person)-[r:KNOWS]->(p2:Person) RETURN *',
'/tmp/export.cypher',{format:'cypher-shell'});

使用 cypher-shell 导入

如果您使用 cypher-shell 格式导出文件,它将包含在 shell 中用于事务的正确语法。

然后您也可以使用 cypher-shell 导入它们。

$ cat /tmp/export.cypher | ./bin/cypher-shell -u neo4j -p password

导出文件示例

// create nodes
:begin
CREATE (:`UNIQUE IMPORT LABEL` {`UNIQUE IMPORT ID`:0});
CREATE (:`User` {`age`:43, `name`:"User1"});
:commit

// add schema
:begin
CREATE INDEX ON :`User`(`age`);
CREATE CONSTRAINT ON (node:`User`) ASSERT node.`name` IS UNIQUE;
CREATE CONSTRAINT ON (node:`UNIQUE IMPORT LABEL`) ASSERT node.`UNIQUE IMPORT ID` IS UNIQUE;
:commit

// wait for index completion
call db.awaitIndexes();

// create relationships
:begin
MATCH (n1:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`:0}), (n2:`User`{`name`:"User1"}) CREATE (n1)-[:`KNOWS` {`since`:2011}]->(n2);
:commit

// clean up temporary import keys (batched)
:begin
MATCH (n:`UNIQUE IMPORT LABEL`)  WITH n LIMIT 1000 REMOVE n:`UNIQUE IMPORT LABEL` REMOVE n.`UNIQUE IMPORT ID`;
:commit
:begin
DROP CONSTRAINT ON (node:`UNIQUE IMPORT LABEL`) ASSERT node.`UNIQUE IMPORT ID` IS UNIQUE;
:commit