创建数据

APOC 库包含增强 Neo4j 写入功能的过程。其中许多过程支持动态数据创建,例如动态添加节点标签。

要了解如何使用 Neo4j 用户创新高级总监 Michael Hunger 提供的 APOC 库动态创建节点和关系,请观看此视频

动态创建数据的函数

限定名称 类型

apoc.create.node(label LIST<STRING>, props MAP<STRING, ANY>) - 使用给定的动态标签创建 `NODE`。

过程

apoc.create.nodes(label LIST<STRING>, props LIST<MAP<STRING, ANY>>) - 使用给定的动态标签创建 `NODE` 值。

过程

apoc.create.relationship(from NODE, relType STRING, props MAP<STRING, ANY>, to NODE) - 使用给定的动态关系类型创建 `RELATIONSHIP`。

过程

apoc.create.removeLabels(nodes ANY, label LIST<STRING>) - 从给定的 `NODE` 值中移除给定的标签。

过程

apoc.create.removeProperties(nodes ANY, keys LIST<STRING>) - 从给定的 `NODE` 值中移除给定的属性。

过程

apoc.create.removeRelProperties(rels ANY, keys LIST<STRING>) - 从给定的 `RELATIONSHIP` 值中移除给定的属性。

过程

apoc.create.setProperties(nodes ANY, keys LIST<STRING>, values LIST<ANY>) - 将给定的属性设置到给定的 `NODE` 值。

过程

apoc.create.setProperty(nodes ANY, key LIST<STRING>, value ANY) - 将给定的属性设置到给定的 `NODE` 值。

过程

apoc.create.setRelProperties(rels ANY, keys LIST<STRING>, values LIST<ANY>) - 在 `RELATIONSHIP` 值上设置给定的属性。

过程

apoc.create.setRelProperty(rels ANY, key STRING, value ANY) - 在 `RELATIONSHIP` 值上设置给定的属性。

过程

apoc.nodes.link(nodes LIST<NODE>, type STRING, config MAP<STRING, ANY>) - 创建一个由给定 `RELATIONSHIP` 类型连接的给定 `NODE` 值的链表。

过程

apoc.merge.relationship(startNode NODE, relType STRING, identProps MAP<STRING, ANY>, onCreateProps MAP<STRING, ANY>, endNode NODE, onMatchProps MAP<STRING, ANY>) - 使用给定的动态类型/属性合并给定的 `RELATIONSHIP` 值。

过程

apoc.merge.nodeWithStats(label LIST<STRING>, identProps MAP<STRING, ANY>, onCreateProps MAP<STRING, ANY>, onMatchProps MAP<STRING, ANY>) - 使用给定的动态标签合并给定的 `NODE` 值。在结果中提供查询统计信息。

过程

apoc.merge.nodeWithStats.eager(label LIST<STRING>, identProps MAP<STRING, ANY>, onCreateProps MAP<STRING, ANY>, onMatchProps MAP<STRING, ANY>) - 使用给定的动态标签急切地合并给定的 `NODE` 值。在结果中提供查询统计信息。

过程

apoc.merge.relationshipWithStats(startNode NODE, relType STRING, identProps MAP<STRING, ANY>, onCreateProps MAP<STRING, ANY>, endNode NODE, onMatchProps MAP<STRING, ANY>) - 使用给定的动态类型/属性合并给定的 `RELATIONSHIP` 值。在结果中提供查询统计信息。

过程

apoc.merge.relationshipWithStats.eager(startNode NODE, relType STRING, identProps MAP<STRING, ANY>, onCreateProps MAP<STRING, ANY>, endNode NODE, onMatchProps MAP<STRING, ANY>) - 使用给定的动态类型/属性急切地合并给定的 `RELATIONSHIP` 值。在结果中提供查询统计信息。

过程

示例

以下示例将进一步解释这些过程。

移除节点标签

只要标签是硬编码的,Cypher 就支持删除标签。如果标签是动态指定的,则可以使用 `apoc.create.removeLabels` 过程。

以下创建一个人物样本图
CREATE (jennifer:Person:US {name: "Jennifer", community: 1, partition: 4})
CREATE (karin:Person:US {name: "Karin", community: 4, partition: 2})
CREATE (mark:Person:UK {name: "Mark", community: 3, partition: 3})
以下从所有节点中删除除 `Person` 之外的所有标签
CALL db.labels()
YIELD label WHERE label <> "Person"
WITH collect(label) AS labels
MATCH (p:Person)
WITH collect(p) AS people, labels
CALL apoc.create.removeLabels(people, labels)
YIELD node
RETURN node, labels(node) AS labels
表 1. 结果
节点 标签

(:Person {name: "Jennifer", partition: 4, community: 1})

["Person"]

(:Person {name: "Karin", partition: 2, community: 4})

["Person"]

(:Person {name: "Mark", partition: 3, community: 3})

["Person"]

设置节点和关系属性

