|| apoc.export.cypher.data - APOC 核心文档 - Neo4j 文档

apoc.export.cypher.data

此过程被认为从多线程运行不安全。因此,并行运行时不支持此过程。欲了解更多信息,请参阅 Cypher 手册 → 并行运行时

详情

语法

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

描述

将给定的 NODERELATIONSHIP 值(包括索引)作为 Cypher 语句导出到指定文件(默认:Cypher Shell)。

输入参数

名称

类型

描述

nodes

LIST<NODE>

要导出的节点列表。

rels

LIST<RELATIONSHIP>

要导出的关系列表。

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 - 导出纯 Cypher,不带 begincommitawait 命令。用于使用 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 - 通过 UNWIND 方法批量处理实体来导出文件,如 Michael Hunger 关于快速批量写入的文章中所述。

  • 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. 电影图可视化

导出到 Cypher 的过程使用 CREATEMATCHMERGE 子句生成 Cypher 语句。格式由 cypherFormat 参数配置。支持以下值

  • create - 仅使用 CREATE 子句(默认)

  • updateAll - 使用 MERGE 而不是 CREATE

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

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

如果首次导出数据库,应使用默认的 create 格式,但对于后续导出,其他格式可能更适合。

以下示例使用 create 格式将 ACTED_IN 关系和周围节点导出到 export-cypher-format-create.cypher

MATCH (person)-[r:ACTED_IN]->(movie)
WITH collect(DISTINCT person) + collect(DISTINCT  movie) AS importNodes, collect(r) AS importRels
CALL apoc.export.cypher.data(importNodes, importRels,
  "export-cypher-format-create.cypher",
  { format: "plain", cypherFormat: "create" })
YIELD file, batches, source, format, nodes, relationships, properties, time, rows, batchSize
RETURN file, batches, source, format, nodes, relationships, properties, time, rows, batchSize;
结果
file batches source format nodes relationships properties time rows batchSize

"export-cypher-format-create.cypher"

1

"data: nodes(5), rels(4)"

"cypher"

5

4

15

2

9

20000

export-cypher-format-create.cypher
CREATE CONSTRAINT uniqueConstraint FOR (node:`UNIQUE IMPORT LABEL`) REQUIRE (node.`UNIQUE IMPORT ID`) IS UNIQUE;
UNWIND [{_id:0, 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:7, properties:{born:1967, name:"Carrie-Anne Moss"}},
        {_id:80, properties:{born:1960, name:"Hugo Weaving"}},
        {_id:27, properties:{born:1964, name:"Keanu Reeves"}},
        {_id:44, 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;

UNWIND [{start: {_id:27}, end: {_id:0}, properties:{roles:["Neo"]}},
        {start: {_id:7}, end: {_id:0}, properties:{roles:["Trinity"]}},
        {start: {_id:44}, end: {_id:0}, properties:{roles:["Morpheus"]}},
        {start: {_id:80}, end: {_id:0}, properties:{roles:["Agent Smith"]}}] 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;

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

所有图实体的创建都使用 Cypher CREATE 子句。如果这些实体可能已存在于目标数据库中,我们可以选择使用其他格式。使用 cypherFormat: "updateAll" 意味着在创建实体时将使用 MERGE 子句而不是 CREATE

以下示例使用 updateAll 格式将 ACTED_IN 关系和周围节点导出到 export-cypher-format-create.cypher

MATCH (person)-[r:ACTED_IN]->(movie)
WITH collect(DISTINCT person) + collect(DISTINCT  movie) AS importNodes, collect(r) AS importRels
CALL apoc.export.cypher.data(importNodes, importRels,
  "export-cypher-format-updateAll.cypher",
  { format: "plain", cypherFormat: "updateAll" })
YIELD file, batches, source, format, nodes, relationships, properties, time, rows, batchSize
RETURN file, batches, source, format, nodes, relationships, properties, time, rows, batchSize;
结果
file batches source format nodes relationships properties time rows batchSize

"export-cypher-format-updateAll.cypher"

1

"data: nodes(5), rels(4)"

"cypher"

5

4

15

8

9

20000

export-cypher-format-updateAll.cypher
CREATE CONSTRAINT uniqueConstraint FOR (node:`UNIQUE IMPORT LABEL`) REQUIRE (node.`UNIQUE IMPORT ID`) IS UNIQUE;
UNWIND [{_id:0, properties:{tagline:"Welcome to the Real World", title:"The Matrix", released:1999}}] AS row
MERGE (n:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row._id}) SET n += row.properties SET n:Movie;

UNWIND [{_id:80, properties:{born:1960, name:"Hugo Weaving"}},
        {_id:7, properties:{born:1967, name:"Carrie-Anne Moss"}},
        {_id:44, properties:{born:1961, name:"Laurence Fishburne"}},
        {_id:27, properties:{born:1964, name:"Keanu Reeves"}}] AS row
MERGE (n:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row._id}) SET n += row.properties SET n:Person;

