流式读取节点
若要检查 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
| 名称 | 类型 | 可选 | 描述 |
|---|---|---|---|
graphName |
字符串 |
否 |
图在目录中存储时所使用的名称。 |
nodeProperties |
字符串 |
否 |
图中要进行流式传输的节点属性。 |
nodeLabels |
字符串或字符串列表 |
是 |
要流式传输其节点属性的节点标签(针对图)。 |
配置 |
Map |
是 |
用于配置 streamNodeProperties 的附加参数。 |
| 名称 | 类型 | 默认 | 可选 | 描述 |
|---|---|---|---|---|
整数 |
4 [1] |
是 |
用于运行算法的并发线程数。 |
|
listNodeLabels |
布尔值 |
false |
是 |
是否为每个节点返回一个节点标签列表。 |
| 名称 | 类型 | 描述 |
|---|---|---|
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
| 名称 | 类型 | 可选 | 描述 |
|---|---|---|---|
graphName |
字符串 |
否 |
图在目录中存储时所使用的名称。 |
nodeProperties |
字符串或字符串列表 |
否 |
图中要进行流式传输的节点属性。 |
nodeLabels |
字符串或字符串列表 |
是 |
要流式传输其节点属性的节点标签(针对图)。 |
配置 |
Map |
是 |
用于配置 streamNodeProperties 的附加参数。 |
| 名称 | 类型 | 默认 | 可选 | 描述 |
|---|---|---|---|---|
整数 |
4 [2] |
是 |
用于运行算法的并发线程数。 |
|
listNodeLabels |
布尔值 |
false |
是 |
是否为每个节点返回一个节点标签列表。 |
| 名称 | 类型 | 描述 |
|---|---|---|
nodeId |
整数 |
节点的 ID。 |
nodeProperty |
字符串 |
节点属性的名称。 |
propertyValue |
|
存储的属性值。 |
nodeLabels |
字符串列表 |
节点的节点标签。 |
示例
|
以下所有示例应在空数据库中运行。 本示例将 Cypher 投影作为标准。原生投影将在未来的版本中弃用。 |
为了演示 GDS 流式传输节点属性的功能,我们将创建一个小型社交网络图,并将其投影到我们的图目录中。
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
| 名称 (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
| 名称 (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
| 名称 (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
| 名称 (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
)
| 名称 | 类型 | 可选 | 描述 |
|---|---|---|---|
graphName |
字符串 |
否 |
目录中图的名称。 |
nodeId |
整数 |
否 |
节点的 ID。 |
属性键 (propertyKey) |
字符串 |
否 |
要访问的属性键。 |
nodeLabel |
字符串 |
是 |
节点上的标签。 |
如果给定节点缺少该属性值,则返回 null。
示例
我们使用上面介绍的具有 score 属性的 socialGraph。
MATCH (florentin:Person {name: 'Florentin'})
RETURN
florentin.name AS name,
gds.util.nodeProperty('socialGraph', florentin, 'score') AS score
| 名称 (name) | score |
|---|---|
"Florentin" |
2.0 |