添加节点标签
在图创建之后添加节点标签,可用于根据算法结果等创建标签。与现有标签一样,它们允许在后续操作中对图进行过滤。
语法
CALL gds.graph.nodeLabel.mutate(
graphName: String,
nodeLabel: String,
configuration: Map
)
YIELD
mutateMillis: Integer,
graphName: String,
nodeLabel: String,
nodeLabelsWritten: Integer,
nodeCount: Integer,
configuration: Map
| 名称 | 类型 | 可选 | 描述 |
|---|---|---|---|
graphName |
字符串 |
否 |
图在目录中存储时所使用的名称。 |
nodeLabel |
字符串 |
否 |
要写回的节点标签。 |
配置 |
Map |
是 |
用于配置 writeNodeProperties 的附加参数。 |
| 名称 | 类型 | 默认 | 可选 | 描述 |
|---|---|---|---|---|
nodeFilter (节点过滤器) |
字符串 |
不适用 |
否 |
用于过滤输入图中节点的 Cypher 谓词。请参阅 投影子图。 |
整数 |
4 [1] |
是 |
用于运行算法的并发线程数。 |
|
布尔值 |
true |
是 |
如果禁用,进度百分比将不会被记录。 |
|
| 名称 | 类型 | 描述 |
|---|---|---|
mutateMillis |
整数 |
将结果数据写回内存图所需的毫秒数。 |
nodeLabel |
字符串 |
添加到内存图中的标签名称。 |
nodeLabelsWritten |
整数 |
已写入的节点标签数量。 |
graphName |
字符串 |
存储在目录中的图的名称。 |
nodeCount |
整数 |
图中节点的总数。 |
配置 |
Map |
运行该过程所使用的配置。 |
示例
|
以下所有示例应在空数据库中运行。 本示例将 Cypher 投影作为标准。原生投影将在未来的版本中弃用。 |
为了演示 GDS 在节点属性上的功能,我们将在 Neo4j 中创建一个小型社交网络图,并将其投影到我们的图目录中。
以下 Cypher 语句将在 Neo4j 数据库中创建示例图:
CREATE
(florentin:Person { name: 'Florentin', age: 16 }),
(adam:Person { name: 'Adam', age: 18 }),
(veselin:Person { name: 'Veselin', age: 20 }),
(hobbit:Book { name: 'The Hobbit', numberOfPages: 310 }),
(florentin)-[:KNOWS { since: 2010 }]->(adam),
(florentin)-[:KNOWS { since: 2018 }]->(veselin),
(adam)-[:READ]->(hobbit)
投影小型社交网络图
MATCH (n:Person)-[r:KNOWS|READ]->(m:Person|Book)
RETURN gds.graph.project('socialGraph', n, m,
{
sourceNodeLabels: labels(n),
targetNodeLabels: labels(m),
sourceNodeProperties: n { .age },
targetNodeProperties: CASE WHEN m:Person THEN m { .age } ELSE {} END,
relationshipType: type(r)
}
)
计算社交图中的度中心性
CALL gds.degree.mutate('socialGraph', {mutateProperty: 'score'})
若要通过为分值高于 0 的节点添加新节点标签来变异(mutate)内存图,我们使用以下查询
向内存图中添加
Reader 节点标签CALL gds.graph.nodeLabel.mutate('socialGraph', 'Reader', { nodeFilter: 'n.score > 0.0' })
YIELD graphName, nodeLabel, nodeLabelsWritten, nodeCount
| graphName | nodeLabel | nodeLabelsWritten | nodeCount |
|---|---|---|---|
"socialGraph" |
"Reader" |
2 |
4 |
正如我们从结果中看到的那样,有两个节点匹配了指定的过滤器,它们被赋予了 Reader 节点标签。我们可以通过流式传输 Reader 节点标签的 score 属性来检查结果,可以使用以下查询来实现
流式传输
Reader 节点的 score 属性CALL gds.graph.nodeProperty.stream('socialGraph', 'score', ['Reader'])
YIELD nodeId, propertyValue
RETURN gds.util.asNode(nodeId).name AS name, propertyValue AS score
ORDER BY score DESC
| 名称 (name) | score |
|---|---|
"Florentin" |
2.0 |
"Adam" |
1.0 |
我们可以看到 Veselin 没有被标记为 Reader,因为该节点的 score 属性为 0。