cancel
Showing results for 
Search instead for 
Did you mean: 

create relationships from csv with distinct node

Hello, 

I have a CSV file as below:

 

 

node01,relationship,node02
A_01,B_dependency,B_01
A_02,B_dependency,B_02
A_03,A_dependency,A_01
A_03,A_dependency,A_02
A_04,B_dependency,B_04

 

 

I hope the graphe like below:

001001

 

 

 

 

 

 

 

My cypher code :

 

 

 

 :auto USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM
'file:///test.csv' AS line
MERGE (n1:node01{name: line.node01})
WITH line,n1
MERGE (n2:node02{name: line.node02})
WITH line,n1,n2
create (n1) -[:relate {relate: line.relationship}]-> (n2)

 

 

But, I get this graphe...

002002

 

 

 

 

 

 

 

How should I go about doing this ?thanks !

 

 

 

1 ACCEPTED SOLUTION

ameyasoft
Graph Maven

Try this. First create all nodes.

LOAD CSV WITH HEADERS FROM "file:/chiang.csv" AS row

with row, left(row.node01, 1) as s1, left(row.node02, 1) as s2

FOREACH(ignoreMe IN CASE WHEN s1 = "A" THEN [1] ELSE [] END|

MERGE (n1:node01{name: row.node01})

)

FOREACH(ignoreMe IN CASE WHEN s2 = "A" THEN [1] ELSE [] END|

MERGE (n1:node01{name: row.node02})

)

FOREACH(ignoreMe IN CASE WHEN s1 = "B" THEN [1] ELSE [] END|

MERGE (n2:node02{name: row.node01})

)

FOREACH(ignoreMe IN CASE WHEN s2 = "B" THEN [1] ELSE [] END|

MERGE (n2:node02{name: row.node02})

)

Next create relationships;

LOAD CSV WITH HEADERS FROM "file:/chiang.csv" AS row

match (n1) where n1.name = row.node01
match (n2) where n2.name = row.node02
with n1, n2, row
MERGE (n1) -[:relate {relate: row.relationship}]-> (n2)

Result:Screen Shot 2022-06-28 at 12.35.14 PM.png

View solution in original post

1 REPLY 1

ameyasoft
Graph Maven

Try this. First create all nodes.

LOAD CSV WITH HEADERS FROM "file:/chiang.csv" AS row

with row, left(row.node01, 1) as s1, left(row.node02, 1) as s2

FOREACH(ignoreMe IN CASE WHEN s1 = "A" THEN [1] ELSE [] END|

MERGE (n1:node01{name: row.node01})

)

FOREACH(ignoreMe IN CASE WHEN s2 = "A" THEN [1] ELSE [] END|

MERGE (n1:node01{name: row.node02})

)

FOREACH(ignoreMe IN CASE WHEN s1 = "B" THEN [1] ELSE [] END|

MERGE (n2:node02{name: row.node01})

)

FOREACH(ignoreMe IN CASE WHEN s2 = "B" THEN [1] ELSE [] END|

MERGE (n2:node02{name: row.node02})

)

Next create relationships;

LOAD CSV WITH HEADERS FROM "file:/chiang.csv" AS row

match (n1) where n1.name = row.node01
match (n2) where n2.name = row.node02
with n1, n2, row
MERGE (n1) -[:relate {relate: row.relationship}]-> (n2)

Result:Screen Shot 2022-06-28 at 12.35.14 PM.png