Neo4j-如何合并来自多个CSV的节点和关系

时间:2019-11-13 18:43:29

标签: neo4j

我有2个csv文件,其数据如下:

"email.csv"
name,email
mike smith,msmith@test.com
mike smith,xsmith@test.com
mary smith,msmith@test.com
john roberts,jroberts@test.com

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

然后,当我使用此查询时:

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;

然后我得到结果输出:

enter image description here

我想要实现的是将同一个名称节点组合在一起,而电话和电子邮件的边缘将来自同一个名称节点。例如,“ john roberts”名称节点将同时具有他的电子邮件关系和电话关系,并且不分开。

1 个答案:

答案 0 :(得分:1)

如何做这样的事情?

为名称合并创建一个:User标签。您可以随后设置电子邮件标签。

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);

在第二个查询中使用:User标签,以便MERGE将找到预先存在的名称节点(如果确实存在),而不是创建一个新的名称节点。

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);