在Neo4j中导入具有空值的CSV文件

时间:2017-10-24 09:40:46

标签: csv graph neo4j merge cypher

这应该是一项非常简单的任务,但我正在努力。对于图形数据库和Neo4j专家的所有人,我真的很感激一些帮助,让我的生活更轻松一些。我似乎无法在CSV文件中找出这个带有NULL值的MERGE子句。

Example.csv文件包含

Name,Lastname,Team,Room

AAAA,BBBB,CCCC,1111

DDDD,EEEE,FFFF,2222

GGGG,HHHH,,3333

IIII,JJJJ,KKKK,

LLLL,MMMM,CCCC,1111

NNNN,OOOO,,2222

执行时

LOAD CSV WITH HEADERS FROM 'file:///EXAMPLE.csv' AS row WITH row RETURN row

我可以看到像Team和Room这样的字段有空值。我想为Employees,Team和Room创建节点,这是我到目前为止编写的代码。

CREATE CONSTRAINT ON (t:Team) ASSERT t.name IS UNIQUE;

CREATE CONSTRAINT ON (r:Room) ASSERT r.name IS UNIQUE;

LOAD CSV WITH HEADERS FROM 'file:///EXAMPLE.csv' AS row WITH row WHERE row.Room <> '' AND row.Room IS NOT NULL AND row.Team <> '' AND row.Team IS NOT NULL

CREATE (m:Employee {name: toUpper(row.Lastname), firstname: toUpper(row.Name)})

MERGE (r:Room { name:row.Room})

MERGE (t.Team {name:row.Team})

CREATE (m)-[:WORKS_IN]->(r)

CREATE (m)-[:WORKS_WITH]->(t);

我猜你已经猜到了一些空值的条目被取消而没有被取消。我希望将Employees保留在数据库中,即使它没有部门或团队中的条目,即。如果为null,则不存在关系。

我非常感谢你们的帮助,并提前多多感谢。

1 个答案:

答案 0 :(得分:1)

您可以尝试使用FOREACHCASE WHENhandle conditions。像这样:

CREATE (m:Employee {name: toUpper(row.Lastname), firstname: toUpper(row.Name)})
FOREACH (n IN (CASE WHEN row.Room IS NULL THEN [] ELSE [1] END) |
    MERGE (r:Room { name:row.Room})
    CREATE (m)-[:WORKS_IN]->(r)
)
FOREACH (n IN (CASE WHEN row.Team IS NULL THEN [] ELSE [1] END) |
    MERGE (t:Team {name:row.Team})
    CREATE (m)-[:WORKS_WITH]->(t)
)

当相关行与null不同时,FOREACH块只会执行一次。