How to extract a graph out of Neo4J and reconstruct it in the programming language

狂风中的少年 提交于 2019-12-24 20:12:22

问题


Consider I have a bunch of connected nodes in Neo4J, forming a tree or a graph or whatever, and I want to have them in the programming language that I'm using (I'm using Java but that's not important).

I know I can have them all with a single cypher query like this:

MATCH (n0:Root)-[:Child*0..]->(nx:Node) WHERE ID(n0) = 1 RETURN nx;

But the problem I have here is that once returned to Java, I don't know which node is connected to which! How can I return the data so I can reconstruct the graph in my programming language?

I can see that the Neo4J web interface is doing that but I don't know how!?


回答1:


In your query you are returning only :Node and not any relationship info or :Root nodes.

One example would be to return the ids of nodes and type of relationships between them

MATCH (s)-[r]->(t)
RETURN id(s) as source,id(r) as target,type(r) as relationship_type

You can modify this query depending on what you want to export.

The whole idea is to return nodes in pairs (source)->(destination). If you want to export only a specific subgraph that is connected to a specific starting node labeled :Root, you can return the graph like this:

MATCH (n0:Root:Node)-[:Child*0..]->(n1:Node)-[:Child]->(n2:Node)
RETURN n1, n2;



回答2:


As an alternative, if you have access to APOC Procedures, you can take advantage of apoc.path.subgraphAll(), which gives you a list of all nodes in the subgraph, and all relationships between nodes in the subgraph.

MATCH (n0:Root)
CALL apoc.path.subgraphAll(n0,{relationshipFilter:'Child>'}) YIELD nodes, relationships
...


来源:https://stackoverflow.com/questions/43836824/how-to-extract-a-graph-out-of-neo4j-and-reconstruct-it-in-the-programming-langua

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!