Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
08-26-2019 05:05 PM
Hello,
I have a graph that I need to copy as is but rename the node labels and the relationship types in the duplicated graph.
What is the neo4j recommended way of doing this?
Thanks!
08-26-2019 06:21 PM
You can use CYPHER to refactor a graph database.
If you need a new graph database that is a copy of the old one (no need to keep them in sync henceforth) you can do the following:
08-26-2019 07:57 PM
Thanks for the reply.
However, I am trying to do this in real time and in the same database.
08-26-2019 11:44 PM
@dmellonielet Sorry, I misread your intention when said 'copy'. Perhaps you can use: http://neo4j-contrib.github.io/neo4j-apoc-procedures/3.5/operational/triggers/
08-28-2019 03:59 PM
APOC . also has some clone procedures and then you can use apoc.refactor.setType and similar procedures to update your data.
08-28-2019 05:17 PM
Hi Michael,
Thank you for the reply. I did explore APOC and it looks very powerful. However, I have an issue with renaming the labels and relationship types. My APOC query looks like this:
MATCH (rootA:xyz_event{id:'e101'}),
(rootB:abc_event{id: 'e101'})
MATCH path = (rootA)-[*]->(node)
WITH rootA, rootB, collect(path) as paths
CALL apoc.refactor.cloneSubgraphFromPaths(paths, {standinNodes:[[rootA, rootB]]}) YIELD input, output, error
RETURN input, output, error
If I try to call a procedure to rename the label: call apoc.refactor.rename.label('xyz_content', 'abc_content') within the above query, it gives me an error:
Neo.ClientError.Statement.SyntaxError: Procedure call inside a query does not support naming results implicitly (name explicitly using YIELD
instead) (line 4, column 1 (offset: 111)) "call apoc.refactor.rename.label('xyz_content', 'abc_content') " ^
Can I use the procedures to rename labels and relationship types within the same above query? If so how? I could not find any good examples of this.
08-29-2019 05:04 PM
Hi @dmellonielet,
The specific error you are seeing using apoc.refactor.rename.label
is because it expects you to YIELD
something, so the command should be e.g.
CALL apoc.refactor.rename.label("xyz_assets","abc_assets", nodes) YIELD errorMessages
The list of things you can YIELD
can be found by e.g. call apoc.help("rename.label")
There are a couple of other challenges here
The output of ..cloneSubgraphFromPaths
will only give you node information (so if you are wanting to change the relationship labels too you'll need to query again).
Because you have multiple labels that you are trying to change you would need to run the ..rename.labels
multiple times
An example is of a second query that could do this is:
MATCH (rootB:abc_event{id: 'e101'})
MATCH (rootB)-[r*]->(node)
// get distinct nodes
with collect(distinct node) as nodes, r
CALL apoc.refactor.rename.label("xyz_assets","abc_assets", nodes) YIELD errorMessages as labelErrors1
CALL apoc.refactor.rename.label("xyz_content","abc_content", nodes) YIELD errorMessages as labelErrors2
//get distinct rels
with r, nodes
unwind r as rel
with collect(distinct rel) as rels
CALL apoc.refactor.rename.type("xyz_event_has_xyc_content","abc_event_has_abc_content", rels) YIELD errorMessages as relErrors1
CALL apoc.refactor.rename.type("xyz_content_has_xyc_media","abc_content_has_abc_media", rels) YIELD errorMessages as relErrors2
return rels
09-27-2022 12:55 PM
Thanks for the sample query.
I have a similar problem, where I need to clone a graph:
-The original graph must remain the same
-The nodes' label from the original graph have a unique constraint on a property
-The nodes' in the cloned graph must have a different Label
-The nodes in the cloned graph must have the same property and it's value as the original node from the original graph
Im trying to use virtual nodes/relationships but it is getting harder to map relationships' start and end nodes
09-27-2022 12:32 PM - edited 09-27-2022 12:53 PM
Hi Michael,
Thanks for the sample query.
I have a similar problem, where I need to clone a graph:
-The original graph must remain the same
-The nodes' label from the original graph have a unique constraint on a property
-The nodes' in the cloned graph must have a different Label
-The nodes in the cloned graph must have the same property and it's value as the original node from the original graph
Im trying to use virtual nodes/relationships but it is getting harder to map relationships' start and end nodes
All the sessions of the conference are now available online