0
votes

I am stuck in the command in Neo4j (I am a newbie) to create a database based on a CSV like this:

Country,Name1,Name2,Name3,Influence
France,John,Pete,Josh,2
Italy,Pete,Bepe,Juan,3
USA,Josh,Juan,Pete,1
Spain,Juan,John,,2

When I try to create one node per person (NameX) setting the relationship between names columns adding the tags of Influence and Country,It fails because there are empty names.

How can achive this?

Thanks

UPDATE:

LOAD CSV WITH HEADERS FROM 'file:///diag.csv' AS row FIELDTERMINATOR ';' 
MERGE (c:Country{name:row.Country})
WITH CASE row.name1 WHEN NULL THEN [] WHEN '' THEN [] ELSE [row.name1] END as 
name1List ,c
WITH CASE row.name2 WHEN NULL THEN [] WHEN '' THEN [] ELSE [row.name2] END as 
name2List ,c
WITH CASE row.name3 WHEN NULL THEN [] WHEN '' THEN [] ELSE [row.name3] END as 
name3List ,c
FOREACH (x IN name1List | MERGE (n:Node{name : x} ) 
  MERGE (n)-[:REL_TYPE]->(c)
)
FOREACH (x IN name2List | MERGE (n:Node{name : x} ) 
  MERGE (n)-[:REL_TYPE]->(c)
)
FOREACH (x IN name3List | MERGE (n:Node{name : x} ) 
  MERGE (n)-[:REL_TYPE]->(c)
)
RETURN  SUM(1) 

Getting error:

Variable row not defined (line 4, column 11 (offset: 209)) "WITH CASE row.name2 WHEN NULL THEN [] WHEN '' THEN [] ELSE [row.name2] END as >name2List ,c"

2
if you can share the graph structure , i can write the complete query . But for now , i added the trick which you can use to get over this kind of problemTheTeacher
Thanks @TheTeacher, I tried your solution with the initial command LOAD CSV WITH HEADERS FROM 'file:///diag.csv' AS row FIELDTERMINATOR ';', but the problem is any of the three Name fields could be empty, not only Name3, so i am getting this error: Cannot merge node using null property value for namemahdsip
you have to apply case on all the fields or share the structure of your graph as i said earlier,.TheTeacher
I tried as showed in the updated code posted, but still getting an error regarding the variable 'row'. any idea?mahdsip
Hey, updated my answer for your query..you need pass the row in the WITH operatorTheTeacher

2 Answers

0
votes

The last line has empty Name3 field. Try adding Name3 to the last line in your data set.

Spain,Juan,John, {empty - fill this},2

0
votes
LOAD CSV WITH HEADERS FROM 'file:///diag.csv' AS row FIELDTERMINATOR ';' MERGE (c:Country{name:row.Country}) 
WITH CASE row.name1 WHEN NULL THEN [] WHEN '' THEN [] ELSE [row.name1] END as name1List ,
CASE row.name2 WHEN NULL THEN [] WHEN '' THEN [] ELSE [row.name2] END as name2List ,
CASE row.name3 WHEN NULL THEN [] WHEN '' THEN [] ELSE [row.name3] END as name3List ,c,row
FOREACH (x IN name1List | MERGE (n:Node{name : x} ) MERGE (n)-[:REL_TYPE]->(c) )
FOREACH (x IN name2List | MERGE (n:Node{name : x} ) MERGE (n)-[:REL_TYPE]->(c) ) 
FOREACH (x IN name3List | MERGE (n:Node{name : x} ) MERGE (n)-[:REL_TYPE]->(c) ) RETURN SUM(1)  

here, with the help of 'case' expression of cypher , we are creating either empty list when its null or empty or list with one value i.e (row.name3) .After case check , we can use this list to iterate and create node with property name3 . so , when its null or empty , you iterate zero times, so you wont get the error . Finally, sum(1) will give you number of rows you processed .so, you can cross check if you have processed all the rows in the csv file or not