找到你要的答案

Q:Neo4j/Cypher Conditional Statements for Loading Data

Q:Neo4j /密码条件加载数据报表

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.

For example, is it possible to go through a CSV a single time, and do something like the following

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. 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?

UPDATE: Just saw CASE. Gonna check this out real quick..

在Neo4j的,有没有简单的方法将在报表避免阅读同一个文件多次同时忽略空值。

例如,它可能要经过一个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})
;

截至目前,负载脚本将如果NodeTwoID即使不为空nodeoneid是空境。我怎么能建立在一个简单的如果一样的状态,加载脚本将加载合适的节点只要nodeoneid或nodetwoid是无效的?

更新:刚才看到的情况。去检查这个真正快速..

answer1: 回答1:

I think you could do something like this for each line. If the value is not null put it in a collection of one. 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 }))

我认为你可以为每一行做这样的事情。如果该值不是NULL,将其放入一个集合中。然后,如果集合有一个项,则该节点被创建,如果它们是空的,则不会创建节点。如果它们都有值,则创建两个节点。

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 }))
answer2: 回答2:

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

这个适合你吗?

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 }));
neo4j  cypher