流式传输节点
若要检查 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 |
字符串或字符串列表 |
是 |
要流式传输图的节点属性的节点标签。 |
configuration |
映射 |
是 |
配置 streamNodeProperties 的附加参数。 |
名称 | 类型 | 默认值 | 可选 | 描述 |
---|---|---|---|---|
整数 |
4 [1] |
是 |
运行算法所使用的并发线程数。 |
|
listNodeLabels |
布尔值 |
否 |
是 |
是否为每个节点返回一个节点标签列表。 |
名称 | 类型 | 描述 |
---|---|---|
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 |
字符串或字符串列表 |
是 |
要流式传输图的节点属性的节点标签。 |
configuration |
映射 |
是 |
配置 streamNodeProperties 的附加参数。 |
名称 | 类型 | 默认值 | 可选 | 描述 |
---|---|---|---|---|
整数 |
4 [2] |
是 |
运行算法所使用的并发线程数。 |
|
listNodeLabels |
布尔值 |
否 |
是 |
是否为每个节点返回一个节点标签列表。 |
名称 | 类型 | 描述 |
---|---|---|
nodeId |
整数 |
节点的 ID。 |
nodeProperty |
字符串 |
节点属性的名称。 |
propertyValue |
|
存储的属性值。 |
nodeLabels |
字符串列表 |
节点的节点标签。 |
示例
以下所有示例都应在一个空数据库中运行。 示例中通常使用 Cypher 投射。本机投射将在未来的版本中弃用。 |
为了演示 GDS 流式传输节点属性的能力,我们将在 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
名称 | 分数 |
---|---|
"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
名称 | 分数 |
---|---|
"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
名称 | 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
名称 | nodeProperty | propertyValue | nodeLabels |
---|---|---|---|
"Florentin" |
"score" |
2.0 |
["Person"] |
"Adam" |
"score" |
1.0 |
["Person"] |
"Veselin" |
"score" |
0.0 |
["Person"] |
"The Hobbit" |
"score" |
0.0 |
["Book"] |