Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
07-03-2020 06:52 AM
Hi All,
Is there an option to use IF/CASE like conditional check before creating a node or a relationship while loading with LOAD CSV?
For ex, if I have a,b,c,d,e attributes/columns in a csv and while loading I want to create a node based on a,b,c values if they are not null and d,e (if not null) are used to create a relationship to an existing node.
So, is there something like:
LOAD CSV WITH HEADERS FROM "file:///sample.csv" as row
CASE WHEN row.a is not null THEN
create (n:NewNode {prop1:row.b, prop2:row.c})
IF (row.d is not null) THEN
match (e:ExistingNode {value1:row.d})
create (n)-[:newRelation]->(e)
END
Solved! Go to Solution.
07-03-2020 02:06 PM
Take a look at the APOC library's conditional Cypher execution procedures - https://neo4j.com/docs/labs/apoc/current/cypher-execution/conditionals/
I think you should be able to do what you want with those.
07-03-2020 12:21 PM
Hi Gopal,
As we know that Neo4j Nodes and Relationships are not bounded with hardcore schema definition as RDBMS. That means a Node1 and Node2 belong to label XYZ may have different property list, for an example Node1 have property prop1 and Node2 has properties prop1,prop2.
Usually this happens because value of prop2 for Node1 is Null.
I guess this is the what your usecase too.
You have not make sure that the identifier of the Node must be unique and not null
So while creating node as per above example consider prop1 is identifier property then
Following code will do
Load csv from 'file:///test.csv' as row
Merge(a:XYZ{prop1:row[0]}) set a.prop2=row[1]
Similarly you can design for relationships.
For better understanding kindly visit Neo4j documentation.
Incase you face any problem we are here to help you
07-03-2020 09:42 PM
Thanks Vivek for your time to respond back but I was looking for something more like a conditional execution while loading CSV. Seems like apoc procedures might help with that.
07-03-2020 02:06 PM
Take a look at the APOC library's conditional Cypher execution procedures - https://neo4j.com/docs/labs/apoc/current/cypher-execution/conditionals/
I think you should be able to do what you want with those.
07-03-2020 09:40 PM
ThanX Mark. This looks promising, will try the same.
All the sessions of the conference are now available online