流式读取关系
若仅需检查关系拓扑,可以使用 gds.graph.relationships.stream 过程。若要检查已存储的关系属性值,可以使用 relationshipProperties.stream 过程。如果您在 mutate 模式下运行了多个算法并希望检索部分或全部结果,这将非常有用。
语法
CALL gds.graph.relationships.stream(
graphName: String,
relationshipTypes: String or List of Strings,
configuration: Map
)
YIELD
sourceNodeId: Integer,
targetNodeId: Integer,
relationshipType: String
| 名称 | 类型 | 可选 | 描述 |
|---|---|---|---|
graphName |
字符串 |
否 |
图在目录中存储时所使用的名称。 |
relationshipTypes |
字符串或字符串列表 |
是 |
用于流式传输图关系属性的关系类型。 |
配置 |
Map |
是 |
用于配置 streamNodeProperties 的附加参数。 |
| 名称 | 类型 | 默认 | 可选 | 描述 |
|---|---|---|---|---|
整数 |
4 [1] |
是 |
用于运行算法的并发线程数。 |
|
| 名称 | 类型 | 描述 |
|---|---|---|
sourceNodeId |
整数 |
关系源节点的 ID。 |
targetNodeId |
整数 |
关系目标节点的 ID。 |
关系类型 (relationshipType) |
整数 |
关系的类型。 |
CALL gds.graph.relationshipProperty.stream(
graphName: String,
relationshipProperty: String,
relationshipTypes: String or List of Strings,
configuration: Map
)
YIELD
sourceNodeId: Integer,
targetNodeId: Integer,
relationshipType: String,
propertyValue: Integer or Float
| 名称 | 类型 | 可选 | 描述 |
|---|---|---|---|
graphName |
字符串 |
否 |
图在目录中存储时所使用的名称。 |
relationshipProperty |
字符串 |
否 |
图中要流式传输的关系属性。 |
relationshipTypes |
字符串或字符串列表 |
是 |
用于流式传输图关系属性的关系类型。 |
配置 |
Map |
是 |
用于配置 streamNodeProperties 的附加参数。 |
| 名称 | 类型 | 默认 | 可选 | 描述 |
|---|---|---|---|---|
整数 |
4 [2] |
是 |
用于运行算法的并发线程数。 |
|
| 名称 | 类型 | 描述 |
|---|---|---|
sourceNodeId |
整数 |
关系源节点的 ID。 |
targetNodeId |
整数 |
关系目标节点的 ID。 |
关系类型 (relationshipType) |
整数 |
关系的类型。 |
propertyValue |
|
存储的属性值。 |
CALL gds.graph.relationshipProperties.stream(
graphName: String,
relationshipProperties: List of String,
relationshipTypes: String or List of Strings,
configuration: Map
)
YIELD
sourceNodeId: Integer,
targetNodeId: Integer,
relationshipType: String,
relationshipProperty: String,
propertyValue: Integer or Float
| 名称 | 类型 | 可选 | 描述 |
|---|---|---|---|
graphName |
字符串 |
否 |
图在目录中存储时所使用的名称。 |
relationshipProperties |
字符串列表 |
否 |
图中要流式传输的关系属性。 |
relationshipTypes |
字符串或字符串列表 |
是 |
用于流式传输图关系属性的关系类型。 |
配置 |
Map |
是 |
用于配置 streamNodeProperties 的附加参数。 |
| 名称 | 类型 | 默认 | 可选 | 描述 |
|---|---|---|---|---|
整数 |
4 [3] |
是 |
用于运行算法的并发线程数。 |
|
| 名称 | 类型 | 描述 |
|---|---|---|
sourceNodeId |
整数 |
关系源节点的 ID。 |
targetNodeId |
整数 |
关系目标节点的 ID。 |
关系类型 (relationshipType) |
整数 |
关系的类型。 |
relationshipProperty |
整数 |
关系属性的名称。 |
propertyValue |
|
存储的属性值。 |
示例
|
以下所有示例应在空数据库中运行。 本示例将 Cypher 投影作为标准。原生投影将在未来的版本中弃用。 |
为了演示 GDS 对节点属性的处理能力,我们将在 Neo4j 中创建一个小型图,并将其投影到我们的图目录中。
CREATE
(alice:Person {name: 'Alice'}),
(bob:Person {name: 'Bob'}),
(carol:Person {name: 'Carol'}),
(dave:Person {name: 'Dave'}),
(eve:Person {name: 'Eve'}),
(guitar:Instrument {name: 'Guitar'}),
(synth:Instrument {name: 'Synthesizer'}),
(bongos:Instrument {name: 'Bongos'}),
(trumpet:Instrument {name: 'Trumpet'}),
(alice)-[:LIKES { score: 5 }]->(guitar),
(alice)-[:LIKES { score: 4 }]->(synth),
(alice)-[:LIKES { score: 3, strength: 0.5}]->(bongos),
(bob)-[:LIKES { score: 4 }]->(guitar),
(bob)-[:LIKES { score: 5 }]->(synth),
(carol)-[:LIKES { score: 2 }]->(bongos),
(dave)-[:LIKES { score: 3 }]->(guitar),
(dave)-[:LIKES { score: 1 }]->(synth),
(dave)-[:LIKES { score: 5 }]->(bongos)
MATCH (person:Person)-[r:LIKES]->(instr:Instrument)
RETURN gds.graph.project(
'personsAndInstruments',
person,
instr,
{
sourceNodeLabels: labels(person),
targetNodeLabels: labels(instr),
relationshipType: type(r),
relationshipProperties: r { .score, strength: coalesce(r.strength, 1.0) }
}
)
CALL gds.nodeSimilarity.mutate('personsAndInstruments', { (1)
mutateRelationshipType: 'SIMILAR', (2)
mutateProperty: 'score' (3)
})
| 1 | 在 personsAndInstruments 投影图上以 mutate 模式运行 NodeSimilarity。 |
| 2 | 该算法将向投影图中添加 SIMILAR 类型的关系。 |
| 3 | 该算法将为每个添加的关系添加 score 关系属性。 |
拓扑
从命名图中流式传输关系信息的最基本情况是流式传输其拓扑结构。在下面的示例中,我们流式传输了所有关系类型的关系拓扑,以源节点、目标节点和关系类型表示。
CALL gds.graph.relationships.stream(
'personsAndInstruments' (1)
)
YIELD
sourceNodeId, targetNodeId, relationshipType
RETURN
gds.util.asNode(sourceNodeId).name as source, gds.util.asNode(targetNodeId).name as target, relationshipType
ORDER BY source ASC, target ASC
| 1 | 投影图的名称。 |
| source | target(目标) | 关系类型 (relationshipType) |
|---|---|---|
"Alice" |
"Bob" |
"SIMILAR" |
"Alice" |
"Bongos" |
"LIKES" |
"Alice" |
"Carol" |
"SIMILAR" |
"Alice" |
"Dave" |
"SIMILAR" |
"Alice" |
"Guitar" |
"LIKES" |
"Alice" |
"Synthesizer" |
"LIKES" |
"Bob" |
"Alice" |
"SIMILAR" |
"Bob" |
"Dave" |
"SIMILAR" |
"Bob" |
"Guitar" |
"LIKES" |
"Bob" |
"Synthesizer" |
"LIKES" |
"Carol" |
"Alice" |
"SIMILAR" |
"Carol" |
"Bongos" |
"LIKES" |
"Carol" |
"Dave" |
"SIMILAR" |
"Dave" |
"Alice" |
"SIMILAR" |
"Dave" |
"Bob" |
"SIMILAR" |
"Dave" |
"Bongos" |
"LIKES" |
"Dave" |
"Carol" |
"SIMILAR" |
"Dave" |
"Guitar" |
"LIKES" |
"Dave" |
"Synthesizer" |
"LIKES" |
从结果中可以看出,我们得到了两种关系类型(SIMILAR 和 LIKES)。我们可以进一步过滤要流式传输的关系类型。这可以通过向过程传递第二个参数来实现,如下个示例所示。
CALL gds.graph.relationships.stream(
'personsAndInstruments', (1)
['SIMILAR'] (2)
)
YIELD
sourceNodeId, targetNodeId, relationshipType
RETURN
gds.util.asNode(sourceNodeId).name as source, gds.util.asNode(targetNodeId).name as target, relationshipType
ORDER BY source ASC, target ASC
| 1 | 投影图的名称。 |
| 2 | 我们要流式传输的关系类型列表,仅使用我们需要的类型。 |
| source | target(目标) | 关系类型 (relationshipType) |
|---|---|---|
"Alice" |
"Bob" |
"SIMILAR" |
"Alice" |
"Carol" |
"SIMILAR" |
"Alice" |
"Dave" |
"SIMILAR" |
"Bob" |
"Alice" |
"SIMILAR" |
"Bob" |
"Dave" |
"SIMILAR" |
"Carol" |
"Alice" |
"SIMILAR" |
"Carol" |
"Dave" |
"SIMILAR" |
"Dave" |
"Alice" |
"SIMILAR" |
"Dave" |
"Bob" |
"SIMILAR" |
"Dave" |
"Carol" |
"SIMILAR" |
单个属性
从命名图中流式传输关系属性的最基本情况是单个属性。在下面的示例中,我们流式传输 score 关系属性。
CALL gds.graph.relationshipProperty.stream(
'personsAndInstruments', (1)
'score' (2)
)
YIELD
sourceNodeId, targetNodeId, relationshipType, propertyValue
RETURN
gds.util.asNode(sourceNodeId).name as source, gds.util.asNode(targetNodeId).name as target, relationshipType, propertyValue
ORDER BY source ASC, target ASC
| 1 | 投影图的名称。 |
| 2 | 我们要流式传输出来的属性。 |
| source | target(目标) | 关系类型 (relationshipType) | propertyValue |
|---|---|---|---|
"Alice" |
"Bob" |
"SIMILAR" |
0.6666666666666666 |
"Alice" |
"Bongos" |
"LIKES" |
3.0 |
"Alice" |
"Carol" |
"SIMILAR" |
0.3333333333333333 |
"Alice" |
"Dave" |
"SIMILAR" |
1.0 |
"Alice" |
"Guitar" |
"LIKES" |
5.0 |
"Alice" |
"Synthesizer" |
"LIKES" |
4.0 |
"Bob" |
"Alice" |
"SIMILAR" |
0.6666666666666666 |
"Bob" |
"Dave" |
"SIMILAR" |
0.6666666666666666 |
"Bob" |
"Guitar" |
"LIKES" |
4.0 |
"Bob" |
"Synthesizer" |
"LIKES" |
5.0 |
"Carol" |
"Alice" |
"SIMILAR" |
0.3333333333333333 |
"Carol" |
"Bongos" |
"LIKES" |
2.0 |
"Carol" |
"Dave" |
"SIMILAR" |
0.3333333333333333 |
"Dave" |
"Alice" |
"SIMILAR" |
1.0 |
"Dave" |
"Bob" |
"SIMILAR" |
0.6666666666666666 |
"Dave" |
"Bongos" |
"LIKES" |
5.0 |
"Dave" |
"Carol" |
"SIMILAR" |
0.3333333333333333 |
"Dave" |
"Guitar" |
"LIKES" |
3.0 |
"Dave" |
"Synthesizer" |
"LIKES" |
1.0 |
从结果中可以看出,我们得到了两种具有 score 关系属性的关系类型(SIMILAR 和 LIKES)。我们可以进一步过滤要流式传输的关系类型,这在下一个示例中进行了演示。
CALL gds.graph.relationshipProperty.stream(
'personsAndInstruments', (1)
'score', (2)
['SIMILAR'] (3)
)
YIELD
sourceNodeId, targetNodeId, relationshipType, propertyValue
RETURN
gds.util.asNode(sourceNodeId).name as source, gds.util.asNode(targetNodeId).name as target, relationshipType, propertyValue
ORDER BY source ASC, target ASC
| 1 | 投影图的名称。 |
| 2 | 我们要流式传输出来的属性。 |
| 3 | 我们要流式传输其属性的关系类型列表,仅使用我们需要的类型。 |
| source | target(目标) | 关系类型 (relationshipType) | propertyValue |
|---|---|---|---|
"Alice" |
"Bob" |
"SIMILAR" |
0.6666666666666666 |
"Alice" |
"Carol" |
"SIMILAR" |
0.3333333333333333 |
"Alice" |
"Dave" |
"SIMILAR" |
1.0 |
"Bob" |
"Alice" |
"SIMILAR" |
0.6666666666666666 |
"Bob" |
"Dave" |
"SIMILAR" |
0.6666666666666666 |
"Carol" |
"Alice" |
"SIMILAR" |
0.3333333333333333 |
"Carol" |
"Dave" |
"SIMILAR" |
0.3333333333333333 |
"Dave" |
"Alice" |
"SIMILAR" |
1.0 |
"Dave" |
"Bob" |
"SIMILAR" |
0.6666666666666666 |
"Dave" |
"Carol" |
"SIMILAR" |
0.3333333333333333 |
多个属性
同时流式传输多个关系属性也是可能的。
CALL gds.graph.relationshipProperties.stream(
'personsAndInstruments', (1)
['score', 'strength'], (2)
['LIKES'] (3)
)
YIELD
sourceNodeId, targetNodeId, relationshipType, relationshipProperty, propertyValue
RETURN
gds.util.asNode(sourceNodeId).name as source, gds.util.asNode(targetNodeId).name as target, relationshipType, relationshipProperty, propertyValue
ORDER BY source ASC, target ASC
| 1 | 投影图的名称。 |
| 2 | 我们要流式传输出来的属性列表,允许我们流式传输多个属性。 |
| 3 | 我们要流式传输其属性的关系类型列表,仅使用我们需要的类型。 |
| source | target(目标) | 关系类型 (relationshipType) | relationshipProperty | propertyValue |
|---|---|---|---|---|
"Alice" |
"Bongos" |
"LIKES" |
"score" |
3.0 |
"Alice" |
"Bongos" |
"LIKES" |
"strength" |
0.5 |
"Alice" |
"Guitar" |
"LIKES" |
"score" |
5.0 |
"Alice" |
"Guitar" |
"LIKES" |
"strength" |
1.0 |
"Alice" |
"Synthesizer" |
"LIKES" |
"score" |
4.0 |
"Alice" |
"Synthesizer" |
"LIKES" |
"strength" |
1.0 |
"Bob" |
"Guitar" |
"LIKES" |
"score" |
4.0 |
"Bob" |
"Guitar" |
"LIKES" |
"strength" |
1.0 |
"Bob" |
"Synthesizer" |
"LIKES" |
"score" |
5.0 |
"Bob" |
"Synthesizer" |
"LIKES" |
"strength" |
1.0 |
"Carol" |
"Bongos" |
"LIKES" |
"score" |
2.0 |
"Carol" |
"Bongos" |
"LIKES" |
"strength" |
1.0 |
"Dave" |
"Bongos" |
"LIKES" |
"score" |
5.0 |
"Dave" |
"Bongos" |
"LIKES" |
"strength" |
1.0 |
"Dave" |
"Guitar" |
"LIKES" |
"score" |
3.0 |
"Dave" |
"Guitar" |
"LIKES" |
"strength" |
1.0 |
"Dave" |
"Synthesizer" |
"LIKES" |
"score" |
1.0 |
"Dave" |
"Synthesizer" |
"LIKES" |
"strength" |
1.0 |
多个关系类型
与多个关系属性类似,我们可以流式传输多个关系类型的属性。
CALL gds.graph.relationshipProperties.stream(
'personsAndInstruments', (1)
['score'], (2)
['LIKES', 'SIMILAR'] (3)
)
YIELD
sourceNodeId, targetNodeId, relationshipType, relationshipProperty, propertyValue
RETURN
gds.util.asNode(sourceNodeId).name as source, (4)
gds.util.asNode(targetNodeId).name as target, (5)
relationshipType,
relationshipProperty,
propertyValue
ORDER BY source ASC, target ASC
| 1 | 投影图的名称。 |
| 2 | 我们要流式传输出来的属性列表,允许我们流式传输多个属性。 |
| 3 | 我们要流式传输其属性的关系类型列表,仅使用我们需要的类型。 |
| 4 | 返回源节点的 name。 |
| 5 | 返回目标节点的 name。 |
| source | target(目标) | 关系类型 (relationshipType) | relationshipProperty | propertyValue |
|---|---|---|---|---|
"Alice" |
"Bob" |
"SIMILAR" |
"score" |
0.6666666666666666 |
"Alice" |
"Bongos" |
"LIKES" |
"score" |
3.0 |
"Alice" |
"Carol" |
"SIMILAR" |
"score" |
0.3333333333333333 |
"Alice" |
"Dave" |
"SIMILAR" |
"score" |
1.0 |
"Alice" |
"Guitar" |
"LIKES" |
"score" |
5.0 |
"Alice" |
"Synthesizer" |
"LIKES" |
"score" |
4.0 |
"Bob" |
"Alice" |
"SIMILAR" |
"score" |
0.6666666666666666 |
"Bob" |
"Dave" |
"SIMILAR" |
"score" |
0.6666666666666666 |
"Bob" |
"Guitar" |
"LIKES" |
"score" |
4.0 |
"Bob" |
"Synthesizer" |
"LIKES" |
"score" |
5.0 |
"Carol" |
"Alice" |
"SIMILAR" |
"score" |
0.3333333333333333 |
"Carol" |
"Bongos" |
"LIKES" |
"score" |
2.0 |
"Carol" |
"Dave" |
"SIMILAR" |
"score" |
0.3333333333333333 |
"Dave" |
"Alice" |
"SIMILAR" |
"score" |
1.0 |
"Dave" |
"Bob" |
"SIMILAR" |
"score" |
0.6666666666666666 |
"Dave" |
"Bongos" |
"LIKES" |
"score" |
5.0 |
"Dave" |
"Carol" |
"SIMILAR" |
"score" |
0.3333333333333333 |
"Dave" |
"Guitar" |
"LIKES" |
"score" |
3.0 |
"Dave" |
"Synthesizer" |
"LIKES" |
"score" |
1.0 |
| 我们要流式传输的属性必须存在于每个指定的关系类型中。 |