如何在neo4j中显示关系

时间:2018-04-03 14:07:22

标签: csv neo4j cypher graph-databases

  

我已将示例输入逗号分隔值文件和手绘图像附加到下方显示我的预期输出。

Problem Description:
--------------------

我有一个包含节点列表的CSV文件,其中每一行表示第[0]行节点与节点行[2],第[2]行,第[3]行......的每个其他列表的关系。 .line [4500]在该行

实施例。示例输入文件:

0,1,2,3 (line 1)  
1,2 (line 2)    
2,4 (line 3)  
3,7,19 (line 4)  
10,4,5,11 (line 5)

请注意(第1行),(第2行)等实际CSV文件中不存在。在这里,我只是为了解释而命名它们。

for line 1 (Let 'line') node at 
line[0] i.e. "0" has a directed "friends" relationship with 
nodes at line[2] i.e "1" 
nodes at line[4], i,e."2"
nodes at line[6], i,e."3"

similarly again for line 2 (Let 'line') node at 
line[0] i.e. "1" has a directed "friends" relationship with 
nodes at line[2] i.e "2" 

similarly again for line 3(Let 'line')  node at 
line[0] i.e. "2" has a directed "friends" relationship with 
nodes at line[2] i.e "4" 


similarly again for line 4(Let 'line')  node at 
line[0] i.e. "3" has a directed "friends" relationship with 
nodes at line[2] i.e "7" 
nodes at line[4], i,e."19"

and so on....

我想要做的是我想在Neo4j中显示一个图形,描绘每个节点之间建议的朋友关系。

我无法弄清楚如何迭代整个 csv 文件,以及捕获 csv 文件每行上每个节点之间的关系。

请注意:[我附上了样本输入文件的预期手绘输出。]

  

Link to Comma separated value file containing the friends

     

Link to Image

1 个答案:

答案 0 :(得分:3)

首先,将您的CSV文件保存到Neo4j导入目录中(查看Neo4j files location docs)。之后,使用Neo4j LOAD CSV语句导入您的数据。我用来重现你想要的输出的脚本如下:

// Load the csv file
load csv from "file:///friends.csv" as line
// calculate the indexes from the second column to the last
with line, range(1, size(line) - 1) as indexes
// merge (create or assign) the node from first column (0,1,2,3,10)
merge(a:Node{id:line[0]})
// pass 'a', 'indexes' and 'line' to the next context
with a, indexes, line
// unwind the indexes into single index per row
unwind indexes as index
// merge (create or assign) the node from other columns
merge(b:Node{id:line[index]})
// merge the relationship between a and b
merge(a)-[:FRIEND_OF]->(b)

考虑您的样本CSV文件的输出将是:

Output