流式传输节点

若要检查 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 [1]

运行算法所使用的并发线程数。

listNodeLabels

布尔值

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

1. 在 GDS 会话中,默认值是可用处理器的数量

表 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 [2]

运行算法所使用的并发线程数。

listNodeLabels

布尔值

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

2. 在 GDS 会话中,默认值是可用处理器的数量

表 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. 结果
名称 分数

"Florentin"

2.0

"Adam"

1.0

"Veselin"

0.0

"The Hobbit"

0.0

上述示例要求所有给定属性至少存在于一个节点投射上,并且这些属性将为所有此类投射进行流式传输。

节点标签

该过程可以配置为仅流式传输特定节点标签的属性。

流式传输 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. 结果
名称 分数

"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. 结果
名称 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. 结果
名称 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

示例

我们使用上面介绍的带有 score 属性的 socialGraph

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

"Florentin"

2.0

© . All rights reserved.