在neo4j GDS中,是否有一种方法可以访问图目录中的特定节点和关系数据?

kgqe7b3p  于 2023-10-18  发布在  其他
关注(0)|答案(3)|浏览(171)

在Neo4j Graph Data Science(GDS)中,每个图都可以有一个对应的投影。如何获得有关存储在特定投影中的实际节点和关系的详细信息?是否有方法可以访问投影中的特定节点和关系数据?
对于一个特定的图目录投影graph_0,我可以使用CALL gds.graph.list('graph_0')来检索关于投影的基本信息,比如节点和关系的数量。但是,没有列出有关节点和关系的详细信息。
--添加一个例子来解释更多细节-
1.使用以下cypher命令创建一个简单的图形:

  1. CREATE
  2. (alice:Buyer {name: 'Alice'}),
  3. (instrumentSeller:Seller {name: 'Instrument Seller'}),
  4. (bob:Buyer {name: 'Bob'}),
  5. (carol:Buyer {name: 'Carol'}),
  6. (alice)-[:PAYS { amount: 1.0}]->(instrumentSeller),
  7. (alice)-[:PAYS { amount: 2.0}]->(instrumentSeller),
  8. (alice)-[:PAYS { amount: 3.0}]->(instrumentSeller),
  9. (alice)-[:PAYS { amount: 4.0}]->(instrumentSeller),
  10. (alice)-[:PAYS { amount: 5.0}]->(instrumentSeller),
  11. (alice)-[:PAYS { amount: 6.0}]->(instrumentSeller),
  12. (bob)-[:PAYS { amount: 3.0}]->(instrumentSeller),
  13. (bob)-[:PAYS { amount: 4.0}]->(instrumentSeller),
  14. (carol)-[:PAYS { amount: 5.0}]->(bob),
  15. (carol)-[:PAYS { amount: 6.0}]->(bob)

example graph
1.投射出来

  1. MATCH (source)
  2. OPTIONAL MATCH (source)-[r]->(target)
  3. WITH gds.graph.project(
  4. 'graph_0',
  5. source,
  6. target,
  7. {
  8. sourceNodeLabels: labels(source),
  9. targetNodeLabels: labels(target),
  10. relationshipType: type(r)
  11. }
  12. ) AS g
  13. RETURN g.graphName AS graph, g.nodeCount AS nodes, g.relationshipCount AS rels
  14. // return:
  15. //graph nodes rels
  16. //"graph_0" 4 10

1.在示例图上运行随机游走算法

  1. MATCH (start:Buyer {name: 'Alice'})
  2. CALL gds.graph.sample.rwr('mySample', 'graph_0', { samplingRatio: 0.66, startNodes: [id(start)] })
  3. YIELD nodeCount, relationshipCount
  4. RETURN nodeCount, relationshipCount
  5. // return:
  6. // nodeCount relationshipCount
  7. // 3 8

1.在该示例中,两个节点之间存在具有相同类型的多个关系,例如AliceInstrument Seller之间的PAYS。我想知道在此抽样过程中对哪些关系(用关系ID标识)进行了抽样。然而,目前提供的方法只能找出哪种类型的关系被采样,而不是确切的关系ID。

x9ybnkn6

x9ybnkn61#

您可以通过从CALL gds.graph.list()的输出中产生并返回schemaWithOrientation等附加字段来获取有关特定本机命名图的扩展信息。例如,schemaWithOrientation返回投影图中包含的节点标签、关系类型、关系方向和属性。您可以在这里查看完整列表:https://neo4j.com/docs/graph-data-science/current/graph-list/
范例:

  1. CALL gds.graph.list('personsNative')
  2. YIELD graphName, schemaWithOrientation, configuration
  3. RETURN graphName, schemaWithOrientation, configuration.nodeProjection AS nodeProjection
pqwbnv8z

pqwbnv8z2#

您还可以使用通过流式传输来检查节点的内容(请参阅https://neo4j.com/docs/graph-data-science/current/graph-catalog-node-ops/)。这同样适用于关系(参见https://neo4j.com/docs/graph-data-science/current/graph-catalog-relationship-ops/)。
此外,如果你擅长使用Cypher,你也可以在GDS(https://neo4j.com/docs/graph-data-science/current/management-ops/create-cypher-db/)上试用Cypher。

hmtdttj4

hmtdttj43#

不幸的是,GDS目前(在GDS 2.4.4中)没有提供一种从投影中获取关系ID的方法。
但有一个有点丑陋的解决方案。您可以向每个感兴趣的关系添加一个特殊属性(比如_relId),其中包含该关系的本机ID。然后,您可以在创建投影时包含该属性,并在生成GDS结果后取回该属性。
例如,在您的用例中:

  • _relId添加到所有关系中(因为它们在您的用例中都是感兴趣的)。
  1. MATCH ()-[r]->()
  2. SET r._relId = ID(r)
  • 项目,并包含_relId属性。
  1. MATCH (source)
  2. OPTIONAL MATCH (source)-[r]->(target)
  3. WITH gds.graph.project(
  4. 'graph_0',
  5. source,
  6. target,
  7. {
  8. sourceNodeLabels: labels(source),
  9. targetNodeLabels: labels(target),
  10. relationshipType: type(r),
  11. relationshipProperties: r { ._relId }
  12. }
  13. ) AS g
  14. RETURN g.graphName AS graph, g.nodeCount AS nodes, g.relationshipCount AS rels
  • 运行随机游走。
  1. MATCH (start:Buyer {name: 'Alice'})
  2. CALL gds.graph.sample.rwr('mySample', 'graph_0', { samplingRatio: 0.66, startNodes: [id(start)] })
  3. YIELD nodeCount, relationshipCount
  4. RETURN nodeCount, relationshipCount
  • 从输出投影mySample获取关系,包括本机关系ID(结果中为relNativeId)。
  1. CALL gds.graph.relationshipProperties.stream('mySample', ['_relId'])
  2. YIELD sourceNodeId, targetNodeId, relationshipType, propertyValue
  3. RETURN sourceNodeId, targetNodeId, relationshipType, TOINTEGER(propertyValue) AS relNativeId

下面是一个示例结果:

  1. ╒════════════╤════════════╤════════════════╤═══════════╕
  2. sourceNodeIdtargetNodeIdrelationshipTyperelNativeId
  3. ╞════════════╪════════════╪════════════════╪═══════════╡
  4. 171 172 "PAYS" 256
  5. ├────────────┼────────────┼────────────────┼───────────┤
  6. 171 172 "PAYS" 253
  7. ├────────────┼────────────┼────────────────┼───────────┤
  8. 171 172 "PAYS" 257
  9. ├────────────┼────────────┼────────────────┼───────────┤
  10. 171 172 "PAYS" 255
  11. ├────────────┼────────────┼────────────────┼───────────┤
  12. 171 172 "PAYS" 258
  13. ├────────────┼────────────┼────────────────┼───────────┤
  14. 171 172 "PAYS" 254
  15. └────────────┴────────────┴────────────────┴───────────┘

警告:如果从数据库中删除一个投影关系,那么它的投影(包括它的_relId值)将无效。实际上,旧的_relId值要么什么都不引用,要么引用一些随机的新关系。

展开查看全部

相关问题