在 Neo4j 密码查询中使用 apoc.map.fromPairs 的 NullPointer
NullPointer using apoc.map.fromPairs in Neo4j Cypher Query
我正在使用下面的代码 return 所有节点,这些节点是来自特定节点 (id(65)) 的 3 个向内边缘,并在以下帮助下将结果格式化为 JSON 图表apoc.map.fromPairs 程序。如果没有距离起始节点 3 条边的节点,我会收到错误消息。
当我包含 OPTIONAL 语句时,apoc.map.fromPairs 过程似乎针对 "null" 抛出以下错误 运行 用于模式的缺失部分。
Failed to invoke function apoc.map.fromPairs
: Caused by:
java.lang.NullPointerException
关于如何克服这个问题的任何建议。我尝试编写一个 CASE 语句来检查地图中是否有任何键,但也无法使其工作。
*******************************************************************************************
// **
// ** Author: MOS
// ** Date: 02/03/2017
// ** Description: Returns all nodes and relationships that are within 3 inward
// ** hops of the requested node. The response is formatted as Graph JSON.
// **
// *******************************************************************************************
OPTIONAL MATCH (l0) <-[r1]- (l1) <-[r2]- (l2) <-[r3]- (l3)
WHERE ID(l0) = 65
WITH [
{
id: id(l0),
label: labels(l0),
type:"",
metadata: apoc.map.fromPairs([key IN keys(l0) | [key, l0[key]]])
},
{
id: id(l1),
label: labels(l1),
type:"",
metadata: apoc.map.fromPairs([key IN keys(l1) | [key, l1[key]]])
},
{
id: id(l2),
label: labels(l2),
type:"",
metadata: apoc.map.fromPairs([key IN keys(l2) | [key, l2[key]]])
},
{
id: id(l3),
label: labels(l3),
type:"",
metadata: apoc.map.fromPairs([key IN keys(l3) | [key, l3[key]]])
}
] as nodes,
[
{
id: id(r1),
source: ID(startNode(r1)),
relation: type(r1),
target: ID(endNode(r1)),
directed: "true",
metadata: apoc.map.fromPairs([key IN keys(r1) | [key, r1[key]]])
},
{
id: id(r2),
source: ID(startNode(r2)),
relation: type(r2),
target: ID(endNode(r2)),
directed: "true",
metadata: apoc.map.fromPairs([key IN keys(r2) | [key, r2[key]]])
},
{
id: id(r3),
source: ID(startNode(r3)),
relation: type(r3),
target: ID(endNode(r3)),
directed: "true",
metadata: apoc.map.fromPairs([key IN keys(r3) | [key, r3[key]]])
}
] as edges
UNWIND nodes as node
UNWIND edges as edge
RETURN
{
graph:
{
type:"",
label: "",
directed: "true",
node: collect(distinct node) ,
edges: collect(distinct edge),
metadata:{
countNodes: count(distinct node),
countEdges: count(distinct edge)
}
}
}
非常感谢
你为什么不只使用 MATCH 并且不会有缺失值,因为如果找不到你什么都不做
MATCH (l0) <-[r1]- (l1) <-[r2]- (l2) <-[r3]- (l3)
WHERE ID(l0) = 65
如果你愿意,你可以像这样进行可选的匹配和过滤
OPTIONAL MATCH (l0) <-[r1]- (l1) <-[r2]- (l2) <-[r3]- (l3)
WHERE ID(l0) = 65
WITH * where r3 is not null
Do sth
您可以大大简化您的查询,也可以概括它:
OPTIONAL MATCH path = (x)<-[*..3]-() WHERE ID(x) = 65
UNWIND nodes(path) as node
UNWIND rels(path) as rel
WITH collect(distinct node) as nodes,collect(distinct rel) as rels
// WITH apoc.coll.flatten(collect(nodes(path))) as nodes, apoc.coll.flatten(collect(relationships(path))) as rels
WITH apoc.coll.toSet([n in nodes WHERE n is not null
| { id: id(n),label: labels(n),type:"",metadata: properties(n) } ]) as nodes,
apoc.coll.toSet([r in rels WHERE r is not null
| { id: id(r),source: id(startNode(r)),relation: type(r),target: id(endNode(r)), directed: "true" } ]) as rels
RETURN { graph: { type:"",label: "",directed: "true",nodes: nodes,edges: rels,
metadata:{ countNodes: size(nodes),countEdges: size(rels) } } } as graph;
我正在使用下面的代码 return 所有节点,这些节点是来自特定节点 (id(65)) 的 3 个向内边缘,并在以下帮助下将结果格式化为 JSON 图表apoc.map.fromPairs 程序。如果没有距离起始节点 3 条边的节点,我会收到错误消息。
当我包含 OPTIONAL 语句时,apoc.map.fromPairs 过程似乎针对 "null" 抛出以下错误 运行 用于模式的缺失部分。
Failed to invoke function
apoc.map.fromPairs
: Caused by: java.lang.NullPointerException
关于如何克服这个问题的任何建议。我尝试编写一个 CASE 语句来检查地图中是否有任何键,但也无法使其工作。
*******************************************************************************************
// **
// ** Author: MOS
// ** Date: 02/03/2017
// ** Description: Returns all nodes and relationships that are within 3 inward
// ** hops of the requested node. The response is formatted as Graph JSON.
// **
// *******************************************************************************************
OPTIONAL MATCH (l0) <-[r1]- (l1) <-[r2]- (l2) <-[r3]- (l3)
WHERE ID(l0) = 65
WITH [
{
id: id(l0),
label: labels(l0),
type:"",
metadata: apoc.map.fromPairs([key IN keys(l0) | [key, l0[key]]])
},
{
id: id(l1),
label: labels(l1),
type:"",
metadata: apoc.map.fromPairs([key IN keys(l1) | [key, l1[key]]])
},
{
id: id(l2),
label: labels(l2),
type:"",
metadata: apoc.map.fromPairs([key IN keys(l2) | [key, l2[key]]])
},
{
id: id(l3),
label: labels(l3),
type:"",
metadata: apoc.map.fromPairs([key IN keys(l3) | [key, l3[key]]])
}
] as nodes,
[
{
id: id(r1),
source: ID(startNode(r1)),
relation: type(r1),
target: ID(endNode(r1)),
directed: "true",
metadata: apoc.map.fromPairs([key IN keys(r1) | [key, r1[key]]])
},
{
id: id(r2),
source: ID(startNode(r2)),
relation: type(r2),
target: ID(endNode(r2)),
directed: "true",
metadata: apoc.map.fromPairs([key IN keys(r2) | [key, r2[key]]])
},
{
id: id(r3),
source: ID(startNode(r3)),
relation: type(r3),
target: ID(endNode(r3)),
directed: "true",
metadata: apoc.map.fromPairs([key IN keys(r3) | [key, r3[key]]])
}
] as edges
UNWIND nodes as node
UNWIND edges as edge
RETURN
{
graph:
{
type:"",
label: "",
directed: "true",
node: collect(distinct node) ,
edges: collect(distinct edge),
metadata:{
countNodes: count(distinct node),
countEdges: count(distinct edge)
}
}
}
非常感谢
你为什么不只使用 MATCH 并且不会有缺失值,因为如果找不到你什么都不做
MATCH (l0) <-[r1]- (l1) <-[r2]- (l2) <-[r3]- (l3)
WHERE ID(l0) = 65
如果你愿意,你可以像这样进行可选的匹配和过滤
OPTIONAL MATCH (l0) <-[r1]- (l1) <-[r2]- (l2) <-[r3]- (l3)
WHERE ID(l0) = 65
WITH * where r3 is not null
Do sth
您可以大大简化您的查询,也可以概括它:
OPTIONAL MATCH path = (x)<-[*..3]-() WHERE ID(x) = 65
UNWIND nodes(path) as node
UNWIND rels(path) as rel
WITH collect(distinct node) as nodes,collect(distinct rel) as rels
// WITH apoc.coll.flatten(collect(nodes(path))) as nodes, apoc.coll.flatten(collect(relationships(path))) as rels
WITH apoc.coll.toSet([n in nodes WHERE n is not null
| { id: id(n),label: labels(n),type:"",metadata: properties(n) } ]) as nodes,
apoc.coll.toSet([r in rels WHERE r is not null
| { id: id(r),source: id(startNode(r)),relation: type(r),target: id(endNode(r)), directed: "true" } ]) as rels
RETURN { graph: { type:"",label: "",directed: "true",nodes: nodes,edges: rels,
metadata:{ countNodes: size(nodes),countEdges: size(rels) } } } as graph;