apoc.cypher.run

此过程不建议在多线程中运行,因此并行运行时(Parallel runtime)不支持该过程。有关更多信息,请参阅 Cypher 手册 → 并行运行时

详细信息

语法

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

描述

执行一条带有给定参数的、动态构建的只读 Cypher 语句。

输入参数

名称

类型

描述

statement

STRING

要执行的 Cypher 语句。

params

MAP

给定 Cypher 语句的参数。

返回参数

名称

类型

描述

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;
结果
标签 (label) 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;
结果
关系类型 (relationshipType) count

"ACTED_IN"

9

"DIRECTED"

1

如果我们要运行的查询仅返回单个列,我们也可以使用 apoc.cypher.runFirstColumnManyapoc.cypher.runFirstColumnSingle