简体   繁体   English

Neo4j:使用 Cypher 创建具有唯一节点的图

[英]Neo4j: Create a graph with unique nodes using Cypher

I have a table of senders and receivers:我有一个发送者和接收者表:

Table A:

+------+--------+                                                          
|sender|receiver|
+------+--------+ 
|     A|       B|
|     B|       C|
|     C|       D|
|     D|       A|
|     E|       B|
|     A|       D|
|     E|       C|
+------+--------+  

I want to create a graph with connections from 'sender' to 'receiver'.我想创建一个从“发送者”到“接收者”的连接图。 For example, A -(rel)-> B例如, A -(rel)-> B

The values in the sender column can also appear in the receiver column.发送者列中的值也可以出现在接收者列中。 However, I do not want the nodes A,B,C,D, and E to be repeated in my graph.但是,我不希望节点 A、B、C、D 和 E 在我的图中重复。 They must only appear once in the final graph.它们只能在最终图表中出现一次。

I tried doing this:我试过这样做:

LOAD CSV WITH HEADERS FROM 'file:///graph' AS row
CREATE (:sender_node { sender: row.sender})

MATCH (n:sender_node)
WITH n.sender AS sender, collect(n) AS nodes
WHERE size(nodes) > 1
FOREACH (n in tail(nodes) | DELETE n)

LOAD CSV WITH HEADERS FROM 'file:///graph' AS row
CREATE (:receiver_node { receiver: row.receiver})

MATCH (n:receiver_node)
WITH n.receiver AS receiver, collect(n) AS nodes
WHERE size(nodes) > 1
FOREACH (n in tail(nodes) | DELETE n)

LOAD CSV WITH HEADERS FROM 'file:///graph' AS row
MATCH (from_var:sender_node {sender: row.sender}),(to_var:receiver_node {receiver:row.receiver})
CREATE (from_var)-[:rel]->(to_var)
RETURN *

I basically deleted the duplicates separately in sender and receiver nodes.我基本上在发送方和接收方节点中分别删除了重复项。 But since the nodes A, B, C, and D appear in both 'sender' and 'receiver' columns, they appear twice in the graph.但由于节点 A、B、C 和 D 出现在“发送方”和“接收方”列中,因此它们在图中出现了两次。 I want to correct this part.我想纠正这部分。

The start node of an EMAILS relationship is implicitly a sender. EMAILS关系的起始节点隐含地是发送者。 And the end node of an EMAILS relationship is implicitly a receiver. EMAILS关系的结束节点隐含地是一个接收者。 So you should not have different labels for the sender and receiver node, which is redundant and makes your data model overly complex.因此,发送者和接收者节点不应该有不同的标签,这是多余的,并且会使您的数据 model 过于复杂。

Instead, you should just use, say, the User label for both kinds of nodes.相反,您应该只对这两种节点使用User label。 That will also help you to avoid duplicates, especially when you use the MERGE clause instead of CREATE .这也将帮助您避免重复,尤其是当您使用MERGE子句而不是CREATE时。

For example:例如:

LOAD CSV WITH HEADERS FROM 'file:///graph' AS row
MERGE (sender:User {id: row.sender})
MERGE (receiver:User {id: row.receiver})
MERGE (sender)-[:EMAILS]->(receiver)
RETURN *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM