写入关系

要将关系类型持久化到 Neo4j 数据库中,我们可以使用 gds.graph.relationship.write。与流式传输关系拓扑或属性类似,也可以将其写回 Neo4j 数据库。这类似于算法的 write 执行模式,但允许对操作进行更精细的控制。

默认情况下,不会写入任何关系属性。要写入关系属性,必须明确指定它们。

语法

关系类型各种操作的语法描述
CALL gds.graph.relationship.write(
    graphName: String,
    relationshipType: String,
    relationshipProperty: String,
    configuration: Map
)
YIELD
  writeMillis: Integer,
  graphName: String,
  relationshipType: String,
  relationshipsWritten: Integer,
  relationshipProperty: String,
  propertiesWritten: Integer,
  configuration: Map
表 1. 参数
名称 类型 可选 描述

graphName

字符串

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

关系类型 (relationshipType)

字符串

图谱中要写回的关系类型。

relationshipProperty

字符串

要写回的关系属性。

配置

Map

用于配置 writeRelationship 的附加参数。

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

concurrency

整数

4 [1]

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

writeConcurrency

整数

'concurrency'

用于写入关系属性的并发线程数。请注意,此过程始终以单线程运行。

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

表 3. 结果
名称 类型 描述

writeMillis

整数

将结果数据写回 Neo4j 的毫秒数。

graphName

字符串

存储在目录中的图的名称。

关系类型 (relationshipType)

字符串

已写入的关系类型。

relationshipsWritten

整数

已写入的关系数量。

relationshipProperty

字符串

已写入的关系属性名称。

propertiesWritten

整数

已写入的关系属性数量。

配置

Map

运行该过程所使用的配置。

CALL gds.graph.relationshipProperties.write(
    graphName: String,
    relationshipType: String,
    relationshipProperties: List of String,
    configuration: Map
)
YIELD
  writeMillis: Integer,
  graphName: String,
  relationshipType: String,
  relationshipsWritten: Integer,
  relationshipProperties: List of String,
  propertiesWritten: Integer,
  configuration: Map
表 4. 参数
名称 类型 可选 描述

graphName

字符串

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

关系类型 (relationshipType)

字符串

图谱中要写回的关系类型。

relationshipProperties

字符串

要写回的关系属性。

配置

Map

用于配置该过程的附加参数。

表 5. 结果
名称 类型 描述

writeMillis

整数

将结果数据写回 Neo4j 的毫秒数。

graphName

字符串

存储在目录中的图的名称。

关系类型 (relationshipType)

字符串

已写入的关系类型。

relationshipsWritten

整数

已写入的关系数量。

relationshipProperties

字符串

已写入的关系属性名称。

propertiesWritten

整数

已写入的关系属性数量。

配置

Map

运行该过程所使用的配置。

示例

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

本示例将 Cypher 投影作为标准。原生投影将在未来的版本中弃用。

我们可以将存储在内存中命名图里的关系写回 Neo4j。这可用于写入算法结果(例如来自节点相似度算法的结果)或在图创建过程中聚合的关系。

要写入的关系由关系类型指定。

关系始终使用单个线程写入。

为了演示 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

关系类型

将关系写入 Neo4j
CALL gds.graph.relationship.write(
  'personsAndInstruments',        (1)
  'SIMILAR'                       (2)
)
YIELD
  graphName, relationshipType, relationshipProperty, relationshipsWritten, propertiesWritten
1 投影图的名称。
2 我们想要写回 Neo4j 数据库的关系类型。
表 6. 结果
graphName 关系类型 (relationshipType) relationshipProperty relationshipsWritten propertiesWritten

"personsAndInstruments"

"SIMILAR"

null

10

0

默认情况下,不会写入任何关系属性,从结果中可以看出,relationshipProperty 值为 nullpropertiesWritten0

这是执行上述示例后,示例图在 Neo4j 中外观的示意图。

Visualization of the example graph after writing relationships back

SIMILAR 关系已添加到基础数据库中,可用于 Cypher 查询或投影到内存中以运行算法。在此示例中,关系是无向的,因为我们使用了节点相似度来改变内存图,并且该算法创建的是无向关系;如果我们使用不同的算法,情况可能并非如此。

带属性的关系类型

要写入关系属性,必须明确指定它们。

将关系及其属性写入 Neo4j
CALL gds.graph.relationship.write(
  'personsAndInstruments',          (1)
  'SIMILAR',                        (2)
  'score'                           (3)
)
YIELD
  graphName, relationshipType, relationshipProperty, relationshipsWritten, propertiesWritten
1 投影图的名称。
2 我们想要写回 Neo4j 数据库的关系类型。
3 我们想要写回 Neo4j 数据库的关系属性名称。
表 7. 结果
graphName 关系类型 (relationshipType) relationshipProperty relationshipsWritten propertiesWritten

"personsAndInstruments"

"SIMILAR"

"score"

10

10

带多个属性的关系类型

为了演示将具有多个属性的关系写回 Neo4j,我们将首先在数据库中创建一个小图。

Visualization of the example graph
以下 Cypher 语句将在 Neo4j 数据库中创建此示例的图
CREATE
  (alice:Buyer {name: 'Alice'}),
  (instrumentSeller:Seller {name: 'Instrument Seller'}),
  (bob:Buyer {name: 'Bob'}),
  (carol:Buyer {name: 'Carol'}),
  (alice)-[:PAYS { amount: 1.0}]->(instrumentSeller),
  (alice)-[:PAYS { amount: 2.0}]->(instrumentSeller),
  (alice)-[:PAYS { amount: 3.0}]->(instrumentSeller),
  (alice)-[:PAYS { amount: 4.0}]->(instrumentSeller),
  (alice)-[:PAYS { amount: 5.0}]->(instrumentSeller),
  (alice)-[:PAYS { amount: 6.0}]->(instrumentSeller),

  (bob)-[:PAYS { amount: 3.0}]->(instrumentSeller),
  (bob)-[:PAYS { amount: 4.0}]->(instrumentSeller),
  (carol)-[:PAYS { amount: 5.0}]->(bob),
  (carol)-[:PAYS { amount: 6.0}]->(bob)
投影图
MATCH (buyer:Buyer)-[r:PAYS]->(seller:Buyer|Seller)
WITH buyer, seller, sum(r.amount) AS totalAmount, count(r.amount) AS numberOfPayments
RETURN gds.graph.project(
  'aggregatedGraph',
  buyer,
  seller,
  {
    sourceNodeLabels: labels(buyer),
    targetNodeLabels: labels(seller),
    relationshipType: 'PAID',
    relationshipProperties: { totalAmount: totalAmount, numberOfPayments: numberOfPayments }
  }
)

正如我们所见,Neo4j 图包含一些并行关系。我们使用 GDS 投影将这些关系压缩为节点之间的单一关系。在此示例中,我们想要跟踪某人向某人付款的次数以及所有付款的总金额。

要写入关系属性,必须明确指定它们。

将关系及其属性写入 Neo4j
CALL gds.graph.relationshipProperties.write(
  'aggregatedGraph',                    (1)
  'PAID',                               (2)
  ['totalAmount', 'numberOfPayments'],  (3)
  {}
)
YIELD
  graphName, relationshipType, relationshipProperties, relationshipsWritten, propertiesWritten
1 投影图的名称。
2 我们想要写回 Neo4j 数据库的关系类型。
3 我们想要写回 Neo4j 数据库的关系属性名称。
表 8. 结果
graphName 关系类型 (relationshipType) relationshipProperties relationshipsWritten propertiesWritten

"aggregatedGraph"

"PAID"

["totalAmount", "numberOfPayments"]

3

6