简体   繁体   English

Neo4j / Cypher条件语句,用于加载数据

[英]Neo4j/Cypher Conditional Statements for Loading Data

In Neo4j, is there a simple way to combine WHERE statements to avoid reading through the same file multiple times while simultaneously ignoring NULL values. 在Neo4j中,有一种简单的方法可以组合WHERE语句,以避免多次读取同一文件,而同时忽略NULL值。

For example, is it possible to go through a CSV a single time, and do something like the following 例如,是否可以一次通过CSV并执行以下操作

LOAD CSV WITH HEADERS from "file" as line
WITH line, line.FirstNodeID as NodeOneID
WHERE NodeOneID IS NOT NULL
WITH NodeOneID
CREATE (n1:NodeOne { ID : NodeOneID })
WITH line, line.SecondNodeID as NodeTwoID
WHERE NodeTwoID IS NOT NULL
WITH NodeTwoID
CREATE (n2:NodeTwo { ID : NodeTwoID})
;

As of now, the LOAD script will exit if NodeOneID is null even if NodeTwoID is NOT null. 到目前为止,即使NodeTwoID不为null,如果NodeOneID为null,也会退出LOAD脚本。 How can I build in a simple if-else like condition where the LOAD script will load the proper nodes as long as NodeOneID or NodeTwoID is not null? 如果NodeOneID或NodeTwoID不为null,那么如何在类似if-else之类的简单条件下构建加载脚本将加载正确的节点的条件?

UPDATE: Just saw CASE . 更新:刚刚看到了CASE Gonna check this out real quick.. 会很快检查出来的..

I think you could do something like this for each line. 我认为您可以为每一行做这样的事情。 If the value is not null put it in a collection of one. 如果该值不为null,则将其放入1的集合中。 Then if the collections have an item the node(s) is/are created and if they are empty no nodes are created. 然后,如果集合中有一个项目,则创建一个或多个节点,如果集合为空,则不创建任何节点。 If they both have values then both nodes are created. 如果它们都有值,那么将创建两个节点。

LOAD CSV WITH HEADERS from "file" as line
WITH line
, case when line.FirstNodeID is not null then [line] else [] end as NodeOneID
, case when line.SecondNodeID is not null then [line] else [] end as NodeTwoID
foreach(x in NodeOneID | CREATE (n1:NodeOne { ID : x.FirstNodeID }))
foreach(x in NodeTwoID | CREATE (n2:NodeTwo { ID : x.SecondNodeID }))

Does this work for you? 这对您有用吗?

LOAD CSV WITH HEADERS from "file" as line
foreach(x in COALESCE(line.FirstNodeID)  | CREATE (:NodeOne { ID : x }))
foreach(x in COALESCE(line.SecondNodeID) | CREATE (:NodeTwo { ID : x }));

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

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