流式传输关系

若要仅检查关系拓扑,可使用 `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
表 1. 参数
名称 类型 可选 描述

图名称

字符串

图在目录中存储的名称。

关系类型

字符串或字符串列表

要流式传输图关系属性的关系类型。

配置

Map

配置 streamNodeProperties 的附加参数。

表 2. 配置
名称 类型 默认值 可选 描述

并发

整数

4 [1]

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

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

表 3. 结果
名称 类型 描述

源节点 ID

整数

关系的源节点 ID。

目标节点 ID

整数

关系的目标节点 ID。

关系类型

整数

关系的类型。

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
表 4. 参数
名称 类型 可选 描述

图名称

字符串

图在目录中存储的名称。

关系属性

字符串

要流式传输的图中的关系属性。

关系类型

字符串或字符串列表

要流式传输图关系属性的关系类型。

配置

Map

配置 streamNodeProperties 的附加参数。

表 5. 配置
名称 类型 默认值 可选 描述

并发

整数

4 [2]

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

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

表 6. 结果
名称 类型 描述

源节点 ID

整数

关系的源节点 ID。

目标节点 ID

整数

关系的目标节点 ID。

关系类型

整数

关系的类型。

属性值

  • 整数

  • 浮点数

存储的属性值。

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
表 7. 参数
名称 类型 可选 描述

图名称

字符串

图在目录中存储的名称。

关系属性

字符串列表

要流式传输的图中的关系属性。

关系类型

字符串或字符串列表

要流式传输图关系属性的关系类型。

配置

Map

配置 streamNodeProperties 的附加参数。

表 8. 配置
名称 类型 默认值 可选 描述

并发

整数

4 [3]

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

3. 在 GDS 会话中,默认值为可用处理器数量

表 9. 结果
名称 类型 描述

源节点 ID

整数

关系的源节点 ID。

目标节点 ID

整数

关系的目标节点 ID。

关系类型

整数

关系的类型。

关系属性

整数

关系属性的名称。

属性值

  • 整数

  • 浮点数

存储的属性值。

示例

以下所有示例都应在空数据库中运行。

示例通常使用 Cypher 投影。原生投影将在未来版本中弃用。

为了演示 GDS 在节点属性方面的功能,我们将在 Neo4j 中创建一个小图并将其投影到我们的图目录中。

Visualization of the example graph
以下 Cypher 语句将在 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 投影图的名称。
表 10. 结果
目标 关系类型

"Alice"

"Bob"

"SIMILAR"

"Alice"

"邦戈鼓"

"LIKES"

"Alice"

"Carol"

"SIMILAR"

"Alice"

"Dave"

"SIMILAR"

"Alice"

"吉他"

"LIKES"

"Alice"

"合成器"

"LIKES"

"Bob"

"Alice"

"SIMILAR"

"Bob"

"Dave"

"SIMILAR"

"Bob"

"吉他"

"LIKES"

"Bob"

"合成器"

"LIKES"

"Carol"

"Alice"

"SIMILAR"

"Carol"

"邦戈鼓"

"LIKES"

"Carol"

"Dave"

"SIMILAR"

"Dave"

"Alice"

"SIMILAR"

"Dave"

"Bob"

"SIMILAR"

"Dave"

"邦戈鼓"

"LIKES"

"Dave"

"Carol"

"SIMILAR"

"Dave"

"吉他"

"LIKES"

"Dave"

"合成器"

"LIKES"

从结果中我们可以看到,我们获得了两种关系类型(SIMILARLIKES)。我们可以进一步筛选想要流式传输的关系类型。这可以通过向过程传递第二个参数来实现,如下一个示例所示。

为特定关系类型流式传输单个关系
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 要流式传输的关系类型列表,仅使用我们需要的。
表 11. 结果
目标 关系类型

"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 我们想要流式传输的属性。
表 12. 结果
目标 关系类型 属性值

"Alice"

"Bob"

"SIMILAR"

0.6666666666666666

"Alice"

"邦戈鼓"

"LIKES"

3.0

"Alice"

"Carol"

"SIMILAR"

0.3333333333333333

"Alice"

"Dave"

"SIMILAR"

1.0

"Alice"

"吉他"

"LIKES"

5.0

"Alice"

"合成器"

"LIKES"

4.0

"Bob"

"Alice"

"SIMILAR"

0.6666666666666666

"Bob"

"Dave"

"SIMILAR"

0.6666666666666666

"Bob"

"吉他"

"LIKES"

4.0

"Bob"

"合成器"

"LIKES"

5.0

"Carol"

"Alice"

"SIMILAR"

0.3333333333333333

"Carol"

"邦戈鼓"

"LIKES"

2.0

"Carol"

"Dave"

"SIMILAR"

0.3333333333333333

"Dave"

"Alice"

"SIMILAR"

1.0

"Dave"

"Bob"

"SIMILAR"

0.6666666666666666

"Dave"

"邦戈鼓"

"LIKES"

5.0

"Dave"

"Carol"

"SIMILAR"

0.3333333333333333

"Dave"

"吉他"

"LIKES"

3.0

"Dave"

"合成器"

"LIKES"

1.0

从结果中我们可以看到,我们获得了两种具有 score 关系属性的关系类型(SIMILARLIKES)。我们可以进一步筛选想要流式传输的关系类型,这在下一个示例中进行了演示。

为特定关系类型流式传输单个关系属性
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 要从其中流式传输属性的关系类型列表,仅使用我们需要的。
表 13. 结果
目标 关系类型 属性值

"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 要从其中流式传输属性的关系类型列表,仅使用我们需要的。
表 14. 结果
目标 关系类型 关系属性 属性值

"Alice"

"邦戈鼓"

"LIKES"

"score"

3.0

"Alice"

"邦戈鼓"

"LIKES"

"strength"

0.5

"Alice"

"吉他"

"LIKES"

"score"

5.0

"Alice"

"吉他"

"LIKES"

"strength"

1.0

"Alice"

"合成器"

"LIKES"

"score"

4.0

"Alice"

"合成器"

"LIKES"

"strength"

1.0

"Bob"

"吉他"

"LIKES"

"score"

4.0

"Bob"

"吉他"

"LIKES"

"strength"

1.0

"Bob"

"合成器"

"LIKES"

"score"

5.0

"Bob"

"合成器"

"LIKES"

"strength"

1.0

"Carol"

"邦戈鼓"

"LIKES"

"score"

2.0

"Carol"

"邦戈鼓"

"LIKES"

"strength"

1.0

"Dave"

"邦戈鼓"

"LIKES"

"score"

5.0

"Dave"

"邦戈鼓"

"LIKES"

"strength"

1.0

"Dave"

"吉他"

"LIKES"

"score"

3.0

"Dave"

"吉他"

"LIKES"

"strength"

1.0

"Dave"

"合成器"

"LIKES"

"score"

1.0

"Dave"

"合成器"

"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
表 15. 结果
目标 关系类型 关系属性 属性值

"Alice"

"Bob"

"SIMILAR"

"score"

0.6666666666666666

"Alice"

"邦戈鼓"

"LIKES"

"score"

3.0

"Alice"

"Carol"

"SIMILAR"

"score"

0.3333333333333333

"Alice"

"Dave"

"SIMILAR"

"score"

1.0

"Alice"

"吉他"

"LIKES"

"score"

5.0

"Alice"

"合成器"

"LIKES"

"score"

4.0

"Bob"

"Alice"

"SIMILAR"

"score"

0.6666666666666666

"Bob"

"Dave"

"SIMILAR"

"score"

0.6666666666666666

"Bob"

"吉他"

"LIKES"

"score"

4.0

"Bob"

"合成器"

"LIKES"

"score"

5.0

"Carol"

"Alice"

"SIMILAR"

"score"

0.3333333333333333

"Carol"

"邦戈鼓"

"LIKES"

"score"

2.0

"Carol"

"Dave"

"SIMILAR"

"score"

0.3333333333333333

"Dave"

"Alice"

"SIMILAR"

"score"

1.0

"Dave"

"Bob"

"SIMILAR"

"score"

0.6666666666666666

"Dave"

"邦戈鼓"

"LIKES"

"score"

5.0

"Dave"

"Carol"

"SIMILAR"

"score"

0.3333333333333333

"Dave"

"吉他"

"LIKES"

"score"

3.0

"Dave"

"合成器"

"LIKES"

"score"

1.0

我们想要流式传输的属性必须存在于每个指定的关系类型中。
© . All rights reserved.