Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
04-23-2022 12:18 AM
Hello,
can I duplicate the following graph in neo4j? and if it is possible, then what cyphers can can me?
Solved! Go to Solution.
05-11-2022 03:47 AM
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.
04-23-2022 12:26 AM
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
04-23-2022 02:43 AM
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?
04-23-2022 04:41 AM
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 *
05-10-2022 07:09 AM
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 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
05-10-2022 07:14 AM
Can you share your dataset please?
05-10-2022 10:00 AM
Where get I get it from?(the dataSet) so I can send it here?
05-11-2022 12:04 AM
I can try to export the data I have to one of these:
or dataSet means something else?
05-11-2022 12:18 AM
The dataset is the data, can you share queries to recreate the graph?
05-11-2022 01:26 AM
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
05-11-2022 02:27 AM
and if you meant they tables that represents the data of this graph for example:
05-11-2022 03:47 AM
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.
05-11-2022 04:17 AM
I will try it and let you know today! ,thank you so so much for trying to help me understand !!
05-11-2022 06:38 AM
yeahi! it works!
thank you so much for helping me
05-18-2022 02:18 AM
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
05-18-2022 03:19 AM
Hello @verachkaverachka
It's in the answer I gave you, the second query with the WHERE
clause should do what you want.
Regards,
Cobra
05-18-2022 03:24 AM
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
05-18-2022 04:22 AM
All the nodes of the subgraph must be tagged with the property, not only one node.
05-18-2022 10:26 AM
Can you please elaborate how will the second query look like if will tag the other nodes there??
05-18-2022 10:41 AM
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 ?
05-18-2022 11:15 PM
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.
05-19-2022 01:37 AM
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:
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?
05-19-2022 01:55 AM
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.
05-19-2022 04:00 AM
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;
05-19-2022 06:45 AM
No because you need GDS to tag nodes correctly. Moreover Neo4j 3.5 is no longer maintained so you must upgrade.
05-19-2022 07:12 AM
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!
All the sessions of the conference are now available online