知识库

如何将参数传递给 apoc.cypher.runFile 调用

APOC 允许您拥有一个存储过程,apoc.cypher.runFile,然后将文件内容运行到 Cypher 引擎。为了允许在 conf/neo4j.conf 中读取文件,您需要定义

apoc.import.file.enabled=true

然后,如果您定义一个文件,例如 import/myRunFile.cyp,并且它包含以下内容

create (n:Person {id:123, name:'Emil Eifrem'});

通过 Cypher 运行

call apoc.cypher.runFile("myRunFile.cyp",{}) yield row, result;

将创建具有 id=123 和 name='Emil Eifrem' 的 :Person 节点。

但是,如果您希望提供更多灵活性,以便通过参数传递 idname 的值,可以通过更改 import/myRunFile.cyp 的内容来实现,使其定义为

create (n:Person { id: $id_p1, name: $name_p2});

然后按如下方式调用 apoc.cypher.runfile:

call apoc.cypher.runFile("myRunFile.cyp",{parameters: {id_p1: 123, name_p2:'Emil Eifrem'}}) yield row, result;