写入节点属性和标签

可以将计算出的节点属性持久保存到 Neo4j 数据库中。我们将此操作称为写入。这与算法write执行模式类似,但允许更细粒度地控制操作。

语法

节点属性上不同操作的语法描述
CALL gds.graph.nodeProperties.write(
    graphName: String,
    nodeProperties: String or Map of Strings or List of Strings and/or Maps of Strings,
    nodeLabels: String or List of Strings,
    configuration: Map
)
YIELD
    writeMillis: Integer,
    propertiesWritten: Integer,
    graphName: String,
    nodeProperties: String or List of String,
    configuration: Map
表 1. 参数
名称 类型 可选 描述

graphName

字符串

图在目录中存储的名称。

nodeProperties

字符串、字符串映射或字符串和/或字符串映射列表

要写回的图中的节点属性。要写回具有新自定义名称的属性,请将它们存储为映射中的条目,形式为:nodeProperty: 'renamedProperty'

nodeLabels

字符串或字符串列表

要写回其节点属性的节点标签。

configuration

映射

用于配置 writeNodeProperties 的附加参数。

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

concurrency

整数

4

用于运行过程的并发线程数。还提供了writeConcurrency的默认值。

writeConcurrency

整数

'concurrency'

用于写入节点属性的并发线程数。

表 3. 结果
名称 类型 描述

writeMillis

整数

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

propertiesWritten

整数

写入的属性数。

graphName

字符串

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

nodeProperties

字符串或字符串列表

写入的节点属性。

configuration

映射

用于运行过程的配置。

CALL gds.graph.nodeLabel.write(
    graphName: String,
    nodeLabel: String,
    configuration: Map
)
YIELD
    writeMillis: Integer,
    nodeLabelsWritten: Integer,
    nodeLabel: String,
    graphName: String,
    nodeCount: Integer,
    configuration: Map
表 4. 参数
名称 类型 可选 描述

graphName

字符串

图在目录中存储的名称。

nodeLabel

字符串

要写回的节点标签。

configuration

映射

用于配置 writeNodeProperties 的附加参数。

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

nodeFilter

字符串

n/a

用于过滤输入图中节点的 Cypher 谓词。请参阅投影子图

concurrency

整数

4

用于运行过程的并发线程数。

表 6. 结果
名称 类型 描述

writeMillis

整数

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

nodeLabelsWritten

整数

写入的节点标签数。

graphName

字符串

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

nodeLabel

字符串

写入的节点标签。

nodeCount

整数

图中的节点总数。

configuration

映射

用于运行过程的配置。

示例

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

这些示例使用Cypher 投影 作为规范。原生投影将在未来的版本中被弃用。

为了演示 GDS 对节点属性的功能,我们将创建一个小的社交网络图,并将其投影到我们的图目录中。

以下 Cypher 语句将在 Neo4j 数据库中创建示例图
CREATE
  (florentin:Person { name: 'Florentin', age: 16 }),
  (adam:Person { name: 'Adam', age: 18 }),
  (veselin:Person { name: 'Veselin', age: 20 }),
  (hobbit:Book { name: 'The Hobbit', numberOfPages: 310 }),
  (florentin)-[:KNOWS { since: 2010 }]->(adam),
  (florentin)-[:KNOWS { since: 2018 }]->(veselin),
  (adam)-[:READ]->(hobbit)
投影小的社交网络图
MATCH (n:Person)-[r:KNOWS|READ]->(m:Person|Book)
RETURN gds.graph.project('socialGraph', n, m,
  {
    sourceNodeLabels: labels(n),
    targetNodeLabels: labels(m),
    sourceNodeProperties: n { .age },
    targetNodeProperties: CASE WHEN m:Person THEN m { .age } ELSE {} END,
    relationshipType: type(r)
  }
)
计算我们社交图中的度中心性
CALL gds.degree.mutate('socialGraph', {mutateProperty: 'score'})

写入节点属性

要为社交图中的所有节点标签写入“score”属性,请使用以下查询

score属性写回 Neo4j
CALL gds.graph.nodeProperties.write('socialGraph', ['score'])
YIELD propertiesWritten
表 7. 结果
propertiesWritten

4

上面的示例要求score属性在至少一个投影的节点标签上存在,并且这些属性将为所有此类标签写入。

写回时重命名属性

可以重命名节点属性,并将它们写回数据库,使用自定义名称。为此,您可以使用一个映射,其中每个条目都是一个元组{nodeProperty: 'renamedProperty'},即键对应于内存中图中存在的节点属性,而值对应于要写回数据库的名称。

为了方便起见,地图可以包含多个条目。nodeProperties 配置参数接受字符串和地图,以及列表中任意组合。当我们只想重命名少量属性时,这将非常有用。

agescore 属性以新名称写入 Neo4j,score 的新名称为 writtenScore
CALL gds.graph.nodeProperties.write('socialGraph', ['age', {score: 'writtenScore'}])
YIELD nodeProperties
表 8. 结果
nodeProperties

["age", "writtenScore"]

在上面的示例中,我们将 age 以其默认名称写回数据库,而通过使用地图,我们将 score 重命名为 writtenScore

节点标签

该过程可以配置为仅写入特定节点标签的属性。在以下示例中,我们只写回 Person 节点的分数。

将特定投影节点标签的节点属性写入 Neo4j
CALL gds.graph.nodeProperties.write('socialGraph', ['score'], ['Person'])
YIELD propertiesWritten
表 9. 结果
propertiesWritten

3

如果指定了 nodeLabels 参数,则要求所有给定的节点标签都具有所有给定的属性。

写入节点标签

为了将分数大于 0 的节点的新节点标签写入数据库,我们使用以下查询

Reader 节点标签写回 Neo4j
CALL gds.graph.nodeLabel.write('socialGraph', 'Reader', { nodeFilter: 'n.score > 0.0' })
YIELD graphName, nodeCount, nodeLabel, nodeLabelsWritten
表 10. 结果
graphName nodeCount nodeLabel nodeLabelsWritten

"socialGraph"

4

"Reader"

2

查询 Reader 节点标签
MATCH (n:Reader) RETURN n.name AS name, labels(n) AS labels
ORDER BY name ASC
表 11. 结果
名称 标签

"Adam"

["Person", "Reader"]

"Florentin"

["Person", "Reader"]

正如我们从数据库中看到的那样,Veselinscore: 0.0 不是 Reader