Mehran
Mehran

Reputation: 16911

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

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!?

Upvotes: 0

Views: 532

Answers (2)

InverseFalcon
InverseFalcon

Reputation: 30417

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
...

Upvotes: 1

Tomaž Bratanič
Tomaž Bratanič

Reputation: 6534

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;

Upvotes: 1

Related Questions