流节点

要检查 GDS 图中节点上的属性值,可以使用 gds.graph.nodeProperties.stream 过程。 如果我们在 mutate 模式下运行了多个算法,并且想要检索部分或所有结果,这将非常有用。

语法

不同节点流选项的语法描述
CALL gds.graph.nodeProperty.stream(
    graphName: String,
    nodeProperties: String,
    nodeLabels: String or List of Strings,
    configuration: Map
)
YIELD
    nodeId: Integer,
    propertyValue: Integer or Float or List of Integer or List of Float,
    nodeLabels: List of Strings
表 1. 参数
名称 类型 可选 描述

graphName

字符串

图在目录中存储的名称。

nodeProperties

字符串

图中要流的节点属性。

nodeLabels

字符串或字符串列表

要为图流节点属性的节点标签。

configuration

映射

配置 streamNodeProperties 的其他参数。

表 2. 配置
名称 类型 默认 描述

concurrency

整数

4

并发线程数。 请注意,此过程始终以单线程运行。

listNodeLabels

布尔值

false

是否返回每个节点的节点标签列表。

表 3. 结果
名称 类型 描述

nodeId

整数

节点的 ID。

propertyValue

  • 整数

  • 浮点数

  • 整数列表

  • 浮点数列表

存储的属性值。

nodeLabels

字符串列表

节点的节点标签。

CALL gds.graph.nodeProperties.stream(
    graphName: String,
    nodeProperties: String or List of Strings,
    nodeLabels: String or List of Strings,
    configuration: Map
)
YIELD
    nodeId: Integer,
    nodeProperty: String,
    propertyValue: Integer or Float or List of Integer or List of Float,
    nodeLabels: List of Strings
表 4. 参数
名称 类型 可选 描述

graphName

字符串

图在目录中存储的名称。

nodeProperties

字符串或字符串列表

图中要流的节点属性。

nodeLabels

字符串或字符串列表

要为图流节点属性的节点标签。

configuration

映射

配置 streamNodeProperties 的其他参数。

表 5. 配置
名称 类型 默认 描述

concurrency

整数

4

并发线程数。 请注意,此过程始终以单线程运行。

listNodeLabels

布尔值

false

是否返回每个节点的节点标签列表。

表 6. 结果
名称 类型 描述

nodeId

整数

节点的 ID。

nodeProperty

字符串

节点属性的名称。

propertyValue

  • 整数

  • 浮点数

  • 整数列表

  • 浮点数列表

存储的属性值。

nodeLabels

字符串列表

节点的节点标签。

示例

以下所有示例都应在空数据库中运行。

这些示例使用 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'})

我们可以将存储在命名内存图中的节点属性流回给用户。 如果我们在 mutate 模式下运行了多个算法,并且想要检索部分或所有结果,这将非常有用。 这与算法 stream 执行模式类似,但允许更细粒度地控制操作。

单个属性

在下面,我们将流先前计算的分数 score

score 节点属性
CALL gds.graph.nodeProperty.stream('socialGraph', 'score')
YIELD nodeId, propertyValue
RETURN gds.util.asNode(nodeId).name AS name, propertyValue AS score
ORDER BY score DESC
表 7. 结果
name score

"Florentin"

2.0

"Adam"

1.0

"Veselin"

0.0

"The Hobbit"

0.0

上面的示例要求所有给定的属性都存在于至少一个节点投影中,并且将为所有这些投影流属性。

NodeLabels

可以配置过程以仅为特定节点标签流属性。

Person 节点流 score 属性
CALL gds.graph.nodeProperty.stream('socialGraph', 'score', ['Person'])
YIELD nodeId, propertyValue
RETURN gds.util.asNode(nodeId).name AS name, propertyValue AS score
ORDER BY score DESC
表 8. 结果
name score

"Florentin"

2.0

"Adam"

1.0

"Veselin"

0.0

要求所有指定的节点标签都具有节点属性。

多个属性

我们也可以一次流多个属性。

流多个节点属性
CALL gds.graph.nodeProperties.stream('socialGraph', ['score', 'age'])
YIELD nodeId, nodeProperty, propertyValue
RETURN gds.util.asNode(nodeId).name AS name, nodeProperty, propertyValue
ORDER BY name, nodeProperty
表 9. 结果
name nodeProperty propertyValue

"Adam"

"age"

18

"Adam"

"score"

1.0

"Florentin"

"age"

16

"Florentin"

"score"

2.0

"Veselin"

"age"

20

"Veselin"

"score"

0.0

当流式传输多个节点属性时,每个属性的名称都包含在结果中。这会增加一些开销,因为每个属性名称必须在结果中的每个节点重复,但这是为了区分属性所必需的。

此外,当流式传输一个或多个节点属性时,我们还可以通过设置listNodeLabels配置选项来返回每个单独节点的节点标签。

使用标签流式传输多个节点属性
CALL gds.graph.nodeProperties.stream(
  'socialGraph',
  ['score'],
  ['*'],
  { listNodeLabels: true }
)
YIELD nodeId, nodeProperty, propertyValue, nodeLabels
RETURN
  gds.util.asNode(nodeId).name AS name,
  nodeProperty,
  propertyValue,
  nodeLabels
表 10. 结果
name nodeProperty propertyValue nodeLabels

"Florentin"

"score"

2.0

["Person"]

"Adam"

"score"

1.0

["Person"]

"Veselin"

"score"

0.0

["Person"]

"The Hobbit"

"score"

0.0

["Book"]

单个节点属性访问

GDS 提供了一个函数,用于直接在 Cypher 查询中访问内存中图的特定节点的属性值。

语法

gds.util.nodeProperty(
  graphName: String,
  nodeId: Node or Integer,
  propertyKey: String,
  nodeLabel: String
)
表 11. 参数
名称 类型 可选 描述

graphName

字符串

目录中图的名称。

nodeId

整数

节点的 ID。

propertyKey

字符串

要访问的属性键。

nodeLabel

字符串

节点上的标签。

如果给定节点缺少属性值,则返回null

示例

我们使用socialGraph,其中包含上面介绍的属性score

访问 Florentin 的节点属性
MATCH (florentin:Person {name: 'Florentin'})
RETURN
  florentin.name AS name,
  gds.util.nodeProperty('socialGraph', florentin, 'score') AS score
表 12. 结果
name score

"Florentin"

2.0