2017-02-28 104 views
0

在下面的查询(我从网上找到的一个我修改)。我正在使用APOC创建14个节点,并且我将会是12个边(连接两行中的每个节点)。即如果你有14个节点,然后分成两列,每列7个项目,你只需要6条线路将它们连接在一起。Neo4J Cypher查询创建比预期更多的链接

无论如何,代码使用APOC来读取JSON文件,并且它正在按照预期创建节点。但我得到了79个边缘。必须有一些关于CYPHER或UNWIND我不明白。

您可以清楚地看到,在JSON文件中,管道阵列中只有12个对象。

CALL apoc.load.json("file:///G:/wwwroot/DataFlow/graph.json") YIELD value AS row 
WITH row, row.graph.nodes AS nodes 
UNWIND nodes AS node 
CALL apoc.create.node(node.labels, node.properties) YIELD node AS n 
WITH row 
UNWIND row.graph.pipes AS rel 
MATCH (a) WHERE a.key = rel.start 
MATCH (b) WHERE b.key = rel.end 
CALL apoc.create.relationship(a, rel.type, rel.properties, b) YIELD rel AS r 
RETURN * 

{ 
    "graph" : { 
     "nodes" : [{ 
       "id" : "HW.SCIM", 
       "labels" : [ 
        "Actor" 
       ], 
       "properties" : { 
        "key" : "HW.SCIM", 
        "partition" : "Hardware", 
        "CSC" : "HW", 
        "name" : "SCIM", 
        "rate" : 10, 
        "caption" : "SCIM" 
       } 
      }, { 
       "id" : "HW.GPS", 
       "labels" : [ 
        "Actor" 
       ], 
       "properties" : { 
        "key" : "HW.GPS", 
        "partition" : "Hardware", 
        "CSC" : "HW", 
        "name" : "GPS", 
        "rate" : 50, 
        "caption" : "GPS" 
       } 
      }, { 
       "id" : "HW.SCIM-Channel", 
       "labels" : [ 
        "Channel" 
       ], 
       "properties" : { 
        "key" : "HW.SCIM-Channel", 
        "wordLength" : 300, 
        "channelType" : "Partition", 
        "timeStamp" : "", 
        "writer" : "" 
       } 
      }, { 
       "id" : "HW.GPS-Channel", 
       "labels" : [ 
        "Channel" 
       ], 
       "properties" : { 
        "key" : "HW.GPS-Channel", 
        "wordLength" : 200, 
        "channelType" : "Partition", 
        "timeStamp" : "", 
        "writer" : "" 
       } 
      }, { 
       "id" : "Platform.SCIM", 
       "labels" : [ 
        "Actor" 
       ], 
       "properties" : { 
        "key" : "Platform.SCIM", 
        "partition" : "Platform", 
        "CSC" : "Platform", 
        "name" : "SCIM", 
        "rate" : 10, 
        "caption" : "Platform SCIM" 
       } 
      }, { 
       "id" : "Platform.GPS", 
       "labels" : [ 
        "Actor" 
       ], 
       "properties" : { 
        "key" : "Platform.GPS", 
        "partition" : "Platform", 
        "CSC" : "Platform", 
        "name" : "GPS", 
        "rate" : 50, 
        "caption" : "Platform GPS" 
       } 
      }, { 
       "id" : "Platform.SCIM-Channel", 
       "labels" : [ 
        "Channel" 
       ], 
       "properties" : { 
        "key" : "Platform.SCIM-Channel", 
        "wordLength" : 300, 
        "channelType" : "Partition", 
        "timeStamp" : "", 
        "writer" : "" 
       } 
      }, { 
       "id" : "Platform.GPS-Channel", 
       "labels" : [ 
        "Channel" 
       ], 
       "properties" : { 
        "key" : "Platform.GPS-Channel", 
        "wordLength" : 200, 
        "channelType" : "Partition", 
        "timeStamp" : "", 
        "writer" : "" 
       } 
      }, { 
       "id" : "ALFSW.SCIM", 
       "labels" : [ 
        "Actor" 
       ], 
       "properties" : { 
        "key" : "ALFSW.SCIM", 
        "partition" : "Application", 
        "CSC" : "Application", 
        "name" : "SCIM", 
        "rate" : 10, 
        "caption" : "App SCIM" 
       } 
      }, { 
       "id" : "ALFSW.GPS", 
       "labels" : [ 
        "Actor" 
       ], 
       "properties" : { 
        "key" : "ALFSW.GPS", 
        "partition" : "Application", 
        "CSC" : "Application", 
        "name" : "GPS", 
        "rate" : 50, 
        "caption" : "App GPS" 
       } 
      }, { 
       "id" : "ALFSW.SCIM-Channel", 
       "labels" : [ 
        "Channel" 
       ], 
       "properties" : { 
        "key" : "ALFSW.SCIM-Channel", 
        "wordLength" : 300, 
        "channelType" : "Partition", 
        "timeStamp" : "", 
        "writer" : "" 
       } 
      }, { 
       "id" : "ALFSW.GPS-Channel", 
       "labels" : [ 
        "Channel" 
       ], 
       "properties" : { 
        "key" : "ALFSW.GPS-Channel", 
        "wordLength" : 200, 
        "channelType" : "Partition", 
        "timeStamp" : "", 
        "writer" : "" 
       } 
      }, { 
       "id" : "GNC.SCIM", 
       "labels" : [ 
        "Actor" 
       ], 
       "properties" : { 
        "key" : "GNC.SCIM", 
        "partition" : "GNC", 
        "CSC" : "MediumRate", 
        "name" : "SCIM", 
        "rate" : 10, 
        "caption" : "GNC Medium Rate" 
       } 
      }, { 
       "id" : "GNC.GPS", 
       "labels" : [ 
        "Actor" 
       ], 
       "properties" : { 
        "key" : "GNC.GPS", 
        "partition" : "GNC", 
        "CSC" : "HighRate", 
        "name" : "GPS", 
        "rate" : 50, 
        "caption" : "GNC High Rate" 
       } 
      } 
     ], 
     "pipes" : [{ 
       "type" : "Pipe", 
       "start" : "HW.SCIM", 
       "end" : "HW.SCIM-Channel", 
       "properties" : { 
        "flow" : "OUT" 
       } 
      }, { 
       "type" : "Pipe", 
       "start" : "HW.GPS", 
       "end" : "HW.GPS-Channel", 
       "properties" : { 
        "flow" : "OUT" 
       } 
      }, { 
       "type" : "Pipe", 
       "start" : "Platform.SCIM", 
       "end" : "HW.SCIM-Channel", 
       "properties" : { 
        "flow" : "IN" 
       } 
      }, { 
       "type" : "Pipe", 
       "start" : "Platform.GPS", 
       "end" : "HW.GPS-Channel", 
       "properties" : { 
        "flow" : "IN" 
       } 
      }, { 
       "type" : "Pipe", 
       "start" : "Platform.SCIM", 
       "end" : "Platform.SCIM-Channel", 
       "properties" : { 
        "flow" : "OUT" 
       } 
      }, { 
       "type" : "Pipe", 
       "start" : "Platform.GPS", 
       "end" : "Platform.GPS-Channel", 
       "properties" : { 
        "flow" : "OUT" 
       } 
      }, { 
       "type" : "Pipe", 
       "start" : "ALFSW.SCIM", 
       "end" : "Platform.SCIM-Channel", 
       "properties" : { 
        "flow" : "IN" 
       } 
      }, { 
       "type" : "Pipe", 
       "start" : "ALFSW.GPS", 
       "end" : "Platform.GPS-Channel", 
       "properties" : { 
        "flow" : "IN" 
       } 
      }, { 
       "type" : "Pipe", 
       "start" : "ALFSW.SCIM", 
       "end" : "ALFSW.SCIM-Channel", 
       "properties" : { 
        "flow" : "OUT" 
       } 
      }, { 
       "type" : "Pipe", 
       "start" : "ALFSW.GPS", 
       "end" : "ALFSW.GPS-Channel", 
       "properties" : { 
        "flow" : "OUT" 
       } 
      }, { 
       "type" : "Pipe", 
       "start" : "GNC.SCIM", 
       "end" : "ALFSW.SCIM-Channel", 
       "properties" : { 
        "flow" : "IN" 
       } 
      }, { 
       "type" : "Pipe", 
       "start" : "GNC.GPS", 
       "end" : "ALFSW.GPS-Channel", 
       "properties" : { 
        "flow" : "IN" 
       } 
      } 
     ] 
    } 
} 

