[英]Import Graphml file into neo4j using Python and export it to CSV
[英]Neo4j import csv with spliting values in a column
我想将以下数据导入到neo4j 数据库中。 “friends”列是一串由“,”分隔的id。 因此,应该有 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})
这是正确的方法吗? 以及如何简化代码?
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))
所有值都是字符串,因此您需要根据需要转换为其他数据类型(请参阅年龄)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.