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
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
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.
1 Like
ThanX Mark. This looks promising, will try the same.
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.