回答

2
CALL apoc.load.json("file:///G:/wwwroot/DataFlow/graph.json") YIELD value AS row 
UNWIND row.graph.nodes AS node 
CALL apoc.create.node(node.labels, node.properties) YIELD node AS n 
// aggregation or distinct reduces cardinality to 1 per row again 
WITH row, count(*) 
UNWIND row.graph.pipes AS rel 
// you should use labels here, otherwise you get no lookup benefits 
// from indexes, even if you just use a generic :Node label 
MATCH (a) WHERE a.key = rel.start 
MATCH (b) WHERE b.key = rel.end 
CALL apoc.create.relationship(a, rel.type, rel.properties, b) YIELD rel AS r 
RETURN * 
+0

仅供参考:WITH中的表达式必须使用别名(使用AS)(第4行,第11列(偏移量:190)) “WITH row,count(*)” –

+0

我想我明白这一点,但让我看看我是否理解你的意见重申他们的方式,我听到他们: 1.通过添加计数(*)你实际上是做一个独特的和结束放松,如下所示:http://stackoverflow.com/questions/30744331/end-unwind-statement-in-a-cypher-query –

+0

2 ,你建议在Actor和Pipe标签中添加一个Label,使它们都是:Actor:Node和:Pipe:Node,这会自动为key属性添加一个索引,还是需要添加? –

