流式读取关系

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

graphName

字符串

图在目录中存储时所使用的名称。

relationshipTypes

字符串或字符串列表

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

配置

Map

用于配置 streamNodeProperties 的附加参数。

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

concurrency

整数

4 [1]

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

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

表 3. 结果
名称 类型 描述

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

graphName

字符串

图在目录中存储时所使用的名称。

relationshipProperty

字符串

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

relationshipTypes

字符串或字符串列表

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

配置

Map

用于配置 streamNodeProperties 的附加参数。

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

concurrency

整数

4 [2]

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

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

表 6. 结果
名称 类型 描述

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

graphName

字符串

图在目录中存储时所使用的名称。

relationshipProperties

字符串列表

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

relationshipTypes

字符串或字符串列表

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

配置

Map

用于配置 streamNodeProperties 的附加参数。

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

concurrency

整数

4 [3]

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

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

表 9. 结果
名称 类型 描述

sourceNodeId

整数

关系源节点的 ID。

targetNodeId

整数

关系目标节点的 ID。

关系类型 (relationshipType)

整数

关系的类型。

relationshipProperty

整数

关系属性的名称。

propertyValue

  • 整数

  • 浮点数

存储的属性值。

示例

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

本示例将 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. 结果
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"

从结果中可以看出,我们得到了两种关系类型(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. 结果
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 我们要流式传输出来的属性。
表 12. 结果
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 关系属性的关系类型(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. 结果
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 我们要流式传输其属性的关系类型列表,仅使用我们需要的类型。
表 14. 结果
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
表 15. 结果
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

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