Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
11-13-2018 12:29 AM
I am reading data from a file and want to create nodes and relations between them. There is a chance that either of the two nodes of the relation are not existing by now.
I solved this by:
LOAD CSV WITH HEADERS FROM "foobar.csv" AS ROW
MERGE (a:TypeA { name: row.name, revision: toInt(row.revision) })
MERGE (b:TypeB { name: row.name }) ;
LOAD CSV WITH HEADERS FROM "foobar.csv" AS ROW
MATCH (a:TypeA { name: row.name, revision: toInt(row.revision) })
MATCH (b:TypeB { name: row.name })
MERGE (a) -[r:is_instance]-> (b) ;
This loads the csv two times, which is not optimal.
I have the following cases:
Using the second variant that MATCH against (b) fails to create anything in the first case, because there is no MATCH at all.
Is there any other way to avoid using the double loop over csv?
Thanks
11-13-2018 09:12 AM
Sure, just use MERGE for all 3:
LOAD CSV WITH HEADERS FROM "foobar.csv" AS row
MERGE (a:TypeA { name: row.name, revision: toInt(row.revision) })
MERGE (b:TypeB { name: row.name })
MERGE (a) -[r:is_instance]-> (b) ;
11-13-2018 09:37 PM
Thanks, - I guess without the ; after the second MERGE.
I somehow assumed that the first two MERGE will not be ready for the third one.
Thanks for the quick response, and sorry for the rather stupid question
11-14-2018 03:28 PM
My mistake on the ;
, and you're welcome!
No stupid questions here, learning is full of trial and error.
11-16-2018 12:50 PM
I think doing it all in one go will cause an EAGER (run it through an EXPLAIN). If you have a large CSV this could lead to problems. Might be better to iterate over the file twice. Just something to keep an eye on, if it works loading all at once then no worries.
11-16-2018 02:35 PM
In this case since the nodes are of two different types, no Eager will be introduced.
11-16-2018 06:26 PM
Thanks for the clarification, good to know!
11-16-2018 07:40 PM
I don't know about EAGER, sorry, but I anyway added a USING PERIODIC COMMIT 1000
before each load.
11-17-2018 05:29 AM
I was mistaken, you only have EAGER if the nodes had the same label. If you run:
EXPLAIN
LOAD CSV WITH HEADERS FROM "foobar.csv" AS row
MERGE (a:TypeA { name: row.name, revision: toInt(row.revision) })
MERGE (b:TypeB { name: row.name })
MERGE (a) -[r:is_instance]-> (b) ;
in the browser it comes back with the "query plan". If you see a line that just says "EAGER" when loading a CSV, its bad news. You should be good
All the sessions of the conference are now available online