|| apoc.export.cypher.query - APOC Core 文档 - Neo4j 文档

apoc.export.cypher.query

此过程被认为在多线程环境中运行不安全。因此,并行运行时不支持此过程。有关更多信息,请参阅Cypher 手册 → 并行运行时

详情

语法

apoc.export.cypher.query(statement [, file, config ]) :: (file, batches, source, format, nodes, relationships, properties, time, rows, batchSize, cypherStatements, nodeStatements, relationshipStatements, schemaStatements, cleanupStatements)

描述

将给定 Cypher 查询中的 NODERELATIONSHIP 值(包括索引)导出为 Cypher 语句到提供的文件(默认:Cypher Shell)。

输入参数

名称

类型

描述

statement

STRING

用于收集导出数据的查询。

file

STRING

要导出数据的文件名。默认值是:``。

config

MAP

{ stream = false :: BOOLEAN, batchSize = 20000 :: INTEGER, bulkImport = false :: BOOLEAN, timeoutSeconds = 100 :: INTEGER, compression = 'None' :: STRING, charset = 'UTF_8' :: STRING, sampling = false :: BOOLEAN, samplingConfig :: MAP }。默认值是:{}

返回参数

名称

类型

描述

file

STRING

数据被导出到的文件名。

batches

INTEGER

导出运行的批次数量。

source

STRING

导出数据的摘要。

format

STRING

文件导出的格式。

nodes

INTEGER

导出的节点数量。

relationships

INTEGER

导出的关系数量。

properties

INTEGER

导出的属性数量。

time

INTEGER

导出持续时间。

rows

INTEGER

返回的行数。

batchSize

INTEGER

导出运行的批次大小。

cypherStatements

ANY

已执行的 Cypher 语句。

nodeStatements

ANY

已执行的节点语句。

relationshipStatements

ANY

已执行的关系语句。

schemaStatements

ANY

已执行的 schema 语句。

cleanupStatements

ANY

已执行的清理语句。

配置参数

该过程支持以下配置参数

配置参数
名称 类型 默认 描述

writeNodeProperties

`BOOLEAN

false

如果为 true,则也导出属性。

stream

`BOOLEAN

false

将 json 直接流式传输到客户端的 data 字段中

format

STRING

cypher-shell

导出格式。支持以下值

  • cypher-shell - 用于 Cypher Shell 导入

  • neo4j-shell - 用于 Neo4j Shell 导入

  • plain - 导出不带 begincommitawait 命令的纯 Cypher。用于 Neo4j Browser 导入

cypherFormat

STRING

create

Cypher 更新操作类型。支持以下值

  • create - 仅使用 CREATE 子句

  • updateAll - 使用 MERGE 而不是 CREATE

  • addStructure - 对节点使用 MATCH,对关系使用 MERGE

  • updateStructure - 对节点和关系使用 MERGEMATCH

useOptimizations

MAP

{type: "UNWIND_BATCH", unwindBatchSize: 20}

用于 Cypher 语句生成的优化。type 支持以下值

  • NONE - 使用 CREATE 语句导出文件

  • UNWIND_BATCH - 按照 Michael Hunger 关于快速批量写入的文章中解释的方法,通过 UNWIND 批量处理实体来导出文件。

  • UNWIND_BATCH_PARAMS - 类似于 UNWIND_BATCH,但也在适当的地方使用参数

awaitForIndexes

INTEGER

300

使用 format: "cypher-shell" 时,用于 db.awaitIndexes 的超时时间

ifNotExists

BOOLEAN

false

如果为 true,则向约束和索引添加关键字 IF NOT EXISTS

导出到文件

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

apoc.conf
apoc.export.file.enabled=true

有关访问 apoc.conf 的更多信息,请参阅 配置选项 一章。

如果我们尝试在未首先设置此属性的情况下使用任何导出过程,将收到以下错误消息

调用过程失败:原因:java.lang.RuntimeException: 未启用文件导出,请在 apoc.conf 中设置 apoc.export.file.enabled=true。否则,如果您在没有文件系统访问权限的云环境中运行,请使用 {stream:true} 配置并将 'file' 参数设置为 null 以将导出流式传输回客户端。

导出文件写入到 import 目录,该目录由 server.directories.import 属性定义。这意味着我们提供的任何文件路径都是相对于此目录的。如果我们尝试写入绝对路径,例如 /tmp/filename,将收到类似于以下内容的错误消息

