cancel
Showing results forΒ 
Search instead forΒ 
Did you mean:Β 

Duplicate a subgraph

verachkaverachk
Graph Buddy

Hello,
can I duplicate the following graph in neo4j? and if it is possible, then what cyphers can can me?


Have a lovely day πŸ™‚

1 ACCEPTED SOLUTION

Cobra
Ninja
Ninja

You can try this query to duplicate all nodes and their relationships:

MATCH (n)
WITH collect(n) AS nodes
CALL apoc.refactor.cloneSubgraph(nodes)
YIELD input, output, error
RETURN input, output, error;

You can also add a WHERE clause to select nodes and their relationships from of a subgraph:

MATCH (n)
WHERE n.name = "Graph1"
WITH collect(n) AS nodes
CALL apoc.refactor.cloneSubgraph(nodes)
YIELD input, output, error
RETURN input, output, error;

There is more documentation here.

View solution in original post

25 REPLIES 25

Cobra
Ninja
Ninja

Hello @verachkaverachka

If you want to duplicate this graph in another database?

You can use the Graph Data Science plugin.

First, you create the in-memory graph:

CALL gds.graph.project('graph', '*', '*')
YIELD graphName AS graph, nodeProjection, nodeCount AS nodes, relationshipProjection, relationshipCount AS rels

Then, you export the graph to a new database:

CALL gds.graph.export('graph', { dbName: 'mydatabase' })

Finally, you can create the new database and use it:

:use system
CREATE DATABASE mydatabase;
:use mydatabase

Regards,
Cobra

verachkaverachk
Graph Buddy

thank you so much for your answers
and if I want to duplicate this graph in the same databse? is it possible?
and if it is, then which query can help me?

Cobra
Ninja
Ninja

You can have a look at APOC plugin with clone nodes functions or clone subgraph functions. I think the apoc.refactor.cloneNodesWithRelationships() function is what you need.

Maybe this query will help you:

MATCH (n)
CALL apoc.refactor.cloneNodesWithRelationships([n])
YIELD input, output
RETURN *

verachkaverachk
Graph Buddy

Hello ,I've installed the APOC library, and tried the suggested query.
this one:
MATCH (n)
CALL apoc.refactor.cloneNodesWithRelationships([n])
YIELD input, output
RETURN *what i've tried to get was this whole graph twice , like to saparated similar graphs:


what i've actually got is something weird that looks like this:

What I try to do is find a query that will duplicate my original graph and i'll gey two of these.
What query can I do to solve it?
have a great day.
Vera

Cobra
Ninja
Ninja

Can you share your dataset please?

verachkaverachk
Graph Buddy

Where get I get it from?(the dataSet) so I can send it here?

verachkaverachk
Graph Buddy

I can try to export the data I have to one of these:

or dataSet means something else?

Cobra
Ninja
Ninja

The dataset is the data, can you share queries to recreate the graph?

verachkaverachk
Graph Buddy

I create the graph with the help of java (Springboot Dao) it starts with an entity of CellType that represents the type of a node I'm going to use(operation or input ) and with the first celltype(that must be a type of input) I connect it to a "real cell" and I call it "cellInstaces".(they also will appear in my frontend later without all the attributes). This cellInstance is going to be connected to another cellInstance from Celltype(again cellInstance must have a type-cellType) of function because in the end my purpose is to run a simulator on this graph and calculate stuff from function to function. Then, I connect an entity named a subgraphInstance to cellInstance(that represents the input)to my cellInstance. and all the nodes that are connected to this first cellInstance we will see them.
the query I use to create cell instance is this one


The query to connect between cellInstances is

I did use a query when I wanted to get my graph that looks like this:

verachkaverachk
Graph Buddy

and if you meant they tables that represents the data of this graph for example:


it look like this:
{
"identity": 0,
"labels": [
"CellInstance"
],
"properties": {
"yCoordinate": 5.6,
"xCoordinate": 3.7
}
}
{
"identity": 20,
"labels": [
"SubGraphInstance"
],
"properties": {
"name": "Graph1"
}
}
{
"identity": 77,
"labels": [
"Variable"
],
"properties": {
"variableName": "x"
}
}
{
"identity": 78,
"labels": [
"Function"
],
"properties": {
"expression": "x"
}
}
{
"identity": 79,
"labels": [
"CellType"
],
"properties": {
"name": "Input Cell",
"transformType": "INPUT_TO_ANALOG"
}
}
{
"identity": 84,
"labels": [
"CellInstance"
],
"properties": {
"yCoordinate": 6.6,
"xCoordinate": 4.7
}
}
{
"identity": 85,
"labels": [
"Variable"
],
"properties": {
"variableName": "x"
}
}
{
"identity": 86,
"labels": [
"Variable"
],
"properties": {
"variableName": "y"
}
}
{
"identity": 87,
"labels": [
"Function"
],
"properties": {
"expression": "x+y"
}
}
{
"identity": 88,
"labels": [
"CellType"
],
"properties": {
"name": "Addition ",
"transformType": "ANALOG_TO_ANALOG"
}
}
{
"identity": 89,
"labels": [
"CellInstance"
],
"properties": {
"yCoordinate": 6.6,
"xCoordinate": 4.7
}
}
{
"identity": 90,
"labels": [
"Connection"
],
"properties": {
"delay": 1
}
}
{
"identity": 91,
"labels": [
"Connection"
],
"properties": {
"delay": 1
}
}