UNWIND [{start: {_id:27}, end: {_id:0}, properties:{roles:["Neo"]}},
        {start: {_id:7}, end: {_id:0}, properties:{roles:["Trinity"]}},
        {start: {_id:44}, end: {_id:0}, properties:{roles:["Morpheus"]}},
        {start: {_id:80}, end: {_id:0}, properties:{roles:["Agent Smith"]}}] AS row
MATCH (start:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row.start._id})
MATCH (end:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row.end._id})
MERGE (start)-[r:ACTED_IN]->(end) SET r += row.properties;

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

如果目标数据库中已存在节点,我们可以使用 cypherFormat: "addStructure" 仅为关系创建 Cypher CREATE 语句。

以下示例使用 addStructure 格式将 ACTED_IN 关系和周围节点导出到 export-cypher-format-addStructure.cypher

MATCH (person)-[r:ACTED_IN]->(movie)
WITH collect(DISTINCT person) + collect(DISTINCT  movie) AS importNodes, collect(r) AS importRels
CALL apoc.export.cypher.data(importNodes, importRels,
  "export-cypher-format-addStructure.cypher",
  { format: "plain", cypherFormat: "addStructure" })
YIELD file, batches, source, format, nodes, relationships, properties, time, rows, batchSize
RETURN file, batches, source, format, nodes, relationships, properties, time, rows, batchSize;
结果
file batches source format nodes relationships properties time rows batchSize

"export-cypher-format-addStructure.cypher"

1

"data: nodes(5), rels(4)"

"cypher"

5

4

15

4

9

20000

export-cypher-format-addStructure.cypher
UNWIND [{_id:0, properties:{tagline:"Welcome to the Real World", title:"The Matrix", released:1999}}] AS row
MERGE (n:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row._id}) ON CREATE SET n += row.properties SET n:Movie;

UNWIND [{_id:7, properties:{born:1967, name:"Carrie-Anne Moss"}},
        {_id:27, properties:{born:1964, name:"Keanu Reeves"}},
        {_id:80, properties:{born:1960, name:"Hugo Weaving"}},
        {_id:44, properties:{born:1961, name:"Laurence Fishburne"}}] AS row
MERGE (n:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row._id}) ON CREATE SET n += row.properties SET n:Person;

UNWIND [{start: {_id:27}, end: {_id:0}, properties:{roles:["Neo"]}},
        {start: {_id:7}, end: {_id:0}, properties:{roles:["Trinity"]}},
        {start: {_id:44}, end: {_id:0}, properties:{roles:["Morpheus"]}},
        {start: {_id:80}, end: {_id:0}, properties:{roles:["Agent Smith"]}}] 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;

在此示例中,我们使用 MERGE 子句在节点不存在时创建它,并且仅在节点不存在时才创建属性。在此示例中,关系在目标数据库中不存在,需要创建。

如果这些关系确实存在但具有需要更新的属性,我们可以使用 cypherFormat: "updateStructure" 来创建我们的导入脚本。

以下示例使用 updateStructure 格式将 ACTED_IN 关系和周围节点导出到 export-cypher-format-updateStructure.cypher

MATCH (person)-[r:ACTED_IN]->(movie)
WITH collect(DISTINCT person) + collect(DISTINCT  movie) AS importNodes, collect(r) AS importRels
CALL apoc.export.cypher.data(importNodes, importRels,
  "export-cypher-format-updateStructure.cypher",
  { format: "plain", cypherFormat: "updateStructure" })
YIELD file, batches, source, format, nodes, relationships, properties, time, rows, batchSize
RETURN file, batches, source, format, nodes, relationships, properties, time, rows, batchSize;
结果
file batches source format nodes relationships properties time rows batchSize

"export-cypher-format-updateStructure.cypher"

1

"data: nodes(5), rels(4)"

"cypher"

0

4

4

2

4

20000

export-cypher-format-updateStructure.cypher
UNWIND [{start: {_id:27}, end: {_id:0}, properties:{roles:["Neo"]}},
        {start: {_id:7}, end: {_id:0}, properties:{roles:["Trinity"]}},
        {start: {_id:44}, end: {_id:0}, properties:{roles:["Morpheus"]}},
        {start: {_id:80}, end: {_id:0}, properties:{roles:["Agent Smith"]}}] AS row
MATCH (start:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row.start._id})
MATCH (end:`UNIQUE IMPORT LABEL`{`UNIQUE IMPORT ID`: row.end._id})
MERGE (start)-[r:ACTED_IN]->(end) SET r += row.properties;
© . This site is unofficial and not affiliated with Neo4j, Inc.