2017-02-28 84 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)」 ^ –