apoc.export.cypher.query
|
此过程被认为在多线程环境中运行不安全。因此,并行运行时不支持此过程。有关更多信息,请参阅Cypher 手册 → 并行运行时。 |
语法 |
|
||
描述 |
将给定 Cypher 查询中的 |
||
输入参数 |
名称 |
类型 |
描述 |
|
|
用于收集导出数据的查询。 |
|
|
|
要导出数据的文件名。默认值是:``。 |
|
|
|
|
|
返回参数 |
名称 |
类型 |
描述 |
|
|
数据被导出到的文件名。 |
|
|
|
导出运行的批次数量。 |
|
|
|
导出数据的摘要。 |
|
|
|
文件导出的格式。 |
|
|
|
导出的节点数量。 |
|
|
|
导出的关系数量。 |
|
|
|
导出的属性数量。 |
|
|
|
导出持续时间。 |
|
|
|
返回的行数。 |
|
|
|
导出运行的批次大小。 |
|
|
|
已执行的 Cypher 语句。 |
|
|
|
已执行的节点语句。 |
|
|
|
已执行的关系语句。 |
|
|
|
已执行的 schema 语句。 |
|
|
|
已执行的清理语句。 |
|
配置参数
该过程支持以下配置参数
| 名称 | 类型 | 默认 | 描述 |
|---|---|---|---|
|
`BOOLEAN |
false |
如果为 true,则也导出属性。 |
|
`BOOLEAN |
false |
将 json 直接流式传输到客户端的 |
|
|
cypher-shell |
导出格式。支持以下值
|
|
|
create |
Cypher 更新操作类型。支持以下值
|
useOptimizations |
MAP |
|
用于 Cypher 语句生成的优化。
|
|
|
300 |
使用 |
|
|
false |
如果为 true,则向约束和索引添加关键字 |
导出到文件
默认情况下,导出到文件系统是禁用的。我们可以通过在 apoc.conf 中设置以下属性来启用它
apoc.export.file.enabled=true
有关访问 apoc.conf 的更多信息,请参阅 配置选项 一章。
如果我们尝试在未首先设置此属性的情况下使用任何导出过程,将收到以下错误消息
调用过程失败:原因:java.lang.RuntimeException: 未启用文件导出,请在 apoc.conf 中设置 apoc.export.file.enabled=true。否则,如果您在没有文件系统访问权限的云环境中运行,请使用 |
导出文件写入到 import 目录,该目录由 server.directories.import 属性定义。这意味着我们提供的任何文件路径都是相对于此目录的。如果我们尝试写入绝对路径,例如 /tmp/filename,将收到类似于以下内容的错误消息
调用过程失败:原因:java.io.FileNotFoundException: /path/to/neo4j/import/tmp/fileName (没有此文件或目录) |
我们可以通过在 apoc.conf 中设置以下属性来允许写入文件系统上的任何位置
apoc.import.file.use_neo4j_config=false
|
Neo4j 现在将能够写入文件系统上的任何位置,因此在设置此属性之前请确保这是您的意图。 |
使用示例
本节中的示例基于以下示例图
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 可视化显示了导入的图
导出流
以下查询是使用 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. |
导出到文件
导出到 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 |
这些文件中的每一个都包含图的一个特定部分。我们来看看它们的内容
: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
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
: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
:begin
CREATE CONSTRAINT uniqueConstraint FOR (node:`UNIQUE IMPORT LABEL`) REQUIRE (node.`UNIQUE IMPORT ID`) IS UNIQUE;
:commit
然后我们可以将这些文件应用到目标 Neo4j 实例,方法是将它们的内容流式传输到 Cypher Shell 中,或使用运行 Cypher 片段中描述的过程。
我们也可以在返回导出语句流时使用 separateFiles。结果将出现在名为 nodeStatements、relationshipStatements、cleanupStatements 和 schemaStatements 的列中,而不是 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`{ |
":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`{ |
":begin MATCH (n:`UNIQUE IMPORT LABEL`) WITH n LIMIT 20000 REMOVE n:`UNIQUE IMPORT LABEL` REMOVE n. |
":begin CREATE CONSTRAINT uniqueConstraint FOR (node:`UNIQUE IMPORT LABEL`) REQUIRE (node. |
然后我们可以将这些列的全部内容(不包括双引号)复制/粘贴到 Cypher Shell 会话中,或粘贴到我们流式传输到 Cypher Shell 会话的本地文件中。如果想导出可以粘贴到 Neo4j Browser 查询编辑器中的 Cypher 语句,需要使用配置 format: "plain"。