bkt619
bkt619

Reputation: 81

Neo4j - How to combine nodes and relationships from multiple CSVs

I have 2 csv files with data that looks like:

"email.csv"
name,email
mike smith,[email protected]
mike smith,[email protected]
mary smith,[email protected]
john roberts,[email protected]

"phone.csv"
name,phone
mike smith,714-555-5555
mary smith,714-456-4567
john roberts,714-555-5555
john roberts,714-456-4567

Then when I use this query:

LOAD CSV WITH HEADERS FROM 'file:///email.csv' AS email
WITH email
MERGE(e:Email_name{name:email.name})
MERGE(ee:Email_email{email:email.email})
MERGE(e)-[:has_email]->(ee);

LOAD CSV WITH HEADERS FROM 'file:///phone.csv' AS phone
WITH phone
MERGE (p:Phone_name{name:phone.name})
MERGE (pp:Phone_phone{phone:phone.phone})
MERGE (p)-[:has_phone]->(pp);

MATCH p=()-[*]->() RETURN p;

And I get the resulting output:

enter image description here

What i am trying to achieve is that same name nodes would be combined and the phone and email would edge would come out of the same name node. For example the "john roberts" name node would have both his email relationship and phone relation coming from it and not separated.

Upvotes: 1

Views: 75

Answers (1)

Dave Bennett
Dave Bennett

Reputation: 11216

How about doing something like this?

Create a :User label for the name merge. You can set the email label afterwards.

LOAD CSV WITH HEADERS FROM 'file:///email.csv' AS email
WITH email
MERGE(e:User {name:email.name}) SET e:Email_name
MERGE(ee:Email_email{email:email.email})
MERGE(e)-[:has_email]->(ee);

Use the :User label in the second query so that the MERGE will find the pre-existing names node (if indeed it does exist) instead of creating a new one.

LOAD CSV WITH HEADERS FROM 'file:///phone.csv' AS phone
WITH phone
MERGE (p:User {name:phone.name}) SET p:Phone_name
MERGE (pp:Phone_phone{phone:phone.phone})
MERGE (p)-[:has_phone]->(pp);

Upvotes: 1

Related Questions