apoc.cypher.runFilesReadOnly

过程 Apoc 扩展

apoc.cypher.runFilesReadOnly([files or urls],[{timeout:10,parameters:{}}])) - 运行文件中的每条 READ 语句,均以分号分隔

签名

apoc.cypher.runFilesReadOnly(file :: LIST? OF STRING?, config = {} :: MAP?) :: (row :: INTEGER?, result :: MAP?, fileName :: STRING?)

输入参数

名称 类型 默认

file

LIST? OF STRING?

null

config

MAP?

{}

配置参数

该过程支持以下配置参数

表 1. 配置参数
名称 (name) type 默认 description(描述)

reportError

boolean

false

返回一行包含键 error 和对应错误值(如有)的记录。

statistics

boolean

true

返回一行包含查询统计信息的附加行,利用了 org.neo4j.graphdb.QueryStatistics API。

timeout

long

10

单次查询超时时间(以秒为单位)

queueCapacity

long

100

用于聚合结果的 java.util.concurrent.BlockingQueue 的容量。

parameters

Map<String, Object>

空映射

可与 apoc.schema.runFileapoc.schema.runFiles 过程配合使用的可选参数映射。

输出参数

名称 类型

row

整数?

结果

MAP?

从文件读取

默认情况下,禁止从文件系统导入。我们可以通过在 apoc.conf 中设置以下属性来启用它

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.conf
apoc.import.file.use_neo4j_config=false

Neo4j 现在可以从文件系统的任何位置读取,因此在设置此属性之前,请确保这是您的意图。

使用示例

给定此数据集

CREATE (:ReturnQuery {id:1}), (:ReturnQuery {id:2}), (:ReturnQuery {id:3});
CREATE (n:Other {id: 4});
CREATE (n:SecondFile {id: 5});

以及位于 .$NEO4J_HOME/import 中的以下文件

match_first.cypher
MATCH (n:ReturnQuery) RETURN n;
MATCH (n:Other) RETURN n;
match_second.cypher
MATCH (n:SecondFile) RETURN n

我们可以通过运行以下查询来执行 create.cypherupdate.cypher 中的 Cypher 命令

CALL apoc.cypher.runFilesReadOnly(["match_first.cypher", "match_second.cypher"]);
表 2. 结果
row fileName 结果

0

.match_first.cypher

{"n":{"identity":1,"labels":["ReturnQuery"],"properties":{"id":1},"elementId":"…​"}}

1

.match_first.cypher

{"n":{"identity":2,"labels":["ReturnQuery"],"properties":{"id":2},"elementId":"…​"}}

2

.match_first.cypher

{"n":{"identity":3,"labels":["ReturnQuery"],"properties":{"id":3},"elementId":"…​"}}

0

.match_first.cypher

{"n":{"identity":4,"labels":["Other"],"properties":{"id":4},"elementId":"…​"}}

0

.match_second.cypher

{"n":{"identity":5,"labels":["SecondFile"],"properties":{"id":5},"elementId":"…​"}}

© . This site is unofficial and not affiliated with Neo4j, Inc.