2
votes

I'm using neo4j 3.1 with java 8 and I want to extract a connected subgraph as to store it as a test database. Is it possible to do it and how? How to do it with the clause Return which returns the output. So, I had to create new nodes and relations or just export the subgraph and put it in a new database.

How can I extract a connected subgraph since I have a disconnected graph.

Thank you

2

2 Answers

1
votes

There are two parts to this...getting the connected subgraph, and then finding a means to export.

APOC Procedures seems like it can cover both of these. The approach in this answer using the path expander should get you all the nodes in the connected subgraph (if the relationship type doesn't matter, leave off the relationshipFilter parameter).

The next step is to get all relationships between all of those nodes. APOC's apoc.algo.cover() function in the graph algorithms section should accomplish this.

Something like this (assuming this is after the subgraph query, and subgraphNode is in scope for the column of distinct subgraph nodes):

...
WITH COLLECT(subgraphNode) as subgraph, COLLECT(id(subgraphNode)) as ids
CALL apoc.algo.cover(ids) YIELD rel
WITH subgraph, COLLECT(rel) as rels
...

Now that you have the collections of both the nodes and relationships in the subgraph, you can export them.

APOC Procedures offers several means of exporting, from CSV to CypherScript. You should be able to find an option that works for you.

0
votes

You can also use the neo4j-shell to extract the result of a query to a file and use this same file to re-import it in the neo4j database :

ikwattro@graphaware-team ~/d/_/310> ./bin/neo4j-shell -c 'dump MATCH (n:Product)-[r*2]->(x) RETURN n, r, x;' > result.cypher

check the file

ikwattro@graphaware-team ~/d/_/310> cat result.cypher
begin
commit
begin
create (_1:`Product` {`id`:"product123"})
create (_2:`ProductInformation` {`id`:"product123EXCEL"})
create (_3:`ProductInformationElement` {`id`:"product123EXCELtitle", `key`:"title", `value`:"Original Title"})
create (_5:`ProductInformationElement` {`id`:"product123EXCELproduct_type", `key`:"product_type", `value`:"casual_bag"})
create (_1)-[:`PRODUCT_INFORMATION`]->(_2)
create (_2)-[:`INFORMATION_ELEMENT`]->(_3)
create (_2)-[:`INFORMATION_ELEMENT`]->(_5)
;
commit

Use this file for feeding another neo4j :

ikwattro@graphaware-team ~/d/_/310> ./bin/neo4j-shell -file result.cypher
Transaction started
Transaction committed
Transaction started
+-------------------+
| No data returned. |
+-------------------+
Nodes created: 4
Relationships created: 3
Properties set: 8
Labels added: 4
52 ms
Transaction committed