流关系
要仅检查关系拓扑结构,可以使用 `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 |
字符串或字符串列表 |
是 |
要为图流关系属性的关系类型。 |
configuration |
映射 |
是 |
配置 streamNodeProperties 的其他参数。 |
名称 | 类型 | 默认值 | 描述 |
---|---|---|---|
concurrency |
整数 |
4 |
并发线程数。请注意,此过程始终以单线程运行。 |
名称 | 类型 | 描述 |
---|---|---|
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 |
字符串或字符串列表 |
是 |
要为图流关系属性的关系类型。 |
configuration |
映射 |
是 |
配置 streamNodeProperties 的其他参数。 |
名称 | 类型 | 默认值 | 描述 |
---|---|---|---|
concurrency |
整数 |
4 |
并发线程数。请注意,此过程始终以单线程运行。 |
名称 | 类型 | 描述 |
---|---|---|
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 |
字符串或字符串列表 |
是 |
要为图流关系属性的关系类型。 |
configuration |
映射 |
是 |
配置 streamNodeProperties 的其他参数。 |
名称 | 类型 | 默认值 | 描述 |
---|---|---|---|
concurrency |
整数 |
4 |
并发线程数。请注意,此过程始终以单线程运行。 |
名称 | 类型 | 描述 |
---|---|---|
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 | 投影图的名称。 |
源节点 | 目标节点 | 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 | 我们要从中流式传输的关系类型列表,仅使用我们需要的类型。 |
源节点 | 目标节点 | 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 | 我们要流式传输的属性。 |
源节点 | 目标节点 | 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 | 我们要从中流式传输属性的关系类型列表,仅使用我们需要的类型。 |
源节点 | 目标节点 | 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 | 我们要从中流式传输属性的关系类型列表,仅使用我们需要的类型。 |
源节点 | 目标节点 | 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 。 |
源节点 | 目标节点 | 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 |
我们要流式传输的属性必须存在于每个指定的关系列表中。 |