写入关系

为了将关系类型持久化到 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

字符串

要写回的关系属性。

configuration

映射

配置 writeRelationship 的附加参数。

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

concurrency

整数

4 [1]

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

writeConcurrency

整数

'concurrency'

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

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

表 3. 结果
名称 类型 描述

writeMillis

整数

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

graphName

字符串

目录中存储的图的名称。

relationshipType

字符串

已写入的关系类型。

relationshipsWritten

整数

写入的关系数量。

relationshipProperty

字符串

已写入的关系属性名称。

propertiesWritten

整数

写入的关系属性数量。

configuration

映射

用于运行过程的配置。

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

字符串

要写回的关系属性。

configuration

映射

配置过程的附加参数。

表 5. 结果
名称 类型 描述

writeMillis

整数

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

graphName

字符串

目录中存储的图的名称。

relationshipType

字符串

已写入的关系类型。

relationshipsWritten

整数

写入的关系数量。

relationshipProperties

字符串

已写入的关系属性名称。

propertiesWritten

整数

写入的关系属性数量。

configuration

映射

用于运行过程的配置。

示例

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

示例通常使用 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

© . All rights reserved.