[英]Creating relationships of nodes of the same kind in Neo4j Cypher
Here is a part from the dataframe that im working with:这是我使用的数据帧的一部分:
A_PERSON, B_PERSON, MESSAGE_TIME
SARAH, RON, 13:20 pm
RON, TIM, 13:33 pm
TIM, MARTHA, 13:45 pm
RON, LARRY, 13:35 pm
LARRY, JIM, 13:38 pm
I want to create a graph that shows all the messages sent between these people, and the arrows should show the time of the message.我想创建一个图表来显示这些人之间发送的所有消息,箭头应该显示消息的时间。
I've tried something like this:我试过这样的事情:
load csv with headers
from "file:\\df.csv" as row
create(m: Message {a_person: row.A_PERSON, b_person: row.B_PERSON, time: row.MESSAGE_TIME})
so that later I could MATCH them but it doesn't seem possible.这样以后我就可以匹配它们,但这似乎不可能。 The result has some A_PERSON duplicates in it because of the second and third label.由于第二个和第三个标签,结果中有一些 A_PERSON 重复。 So I don't understand how to create unique nodes and connect them to each other.所以我不明白如何创建独特的节点并将它们相互连接。 Is it possible to do?有可能吗? And if it is, then how?如果是,那又如何?
The key to this is to use the MERGE
clause.关键是使用MERGE
子句。 MERGE
each person and then MERGE
the relationship between the two people. MERGE
每个人,然后MERGE
两个人之间的关系。
MERGE
will create the person if they don't exist or MATCH
them if they do.如果他们不存在, MERGE
将创建该人,如果他们存在,则MATCH
他们。
LOAD CSV WITH HEADERS
FROM "file:\\df.csv" AS row
MERGE (a_person:Person {name: row.A_PERSON})
MERGE (b_person:Person {name: row.B_PERSON})
MERGE (a_person)-[:SENT_MESSAGE {time: row.MESSAGE_TIME}]->(b_person)
or, if you anted message to be a node, you could do something like this.或者,如果您将 message 设置为节点,则可以执行以下操作。
LOAD CSV WITH HEADERS
FROM "file:\\df.csv" AS row
MERGE (a_person:Person {name: row.A_PERSON})
MERGE (b_person:Person {name: row.B_PERSON})
MERGE (a_person)-[:SENT_MESSAGE]->(:Message {time: row.MESSAGE_TIME})-[:TO]->(b_person)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.