|| apoc.cypher.run - APOC 核心文档 - Neo4j 文档

apoc.cypher.run

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

详情

语法

apoc.cypher.run(statement, params) :: (value)

描述

运行一个使用给定参数动态构造的只读语句。

输入参数

名称

类型

描述

statement

STRING

要运行的 Cypher 语句。

params

MAP

给定 Cypher 语句的参数。

返回参数

名称

类型

描述

value

MAP

从 Cypher 语句返回的结果。

在 Cypher 中使用动态标签

节点标签和关系类型可以在 Cypher 中动态引用,而无需使用 APOC。

动态创建、匹配和合并标签与类型的 Cypher 语法
CREATE (n1:$(label))-[r:$(type)]->(n2:$(label))
MERGE (n1:$(label))-[r:$(type)]->(n2:$(label))
MATCH (n1:$(label))-[r:$(type)]->(n2:$(label))

动态计算的类型必须解析为 STRINGLIST<STRING>。欲了解更多信息,请参阅Cypher 手册 → CREATEMERGEMATCH

使用示例

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

CREATE (Keanu:Person {name:'Keanu Reeves', born:1964})
CREATE (TomH:Person {name:'Tom Hanks', born:1956})
CREATE (TomT:Person {name:'Tom Tykwer', born:1965})

CREATE (TheMatrix:Movie {title:'The Matrix', released:1999, tagline:'Welcome to the Real World'})
CREATE (TheMatrixReloaded:Movie {title:'The Matrix Reloaded', released:2003, tagline:'Free your mind'})
CREATE (TheMatrixRevolutions:Movie {title:'The Matrix Revolutions', released:2003, tagline:'Everything that has a beginning has an end'})
CREATE (SomethingsGottaGive:Movie {title:"Something's Gotta Give", released:2003})
CREATE (TheDevilsAdvocate:Movie {title:"The Devil's Advocate", released:1997, tagline:'Evil has its winning ways'})

CREATE (YouveGotMail:Movie {title:"You've Got Mail", released:1998, tagline:'At odds in life... in love on-line.'})
CREATE (SleeplessInSeattle:Movie {title:'Sleepless in Seattle', released:1993, tagline:'What if someone you never met, someone you never saw, someone you never knew was the only someone for you?'})
CREATE (ThatThingYouDo:Movie {title:'That Thing You Do', released:1996, tagline:'In every life there comes a time when that thing you dream becomes that thing you do'})
CREATE (CloudAtlas:Movie {title:'Cloud Atlas', released:2012, tagline:'Everything is connected'})

CREATE (Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrix)
CREATE (Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrixReloaded)
CREATE (Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrixRevolutions)
CREATE (Keanu)-[:ACTED_IN {roles:['Julian Mercer']}]->(SomethingsGottaGive)
CREATE (Keanu)-[:ACTED_IN {roles:['Kevin Lomax']}]->(TheDevilsAdvocate)

CREATE (TomH)-[:ACTED_IN {roles:['Joe Fox']}]->(YouveGotMail)
CREATE (TomH)-[:ACTED_IN {roles:['Sam Baldwin']}]->(SleeplessInSeattle)
CREATE (TomH)-[:ACTED_IN {roles:['Mr. White']}]->(ThatThingYouDo)
CREATE (TomH)-[:ACTED_IN {roles:['Zachry', 'Dr. Henry Goose', 'Isaac Sachs', 'Dermot Hoggins']}]->(CloudAtlas)
CREATE (TomT)-[:DIRECTED]->(CloudAtlas);

此过程对于执行具有动态节点标签或关系类型的 Cypher 语句非常有用。但是,这也可以在不使用 APOC 的情况下实现(如果可能,下面的示例将展示 Cypher 和 APOC 的替代方案)。

例如,我们可以通过运行以下查询返回所有标签及其计数的流

apoc.cypher.run
CALL db.labels()
YIELD label
CALL apoc.cypher.run("MATCH (:" + label + ") RETURN count(*) AS count", {})
YIELD value
RETURN label, value.count AS count;
MATCH 子句
CALL db.labels()
YIELD label
MATCH (n:$(label))
RETURN label, count(n) AS count;
结果
标签 计数

"Person"

3

"Movie"

9

我们可以通过运行以下查询返回所有关系类型及其计数的流

apoc.cypher.run
CALL db.relationshipTypes()
YIELD relationshipType
CALL apoc.cypher.run("MATCH ()-[:" + relationshipType + "]->() RETURN count(*) AS count", {})
YIELD value
RETURN relationshipType, value.count AS count;
MATCH 子句
CALL db.relationshipTypes()
YIELD relationshipType
MATCH ()-[r:$(relationshipType)]->()
RETURN relationshipType, count(r) AS count;
结果
关系类型 计数

"ACTED_IN"

9

"DIRECTED"

1

如果我们运行的查询只返回一列,我们也可以使用 apoc.cypher.runFirstColumnManyapoc.cypher.runFirstColumnSingle

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