在Neo4j Cypher查询中使用apoc.map.fromPairs的NullPointer [英] NullPointer using apoc.map.fromPairs in Neo4j Cypher Query

查看:146
本文介绍了在Neo4j Cypher查询中使用apoc.map.fromPairs的NullPointer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码从特定节点(id(65))返回所有3个向内边缘的节点,并借助apoc.map.fromPairs过程将结果格式化为JSON Graph。如果没有与起始节点相距3个边缘的节点,则会出现错误。

I am using the code below to return all nodes that are 3 inward facing edges from a specific node (id(65)) and format the result as JSON Graph with the help of the apoc.map.fromPairs procedure. I am getting an error if there are no nodes that are 3 edges away from the starting node.

似乎apoc.map.fromPairs过程引发了belowerror运行

It seems that the apoc.map.fromPairs procedure throws the belowerror running against the "null" used for missing parts of the pattern when I include the OPTIONAL statement.


未能调用函数时,表示缺少模式部分的空。 apoc.map.fromPairs :原因:
java.lang.NullPointerException

Failed to invoke function apoc.map.fromPairs: Caused by: java.lang.NullPointerException

任何建议我该如何克服这一点。我尝试编写CASE语句来检查地图中是否有任何键,但也无法使它们正常工作。

Any suggestions how I can overcome this. I tried writing a CASE statement to check if there were any keys in the map but could not get that to work either.

*******************************************************************************************
// ** 
// ** 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)
               }
  }
}

非常感谢

推荐答案

您可以简化查询并对其进行概括:

You can simplify your query quite a bit, and also generalize it:

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;

这篇关于在Neo4j Cypher查询中使用apoc.map.fromPairs的NullPointer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