简体   繁体   English

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

[英]Import a CSV file with null values in Neo4j

This is supposed to be a really easy task and yet I am struggling.这应该是一项非常简单的任务,但我正在努力。 To all the people who are expert in a graph-database and Neo4j I would really appreciate some help and make my life a bit easier.对于所有图形数据库和 Neo4j 专家,我真的很感激一些帮助,让我的生活更轻松一些。 I can't seem to figure out this MERGE clause with NULL values in CSV files.我似乎无法在 CSV 文件中找出带有 NULL 值的 MERGE 子句。

Example.csv file contains 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

When executing执行时

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

I can see some fields like Team and Room has null values.我可以看到诸如 Team 和 Room 之类的一些字段具有空值。 I would like to create nodes for Employees, Team and Room and here is the code I have written so far.我想为员工、团队和房间创建节点,这是我迄今为止编写的代码。


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

I guess you already guessed some entries with null values are cancelled and not taken.我猜你已经猜到了一些带有空值的条目被取消而不是被采用。 I want to keep the Employees within the database even it has no entries in department or team ie.我想将员工保留在数据库中,即使它在部门或团队中没有条目,即。 no relationship exists if null.如果为 null,则不存在任何关系。

I would really appreciate some help and many thanks in advance.我真的很感激一些帮助,并在此先感谢。

You can try using FOREACH and CASE WHEN to handle conditions .您可以尝试使用FOREACHCASE WHEN处理条件 Something like this:像这样的东西:

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

The FOREACH block will be executed only once when the related line is different of null. FOREACH块只会在相关行不为空时执行一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM