流关系

要仅检查关系拓扑结构,可以使用 `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

字符串或字符串列表

要为图流关系属性的关系类型。

configuration

映射

配置 streamNodeProperties 的其他参数。

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

concurrency

整数

4

并发线程数。请注意,此过程始终以单线程运行。

表 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

字符串或字符串列表

要为图流关系属性的关系类型。

configuration

映射

配置 streamNodeProperties 的其他参数。

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

concurrency

整数

4

并发线程数。请注意,此过程始终以单线程运行。

表 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

字符串或字符串列表

要为图流关系属性的关系类型。

configuration

映射

配置 streamNodeProperties 的其他参数。

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

concurrency

整数

4

并发线程数。请注意,此过程始终以单线程运行。

表 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. 结果
源节点 目标节点 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. 结果
源节点 目标节点 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. 结果
源节点 目标节点 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. 结果
源节点 目标节点 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. 结果
源节点 目标节点 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. 结果
源节点 目标节点 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

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