我想把下面的数据放到neo4j数据库中。"friends“列是由",”分隔的ids字符串。因此,应该有10个节点(id为1-10),其中只有5个我知道它们的年龄。我想在每个id和他们的朋友之间建立关系。
示例数据帧
id age friends
1 10 "3,2"
2 20 "1,6"
3 15 "4,5,10"
4 13 "2,8,9"
5 25 "1,4,7"
我使用的代码是
LOAD CSV WITH HEADERS FROM 'file:///df.csv' AS line
MERGE (User {id: line.id, age: line.age})
LOAD CSV WITH HEADERS FROM 'file:///df.csv' AS line
UNWIND split(line.friends, ',') AS friends
MERGE (u:User {id: friends})
LOAD CSV WITH HEADERS FROM 'file:///df.csv' AS line
UNWIND split(line.friends, ',') AS friends
With line, friends
MERGE (User1{id: line.id})-[:FRIENDS]->(User2{id: friends})
这是正确的做法吗?如何简化代码?
发布于 2019-12-29 02:52:30
foreach
应该做到这一点:
LOAD CSV WITH HEADERS FROM 'file:///df.csv' AS line
MERGE (u:User {id: line.id}) SET u.age=toInteger(line.age)
WITH u,line,split(line.friends, ',') AS friends
FOREACH (f in friends | merge (friend:User {id: f}) merge (u)-[:FRIENDS]->(friend))
所有值都是字符串,因此如果需要,您将需要转换为其他数据类型(请参阅年龄)。
https://stackoverflow.com/questions/59497863
复制