调用过程失败:原因:java.io.FileNotFoundException: /path/to/neo4j/import/tmp/fileName (没有此文件或目录)

我们可以通过在 apoc.conf 中设置以下属性来允许写入文件系统上的任何位置

apoc.conf
apoc.import.file.use_neo4j_config=false

Neo4j 现在将能够写入文件系统上的任何位置,因此在设置此属性之前请确保这是您的意图。

导出流

如果我们不想导出到文件,可以通过传入文件名为 null 并提供 stream:true 配置来将结果流式传输回 data 列。

使用示例

本节中的示例基于以下示例图

CREATE (TheMatrix:Movie {title:'The Matrix', released:1999, tagline:'Welcome to the Real World'})
CREATE (Keanu:Person {name:'Keanu Reeves', born:1964})
CREATE (Carrie:Person {name:'Carrie-Anne Moss', born:1967})
CREATE (Laurence:Person {name:'Laurence Fishburne', born:1961})
CREATE (Hugo:Person {name:'Hugo Weaving', born:1960})
CREATE (LillyW:Person {name:'Lilly Wachowski', born:1967})
CREATE (LanaW:Person {name:'Lana Wachowski', born:1965})
CREATE (JoelS:Person {name:'Joel Silver', born:1952})
CREATE
(Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrix),
(Carrie)-[:ACTED_IN {roles:['Trinity']}]->(TheMatrix),
(Laurence)-[:ACTED_IN {roles:['Morpheus']}]->(TheMatrix),
(Hugo)-[:ACTED_IN {roles:['Agent Smith']}]->(TheMatrix),
(LillyW)-[:DIRECTED]->(TheMatrix),
(LanaW)-[:DIRECTED]->(TheMatrix),
(JoelS)-[:PRODUCED]->(TheMatrix);

下面的 Neo4j Browser 可视化显示了导入的图

play movies
图 1. 电影图可视化

导出流

以下查询是使用 apoc.export.cypher.query 导出流的示例

查询
CALL apoc.export.cypher.query(
 "MATCH ()-[r]->()
 RETURN *",
 null,
 { format: "cypher-shell", stream: true })
结果
file batches source format nodes relationships properties time rows batchSize cypherStatements

null

1

"statement: nodes(8), rels(7)"

"cypher"

8

7

21

1

15

2000

":begin CREATE CONSTRAINT UNIQUE_IMPORT_NAME FOR (node:`UNIQUE IMPORT LABEL`) REQUIRE (node.UNIQUE IMPORT ID) IS UNIQUE; :commit :begin UNWIND [{_id:13, properties:{tagline:\"Welcome to the Real World\", title:\"The Matrix\", released:1999}}] AS row CREATE (n:`UNIQUE IMPORT LABEL`{UNIQUE IMPORT ID: row._id}) SET n += row.properties SET n:Movie; UNWIND [{_id:17, properties:{born:1960, name:\"Hugo Weaving\"}}, {_id:16, properties:{born:1961, name:\"Laurence Fishburne\"}}, {_id:14, properties:{born:1964, name:\"Keanu Reeves\"}}, {_id:15, properties:{born:1967, name:\"Carrie-Anne Moss\"}}, {_id:19, properties:{born:1965, name:\"Lana Wachowski\"}}, {_id:18, properties:{born:1967, name:\"Lilly Wachowski\"}}, {_id:20, properties:{born:1952, name:\"Joel Silver\"}}] AS row CREATE (n:`UNIQUE IMPORT LABEL`{UNIQUE IMPORT ID: row._id}) SET n += row.properties SET n:Person; :commit :begin UNWIND [{start: {_id:15}, end: {_id:13}, properties:{roles:[\"Trinity\"]}}, {start: {_id:14}, end: {_id:13}, properties:{roles:[\"Neo\"]}}, {start: {_id:17}, end: {_id:13}, properties:{roles:[\"Agent Smith\"]}}, {start: {_id:16}, end: {_id:13}, properties:{roles:[\"Morpheus\"]}}] AS row MATCH (start:`UNIQUE IMPORT LABEL`{UNIQUE IMPORT ID: row.start._id}) MATCH (end:`UNIQUE IMPORT LABEL`{UNIQUE IMPORT ID: row.end._id}) CREATE (start)-[r:ACTED_IN]->(end) SET r += row.properties; UNWIND [{start: {_id:20}, end: {_id:13}, properties:{}}] AS row MATCH (start:`UNIQUE IMPORT LABEL`{UNIQUE IMPORT ID: row.start._id}) MATCH (end:`UNIQUE IMPORT LABEL`{UNIQUE IMPORT ID: row.end._id}) CREATE (start)-[r:PRODUCED]->(end) SET r += row.properties; UNWIND [{start: {_id:18}, end: {_id:13}, properties:{}}, {start: {_id:19}, end: {_id:13}, properties:{}}] AS row MATCH (start:`UNIQUE IMPORT LABEL`{UNIQUE IMPORT ID: row.start._id}) MATCH (end:`UNIQUE IMPORT LABEL`{UNIQUE IMPORT ID: row.end._id}) CREATE (start)-[r:DIRECTED]->(end) SET r += row.properties; :commit :begin MATCH (n:`UNIQUE IMPORT LABEL`) WITH n LIMIT 20000 REMOVE n:`UNIQUE IMPORT LABEL` REMOVE n.UNIQUE IMPORT ID; :commit :begin DROP CONSTRAINT UNIQUE_IMPORT_NAME; :commit "

