2017-02-28 4 views
0

次のクエリでは、ネット上で見つかったものから変更しました。私は14ノードを作成するためにAPOCを使用していますが、私は12エッジ(2ノードの各ノードを2行に接続)になります。私。 14ノードを使用して、7つのアイテムの2つの列に入れた場合、それらを一緒に接続するために必要なのは6行だけです。Neo4J Cypherクエリで予想以上に多くのリンクが作成される

とにかく、このコードでは、APOCを使用してJSONファイルを読み込んでいますが、期待通りにノードを作成しています。しかし、私は79エッジを取得しています。私が理解していないサイファーまたはアンワインドのいずれかについての何かがなければならない。

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

FYI:WITHの式のエイリアスを使用する必要があります(行4、列11(オフセット:190)) "WITH row、count(*)" –

+0

私はこれを理解していると思いますが、あなたのコメントは私がそれらを聞くようにそれらを再作成することによって:。 1.実際にカウントしている数(*)を追加することで、別のことを行い、巻き戻しを終了します:http://stackoverflow.com/questions/30744331/end-unwind-statement-in-a-cypher-query –

+0

2アクターと:パイプラベルの両方にラベルを追加することをお勧めします:アクター:ノードと:パイプ:ノードとこれは自動的にキープロパティのインデックスを追加するか、それでも追加する必要があります? –

1

私はそれは、xが最初の配列の大きさの値である第二のアンワインドのx回を通過するように、あなたはUNWINDの内側にUNWINDを持っているので、それがあると思います。 FORループ内にFORループを持つようにします。それを2つのクエリに分割します。

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 * 

編集:1つのステップで//動作しない

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のウィザードは、これをどのようにして1つのステップで行うのか教えてくれますか? –

+0

私はして今tryしようとしました –

+0

FOREACH(行4、列1(オフセット:165))内のCALLの無効な使用 "CALL apoc.create.node(node.labels、node.properties)YIELDノードAS n)" ^ –

関連する問題