【问题标题】:How to show the relationship in neo4j如何在neo4j中显示关系
【发布时间】:2018-04-03 14:07:21
【问题描述】:

我在下面附上了示例输入逗号分隔值文件和一张手绘图像,显示了我的预期输出。

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

我有一个 CSV 文件,其中包含一个节点列表,其中每一行表示 line[0] 处的节点与每个其他节点列表 line[2],line[2],line[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

【问题讨论】:

  • 你想用Java写这个吗?您已经走了多远 - 是 CSV 处理还是您遇到问题的 Node4J 方面?如果您需要处理 CSV 的方法,Apache CSV 库非常好。
  • 不,我想在 Neo4j 图形数据库中显示关系。
  • 好的,但是您如何尝试将这些数据导入 Neo4J 以便可视化?当您说“我不知道如何迭代整个 csv 文件”时,您是否尝试过编写一些代码来做到这一点?

标签: csv neo4j cypher graph-databases


【解决方案1】:

首先,将您的 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 文件的输出将是:

【讨论】:

  • 感谢 Bruno Peres 的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 2021-09-22
  • 2018-02-19
相关资源
最近更新 更多