apoc.cypher.runFileReadOnly
过程 Apoc 扩展
apoc.cypher.runFileReadOnly(file or url,[{statistics:true,timeout:10,parameters:{}}]) - 执行文件中以分号分隔的每个 READ(读取)语句 - 目前不支持模式(schema)操作
签名
apoc.cypher.runFileReadOnly(file :: STRING?, config = {} :: MAP?) :: (row :: INTEGER?, result :: MAP?, fileName :: STRING?)
配置参数
该过程支持以下配置参数
| 名称 (name) | type | 默认 | description(描述) |
|---|---|---|---|
reportError |
boolean |
false |
返回一行包含键 |
statistics |
boolean |
true |
返回一行包含查询统计信息的附加行,利用了 |
timeout |
long |
10 |
单次查询超时时间(以秒为单位) |
queueCapacity |
long |
100 |
用于聚合结果的 |
parameters |
Map<String, Object> |
空映射 |
可与 |
从文件读取
默认情况下,禁止从文件系统导入。我们可以通过在 apoc.conf 中设置以下属性来启用它
apoc.import.file.enabled=true
如果我们尝试在未先设置此属性的情况下使用任何导入过程,将会收到以下错误消息
Failed to invoke procedure: Caused by: java.lang.RuntimeException: Import from files not enabled, please set apoc.import.file.enabled=true in your apoc.conf |
导入文件从 import 目录读取,该目录由 server.directories.import 属性定义。这意味着我们提供的任何文件路径都是相对于此目录的。如果我们尝试从绝对路径(例如 /tmp/filename)读取,将会收到类似于以下的错误消息
Failed to invoke procedure: Caused by: java.lang.RuntimeException: Can’t read url or key file:/path/to/neo4j/import/tmp/filename as json: /path/to/neo4j//import/tmp/filename (No such file or directory) |
我们可以通过在 apoc.conf 中设置以下属性来启用从文件系统任意位置读取文件
apoc.import.file.use_neo4j_config=false
|
Neo4j 现在可以从文件系统的任何位置读取,因此在设置此属性之前,请确保这是您的意图。 |
用法示例
给定此数据集
CREATE (:ReturnQuery {id:1}), (:ReturnQuery {id:2}), (:ReturnQuery {id:3});
CREATE (n:Other {id: 4});
以及此文件,位于 .$NEO4J_HOME/import 中
MATCH (n:ReturnQuery) RETURN n;
MATCH (n:Other) RETURN n;
我们可以通过运行以下查询来执行 match.cypher 中的 Cypher 命令
CALL apoc.cypher.runFileReadOnly("match.cypher");
| row | fileName |
|---|---|
结果 |
0 |
match.cypher |
{"n":{"identity":1,"labels":["ReturnQuery"],"properties":{"id":1},"elementId":"…"}} |
1 |
match.cypher |
{"n":{"identity":2,"labels":["ReturnQuery"],"properties":{"id":2},"elementId":"…"}} |
2 |
match.cypher |
{"n":{"identity":3,"labels":["ReturnQuery"],"properties":{"id":3},"elementId":"…"}} |
0 |
match.cypher |