导出到文件

导出到 Cypher 过程都支持写入多个文件或多列。我们可以通过传入配置 separateFiles: true 来启用此模式

以下查询将所有 ACTED_IN 关系和相应的节点导出到以 actedIn 为前缀的文件中

CALL apoc.export.cypher.query(
  "MATCH ()-[r:ACTED_IN]->()
   RETURN *",
  "actedIn.cypher",
  { format: "cypher-shell", separateFiles: true })
YIELD file, batches, source, format, nodes, relationships, time, rows, batchSize
RETURN file, batches, source, format, nodes, relationships, time, rows, batchSize;
结果
file batches source format nodes relationships time rows batchSize

"actedIn.cypher"

1

"statement: nodes(10), rels(8)"

"cypher"

10

8

3

18

20000

这将导致创建以下文件

结果
名称 大小(字节) 行数

actedIn.cleanup.cypher

234

6

actedIn.nodes.cypher

893

6

actedIn.relationships.cypher

757

6

actedIn.schema.cypher

109

3

这些文件中的每一个都包含图的一个特定部分。我们来看看它们的内容

actedIn.cleanup.cypher
:begin
MATCH (n:`UNIQUE IMPORT LABEL`)  WITH n LIMIT 20000 REMOVE n:`UNIQUE IMPORT LABEL` REMOVE n.`UNIQUE IMPORT ID`;
:commit
:begin
DROP CONSTRAINT uniqueConstraint;
:commit
actedIn.nodes.cypher
:begin
UNWIND [{_id:28, properties:{tagline:"Welcome to the Real World", title:"The Matrix", released:1999}}, {_id:37, properties:{tagline:"Welcome to the Real World", title:"The Matrix", released:1999}}] AS row
CREATE (n:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row._id}) SET n += row.properties SET n:Movie;
UNWIND [{_id:31, properties:{born:1961, name:"Laurence Fishburne"}}, {_id:30, properties:{born:1967, name:"Carrie-Anne Moss"}}, {_id:42, properties:{born:1964, name:"Keanu Reeves"}}, {_id:0, properties:{born:1960, name:"Hugo Weaving"}}, {_id:29, properties:{born:1964, name:"Keanu Reeves"}}, {_id:38, properties:{born:1960, name:"Hugo Weaving"}}, {_id:43, properties:{born:1967, name:"Carrie-Anne Moss"}}, {_id:57, properties:{born:1961, name:"Laurence Fishburne"}}] AS row
CREATE (n:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row._id}) SET n += row.properties SET n:Person;
:commit
actedIn.relationships.cypher
:begin
UNWIND [{start: {_id:31}, end: {_id:28}, properties:{roles:["Morpheus"]}}, {start: {_id:42}, end: {_id:37}, properties:{roles:["Neo"]}}, {start: {_id:38}, end: {_id:37}, properties:{roles:["Agent Smith"]}}, {start: {_id:0}, end: {_id:28}, properties:{roles:["Agent Smith"]}}, {start: {_id:29}, end: {_id:28}, properties:{roles:["Neo"]}}, {start: {_id:43}, end: {_id:37}, properties:{roles:["Trinity"]}}, {start: {_id:30}, end: {_id:28}, properties:{roles:["Trinity"]}}, {start: {_id:57}, end: {_id:37}, properties:{roles:["Morpheus"]}}] AS row
MATCH (start:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row.start._id})
MATCH (end:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row.end._id})
CREATE (start)-[r:ACTED_IN]->(end) SET r += row.properties;
:commit
actedIn.schema.cypher
:begin
CREATE CONSTRAINT uniqueConstraint FOR (node:`UNIQUE IMPORT LABEL`) REQUIRE (node.`UNIQUE IMPORT ID`) IS UNIQUE;
:commit