Cobra
Ninja
Ninja

You can try this query to duplicate all nodes and their relationships:

MATCH (n)
WITH collect(n) AS nodes
CALL apoc.refactor.cloneSubgraph(nodes)
YIELD input, output, error
RETURN input, output, error;

You can also add a WHERE clause to select nodes and their relationships from of a subgraph:

MATCH (n)
WHERE n.name = "Graph1"
WITH collect(n) AS nodes
CALL apoc.refactor.cloneSubgraph(nodes)
YIELD input, output, error
RETURN input, output, error;

There is more documentation here.

verachkaverachk
Graph Buddy

I will try it and let you know today! ,thank you so so much for trying to help me understand !!

verachkaverachk
Graph Buddy

yeahi! it works!
thank you so much for helping me

verachkaverachk
Graph Buddy

Hello!
Can I duplicate my graph by his name or id with a query?for example if I have a few different subgraphs(When I say subgraphisntance i mean the node and all his connected elements) and I want to duplicate all the elements of a specific subgraph in my DB and not duplicate them all, is there a way to do it with apoc? Like you showed me with the first query but this time finding the whole subgraph by his id or name?
Vera

Cobra
Ninja
Ninja

Hello @verachkaverachka

It's in the answer I gave you, the second query with the WHERE clause should do what you want.

Regards,
Cobra

verachkaverachk
Graph Buddy

I tried the second one, and got only 1 duplicated node which was Graph1 that represents subGraphInstance, my problem is that i got it without all the connected elements of this node and I need them too

Cobra
Ninja
Ninja

All the nodes of the subgraph must be tagged with the property, not only one node.

verachkaverachk
Graph Buddy

Can you please elaborate how will the second query look like if will tag the other nodes there??

verachkaverachk
Graph Buddy

Also is there a way to create a query where I can choose specific nodes from my graph and duplicate them and get their connections ?

Cobra
Ninja
Ninja

I already gave you all information in other topic:

CALL gds.graph.project(
    "graph",            
    "*",             
    "*"               
)
YIELD graphName AS graph, nodeProjection, nodeCount AS nodes, relationshipProjection, relationshipCount AS rels
CALL gds.wcc.write('graph', { writeProperty: 'componentId' })
YIELD nodePropertiesWritten, componentCount;
CALL gds.graph.drop('graph', false) YIELD graphName;

So after you can change the 0 by the componentId you want:

MATCH (n)
WHERE n.componentId = 0
WITH collect(n) AS nodes
CALL apoc.refactor.cloneSubgraph(nodes)
YIELD input, output, error
RETURN input, output, error;

If you know which nodes you want to duplicates, you can change the WHERE clause in the previous query to select only the nodes you want.

verachkaverachk
Graph Buddy

Hey Cobra, thank you for your answers!
I only have the apoc library my version of neo4j is 3.5.30
so I tried to create a new graph, a simple one that looks loke this:


and I tried to duplicate query like this:

MATCH (n)
WHERE n.name = "Graph1" AND WHERE ID(type) ="CellType"
AND WHERE ID(type)="Function" AND WHERE ID(type) = "cellInstance" And WHERE ID(type) ="Connection"
AND WHERE ID(type) = "Variable"
WITH collect(n) AS nodes
CALL apoc.refactor.cloneSubgraph(nodes,[rel in relationships Where type(rel) = β€˜From_Graph’ AND Where type(rel) =’FROM_TYPE’ Where type(rel) = β€˜CELL_TYPE_FUNCTION’ AND Where type(rel) = β€˜FROM_CELL’ AND Where type(rel) = β€˜FUNCTION_INPUT_VARIABLE’ AND Where type(rel)= β€˜TO_CELL’,AND Where type(rel) = β€˜Functions_VARS’ ])
YIELD input, output, error
RETURN input, output, error;
still doesn't work for me 😞
any suggestions?

Cobra
Ninja
Ninja

You must use the latest version of Neo4j which is 4.4.6 then install GDS plugin on the database to use the queries I gave you.

verachkaverachk
Graph Buddy

Isn't there any other way to do it with this query you offered and use apoc only?
MATCH (n)
WHERE n.name = "Graph1"
WITH collect(n) AS nodes
CALL apoc.refactor.cloneSubgraph(nodes)
YIELD input, output, error
RETURN input, output, error;

Cobra
Ninja
Ninja

No because you need GDS to tag nodes correctly. Moreover Neo4j 3.5 is no longer maintained so you must upgrade.

verachkaverachk
Graph Buddy

Ok, so i'll try to upgrade it and use the queries you offered. i'll let you know if I succeeded
Cobra thank you so much for trying to help me I really
appriciate it.
have a lovely weekend!