只要属性名称是硬编码的,Cypher 就支持设置属性。如果属性名称是动态指定的,请使用 `apoc.create.setProperties` 和 `apoc.create.setRelProperties` 过程。

以下创建一个人物样本图
CREATE (jennifer:Person {name: "Jennifer", community: 1, partition: 4})
CREATE (karin:Person {name: "Karin", community: 4, partition: 2})
CREATE (elaine:Person {name: "Elaine", community: 3, partition: 3})
MERGE (jennifer)-[:FRIENDS {since: datetime("2019-06-01")}]-(karin)
MERGE (jennifer)-[:FRIENDS {since: datetime("2019-05-04")}]-(elaine)
以下复制 `Person` 节点上的所有节点属性
MATCH (p:Person)
WITH p, keys(p) AS keys
CALL apoc.create.setProperties(p,[k in keys | k + "Copy"], [k in keys | p[k]])
YIELD node
RETURN node
表 2. 结果
节点

{"name":"Jennifer","partition":4,"community":1,"nameCopy":"Jennifer","partitionCopy":4,"communityCopy":1}

{"name":"Karin","partition":2,"community":4,"nameCopy":"Karin","partitionCopy":2,"communityCopy":4}

{"name":"Mark","partition":3,"community":3,"nameCopy":"Mark","partitionCopy":3,"communityCopy":3}

以下复制所有关系属性
MATCH (:Person)-[friends:FRIENDS]->(:Person)
WITH friends, keys(friends) AS keys
CALL apoc.create.setRelProperties(friends,[k in keys | k + "Copy"], [k in keys | friends[k]])
YIELD rel
RETURN startNode(rel) AS start, rel, endNode(rel) AS end
表 3. 结果
开始 关系 结束

{ "name": "Jennifer", "partition": 4, "community": 1, "nameCopy": "Jennifer", "partitionCopy": 4, "communityCopy": 1 }

{ "sinceCopy": "2019-05-04T00:00:00Z", "since": "2019-05-04T00:00:00Z" }

{ "name": "Elaine", "partition": 3, "community": 3, "nameCopy": "Elaine", "partitionCopy": 3, "communityCopy": 3 }

{ "name": "Jennifer", "partition": 4, "community": 1, "nameCopy": "Jennifer", "partitionCopy": 4, "communityCopy": 1 }

{ "sinceCopy": "2019-06-01T00:00:00Z", "since": "2019-06-01T00:00:00Z" }

{ "name": "Karin", "partition": 2, "community": 4, "nameCopy": "Karin", "partitionCopy": 2, "communityCopy": 4 }

移除节点和关系属性

只要属性名称是硬编码的,Cypher 就支持删除属性。如果属性名称是动态指定的,请使用 `apoc.create.removeProperties` 和 `apoc.create.removeRelProperties` 过程。

以下创建一个人物样本图
CREATE (jennifer:Person {name: "Jennifer", community: 1, partition: 4})
CREATE (karin:Person {name: "Karin", community: 4, partition: 2})
CREATE (elaine:Person {name: "Elaine", community: 3, partition: 3})
MERGE (jennifer)-[:FRIENDS {since: datetime("2019-06-01")}]-(karin)
MERGE (jennifer)-[:FRIENDS {since: datetime("2019-05-04")}]-(elaine)
以下从 `Person` 节点中删除除 `name` 之外的所有属性
CALL db.propertyKeys()
YIELD propertyKey WHERE propertyKey <> "name"
WITH collect(propertyKey) AS propertyKeys
MATCH (p:Person)
WITH collect(p) AS nodes, propertyKeys
CALL apoc.create.removeProperties(nodes, propertyKeys)
YIELD node
RETURN node
表 4. 结果
节点

{"name":"Jennifer"}

{"name":"Karin"}

{"name":"Elaine"}

以下从所有关系中删除属性
CALL db.propertyKeys()
YIELD propertyKey
WITH collect(propertyKey) AS propertyKeys
MATCH (:Person)-[friends:FRIENDS]->(:Person)
WITH collect(friends) AS friendsRels, propertyKeys
CALL apoc.create.removeRelProperties(friendsRels, propertyKeys)
YIELD rel
RETURN startNode(rel) AS start, rel, endNode(rel) AS end
表 5. 结果
开始 关系 结束

{"name":"Jennifer"}

{}

{"name":"Elaine"}

{"name":"Jennifer"}

{}

{"name":"Karin"}

链表

链表

apoc.nodes.link 可用于创建节点链表。
MATCH (e:Event)
WITH e ORDER BY e.date
WITH collect(e) AS events
CALL apoc.nodes.link(events, "NEXT")
RETURN count(*);

以下创建一个节点链表

apoc.nodes.link 可用于创建节点链表。
MATCH (e:Event)
WITH e ORDER BY e.date
WITH collect(e) AS events, count(e) as size
CALL {
    WITH events, size
    UNWIND range(0, size - 2) as i
    WITH events[i] as a, events[i+1] as b
    MERGE (a)-[:NEXT]->(b)
}
RETURN events
linked list events
© . All rights reserved.