2017-02-28 1 views
0

Dans la requête suivante (que j'ai modifié d'un j'ai trouvé sur le net). J'utilise APOC pour créer 14 nœuds, et ce que je pense serait 12 bords (reliant chacun des nœuds dans deux rangées). C'est à dire. si vous prenez 14 nœuds, et mettez ensuite dans deux colonnes de 7 éléments chacun, vous n'avez besoin que de 6 lignes pour les relier ensemble. Quoi qu'il en soit, le code utilise APOC pour lire le fichier JSON, et il crée les nœuds exactement comme prévu. Mais je reçois 79 bords. Il doit y avoir quelque chose à propos de CYPHER ou UNWIND que je ne comprends pas.Neo4J requête Cypher crée plus de liens que prévu

Vous pouvez voir clairement que dans le fichier JSON, j'ai seulement 12 objets dans le tableau de tuyaux.

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" 
       } 
      } 
     ] 
    } 
} 

Répondre

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: Expression dans avec doit être aliasé (utiliser AS) (ligne 4, colonne 11 (décalage: 190)) "AVEC ligne, compte (*)" –

+0

Je pense que je comprends cela, mais laissez-moi voir si je comprends vos commentaires en les reformulant comme je les entends: 1. en ajoutant le compte (*) vous êtes en effet en train de faire une distinction et de terminer le déroulement comme dans: http://stackoverflow.com/questions/30744331/end-unwind-statement-in-a-cypher-query –

+0

2 , Vous suggérez d'ajouter une étiquette à la fois aux: Acteur et: Étiquettes de tuyaux pour qu'ils soient tous les deux: Acteur: Nœud et: Pipe: Nœud et ceci ajoutera automatiquement un index pour la propriété clé, ou cela doit-il encore être ajouté? ? –

1

Je pense que parce que vous avez UNWIND l'intérieur de UNWIND il passe par seconde fois dérouleur x où x est la valeur de la taille du premier tableau. Comme si vous aviez une boucle FOR à l'intérieur d'une boucle FOR. Essayez de la diviser en deux requêtes.

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 * 

edit: en une seule étape // ne fonctionne pas

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

Oui, cela fonctionne. Mais existe-t-il un assistant CYPHER qui peut me dire comment faire cela en une étape? –

+0

j'ai fait et éditer essayer maintenant –

+0

Utilisation invalide de CALL à l'intérieur de FOREACH (ligne 4, colonne 1 (décalage: 165)) "CALL apoc.create.node (node.labels, node.properties) RENDEMENT nœud AS n)" ^ –