【问题标题】:Neo4j import csv with spliting values in a columnNeo4j 导入 csv 并在一列中拆分值
【发布时间】:2019-12-27 08:17:05
【问题描述】:

我想把下面的数据放到一个neo4j 数据库中。 “朋友”列是由“,”分隔的一串 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})

这是正确的方法吗?以及如何简化代码?

【问题讨论】:

    标签: python pandas csv neo4j cypher


    【解决方案1】:

    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))
    

    所有值都是字符串,因此如果需要,您需要转换为其他数据类型(请参阅年龄)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      • 2020-01-30
      • 2021-08-03
      • 2021-09-02
      • 2017-11-18
      • 1970-01-01
      相关资源
      最近更新 更多