简体   繁体   English

在 Neo4j Cypher 中创建同类节点的关系

[英]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.

相关问题 在 Neo4j 中创建节点和关系,输入为 pandas DataFrame 与 py2neo - Create nodes and relationships in Neo4j with inputs as a pandas DataFrame with py2neo 有没有办法在Pandas dataframe中建立neo4j的关系? - Is there any way to make relationship of neo4j in Pandas dataframe? 连接两个具有相同索引的 df - Concatenate two df with same kind of index 如何创建布局,将同一组中的节点拼接在一起? - How to create layout that will plot nodes in the same group close together? R:如何以更高效,更紧凑的方式编写代码,以使用相同类型的变量重复代码 - R: How to write it in more efficient, more compact, way the code that repeats using the same kind of variables TypeError: (typecode 'l') 根据转换规则 ''same_kind'' 使用互相关 - TypeError: (typecode 'l') according to the casting rule ''same_kind'' while using cross-correlation 使用同一小时的多行在 R 中创建 dataframe - Creating dataframe in R with multiple lines of the same hour 合并 2 个具有相同列标题的数据框,创建子标题 - Merge 2 dataframes with same column headers creating subheaders 通过在 R 中创建相同的列来复制行 - Duplication Rows by creating same columns in R 在同一个反应函数中创建多个数据框并分别输出 - Creating multiple Data Frames in the same reactive function and outputting each separately
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM