使用 Null 正确查询 .json 格式的 Cypher 查询

问题描述

我对 neo4j 还很陌生,我想编写一个查询来读取 .json 格式并创建节点图。下面是我的查询,它在没有 null 属性文件中成功,当我的 json 文件中有 null 属性时不成功。

代码

CALL apoc.load.json("file:/graph-phase1-labelled.json") YIELD value  

UNWIND value.nodes as nodes

UNWIND nodes.properties as prop

MERGE(n1:Node{src:prop.sourceIP})
 
MERGE(n2:Node{dst:prop.destIP})  

WITH n1,n2,prop

MERGE (n1)-[:CONNECTED_TO]->(n2)

RETURN n1,prop

错误

Cannot merge the following node because of null property value for 'src': (:Node {src: null})

有人知道什么可以解决问题吗?

解决方法

由于您无法合并 null 属性,因此您必须过滤掉其中包含 null 的行。示例:

CALL apoc.load.json("file:/graph-phase1-labelled.json") YIELD value

UNWIND value.nodes as nodes

UNWIND nodes.properties as prop

WHERE prop.sourceIP IS NOT NULL prop.destIP IS NOT NULL
WITH prop

MERGE(n1:Node{src:prop.sourceIP})

MERGE(n2:Node{dst:prop.destIP})

WITH n1,n2,prop

MERGE (n1)-[:CONNECTED_TO]->(n2)

RETURN n1,prop
,

我设法通过以下查询解决了这个问题:

call apoc.load.json("file:/graph-phase1-labelled1.json") yield value

unwind value.nodes as nodes

unwind nodes.properties as prop 

with prop where prop.sourceIP is not null

with prop where prop.destIP is not null

merge(n1:Node{src:prop.sourceIP}) 

merge(n2:Node{dest:prop.destIP}) 

with prop,n1,n2

merge (n1)-[ :connected_to]->(n2)

return n1,prop