1

我认为那是因为你有UNWIND UNWIND内如此这般通过第二开卷x次,其中x是第一个数组的大小值。就像在FOR循环中有一个FOR循环一样。试着将它分成两个查询。

CALL apoc.load.json("file:///G:/wwwroot/DataFlow/graph.json") YIELD value AS row 
WITH row, row.graph.nodes AS nodes 
UNWIND nodes AS node 
CALL apoc.create.node(node.labels, node.properties) YIELD node AS n 
RETURN * 



CALL apoc.load.json("file:///G:/wwwroot/DataFlow/graph.json") YIELD value AS row 
UNWIND row.graph.pipes AS rel 
MATCH (a) WHERE a.key = rel.start 
MATCH (b) WHERE b.key = rel.end 
CALL apoc.create.relationship(a, rel.type, rel.properties, b) YIELD rel AS r 
RETURN * 

编辑:一步到位//没有工作

CALL apoc.load.json("file:///G:/wwwroot/DataFlow/graph.json") YIELD value AS row 
WITH row, row.graph.nodes AS nodes,row.graph.pipes AS rel 
FOREACH (node in nodes | 
CALL apoc.create.node(node.labels, node.properties) YIELD node AS n) 
WITH row,rel 
UNWIND row.graph.pipes AS rel 
MATCH (a) WHERE a.key = rel.start 
MATCH (b) WHERE b.key = rel.end 
CALL apoc.create.relationship(a, rel.type, rel.properties, b) YIELD rel AS r 
RETURN * 
+0

没错,就是工作。但是有没有一个CYPHER向导可以告诉我如何一步到位呢? –

+0

我做了,现在编辑尝试 –

+0

FORALL内部CALL的使用无效(第4行,第1列(偏移:165)) “CALL apoc.create.node(node.labels,node.properties)YIELD node AS n)” ^ –