然后我们可以将这些文件应用到目标 Neo4j 实例,方法是将它们的内容流式传输到 Cypher Shell 中,或使用运行 Cypher 片段中描述的过程。

我们也可以在返回导出语句流时使用 separateFiles。结果将出现在名为 nodeStatementsrelationshipStatementscleanupStatementsschemaStatements 的列中,而不是 cypherStatements

以下查询返回所有 ACTED_IN 关系和相应节点的流

CALL apoc.export.cypher.query(
  "MATCH ()-[r:ACTED_IN]->()
   RETURN *",
  null,
  { format: "cypher-shell", separateFiles: true })
YIELD nodes, relationships, properties, nodeStatements, relationshipStatements, cleanupStatements, schemaStatements
RETURN nodes, relationships, properties, nodeStatements, relationshipStatements, cleanupStatements, schemaStatements;
结果
nodes relationships properties nodeStatements relationshipStatements cleanupStatements schemaStatements

10

8

30

":begin UNWIND [{_id:28, properties:{tagline:\"Welcome to the Real World\", title:\"The Matrix\", released:1999}}, {_id:37, properties:{tagline:\"Welcome to the Real World\", title:\"The Matrix\", released:1999}}] AS row CREATE (n:`UNIQUE IMPORT LABEL`{UNIQUE IMPORT ID: row._id}) SET n += row.properties SET n:Movie; UNWIND [{_id:0, properties:{born:1960, name:\"Hugo Weaving\"}}, {_id:42, properties:{born:1964, name:\"Keanu Reeves\"}}, {_id:31, properties:{born:1961, name:\"Laurence Fishburne\"}}, {_id:29, properties:{born:1964, name:\"Keanu Reeves\"}}, {_id:30, properties:{born:1967, name:\"Carrie-Anne Moss\"}}, {_id:43, properties:{born:1967, name:\"Carrie-Anne Moss\"}}, {_id:38, properties:{born:1960, name:\"Hugo Weaving\"}}, {_id:57, properties:{born:1961, name:\"Laurence Fishburne\"}}] AS row CREATE (n:`UNIQUE IMPORT LABEL`{UNIQUE IMPORT ID: row._id}) SET n += row.properties SET n:Person; :commit "

":begin UNWIND [{start: {_id:31}, end: {_id:28}, properties:{roles:[\"Morpheus\"]}}, {start: {_id:38}, end: {_id:37}, properties:{roles:[\"Agent Smith\"]}}, {start: {_id:0}, end: {_id:28}, properties:{roles:[\"Agent Smith\"]}}, {start: {_id:30}, end: {_id:28}, properties:{roles:[\"Trinity\"]}}, {start: {_id:29}, end: {_id:28}, properties:{roles:[\"Neo\"]}}, {start: {_id:43}, end: {_id:37}, properties:{roles:[\"Trinity\"]}}, {start: {_id:42}, end: {_id:37}, properties:{roles:[\"Neo\"]}}, {start: {_id:57}, end: {_id:37}, properties:{roles:[\"Morpheus\"]}}] AS row MATCH (start:`UNIQUE IMPORT LABEL`{UNIQUE IMPORT ID: row.start._id}) MATCH (end:`UNIQUE IMPORT LABEL`{UNIQUE IMPORT ID: row.end._id}) CREATE (start)-[r:ACTED_IN]->(end) SET r += row.properties; :commit "

":begin MATCH (n:`UNIQUE IMPORT LABEL`) WITH n LIMIT 20000 REMOVE n:`UNIQUE IMPORT LABEL` REMOVE n.UNIQUE IMPORT ID; :commit :begin DROP CONSTRAINT uniqueConstraint; :commit "

":begin CREATE CONSTRAINT uniqueConstraint FOR (node:`UNIQUE IMPORT LABEL`) REQUIRE (node.UNIQUE IMPORT ID) IS UNIQUE; :commit "

然后我们可以将这些列的全部内容(不包括双引号)复制/粘贴到 Cypher Shell 会话中,或粘贴到我们流式传输到 Cypher Shell 会话的本地文件中。如果想导出可以粘贴到 Neo4j Browser 查询编辑器中的 Cypher 语句,需要使用配置 format: "plain"

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