apoc.cypher.runFiles

过程 Apoc 扩展

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

签名

apoc.cypher.runFiles(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 现在可以从文件系统的任何位置读取,因此在设置此属性之前,请确保这是您的意图。

使用示例

本节中的示例基于以下文件

$NEO4J_HOME/import/create.cypher
CREATE (n:Node {id:1});
$NEO4J_HOME/import/update.cypher
MATCH (n:Node {id:1})
SET n.city = "London";

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

CALL apoc.cypher.runFiles(["create.cypher", "update.cypher"]);
表 2. 结果
row fileName

结果

-1

create.cypher

{constraintsRemoved: 0, indexesRemoved: 0, nodesCreated: 1, rows: 0, propertiesSet: 1, labelsRemoved: 0, relationshipsDeleted: 0, constraintsAdded: 0, nodesDeleted: 0, indexesAdded: 0, labelsAdded: 1, relationshipsCreated: 0, time: 0}

-1

update.cypher

我们可以通过运行以下查询来查看数据库的内容

MATCH (n:Node)
RETURN n;
表 3. 结果
n

(:Node {city: "London", id: 1})

如果我们不想查看每条 Cypher 语句的统计信息,可以将 statistics 设置为 false

CALL apoc.cypher.runFiles(["create.cypher", "update.cypher"], {statistics: false});
表 4. 结果
row fileName
© . This site is unofficial and not affiliated with Neo4j, Inc.