Reputation: 587
I have a .csv file with three columns separated by ",". The first column contains first names, the 2nd column contains the last names and the 3rd column contains values:
Lname,Fname,Num
Brown,Helen,3
Right,Eliza,1
Green,Helen,3
Pink,Kate,2
Yellow,Helen,3
and another .csv file like this:
central,value
cent1,10
I want to create nodes for Lname column and nodes for Fname column. For the rows that have the same Fname, I want to connect Lname to the corresponding Fname. For example I want to have a "Helen" node that three nodes "Brown", "Green" and "Yellow" connected to "Helen" (Also if Fname is identical, Num is identical too) I also want to connect "Fname" nodes to a "central node". This is my desired output:
I use this code:
LOAD CSV WITH HEADERS FROM 'file:///central.csv' AS frow
MERGE (c:center {name: frow.central})
WITH *
LOAD CSV WITH HEADERS FROM 'file:///names.csv' AS srow
WITH srow.Fname AS first, srow.Lname AS last, srow.Num as num
MERGE (p:la {last: last})
MERGE (o:fi {first: first, num: num})
MERGE (c)-[r:CONTAINS {first:first}]->(o)
MERGE (o)-[rel:CONTAINS {first: first}]->(p)
RETURN count(o)
When I click on each first
node in Neo4j, I expect to see first
and num
as its properties because of this line:
MERGE (o:fi {first: first, num: num})
but using this code, only one of the first
nodes contains the num
as its properties. Other first
nodes do not show the num
as their properties. What's wrong?
Upvotes: 0
Views: 140
Reputation: 66999
Your names.csv
file has an error. Helen
appears with 2 different Num
values: 2 and 3. This is why your query created 2 different Helen
nodes. The file should look like this:
Lname,Fname,Num
Brown,Helen,3
Right,Eliza,1
Green,Helen,3
Pink,Kate,2
Yellow,Helen,3
Your second WITH
clause did not specify c
, so c
became an unbound variable. This caused the MERGE (c)-[:CONTAINS]->(o)
clause to create a new c
node instead of re-using the original c
node.
This is a fixed version of your query:
LOAD CSV WITH HEADERS FROM 'file:///central.csv' AS frow
MERGE (c:center {name: frow.central})
WITH c
LOAD CSV WITH HEADERS FROM 'file:///names.csv' AS srow
WITH c, srow.Fname AS first, srow.Lname AS last, srow.Num as num
MERGE (p:la {last: last})
MERGE (o:fi {first: first, num: num})
MERGE (c)-[:CONTAINS]->(o)
MERGE (o)-[:CONTAINS]->(p)
RETURN count(o)
Note: I also removed the {first:first}
property settings from the CONTAINS
relationships, as it is not recommendeded to store redundant information in the DB. One of the end nodes of that relationship is always going to have the first name anyway. In fact, it would also be best practice to use different relationship types (e.g., CONTAINS
and HAS_LAST_NAME
).
Upvotes: 2