В следующем запросе (который я изменил из одного я нашел в сети). Я использую APOC для создания 14 узлов, и я думаю, что это будет 12 ребер (соединяя каждый из узлов в две строки). То есть если вы возьмете 14 узлов и поместите их в два столбца по 7 элементов в каждом, вам потребуется всего 6 линий, чтобы соединить их вместе.

В любом случае, код использует APOC для чтения файла JSON и создает узлы точно так, как ожидается. Но я получаю 79 граней. Должно быть, что-то в CYPHER или UNWIND я не понимаю.

Вы можете ясно видеть, что в файле JSON у меня всего 12 объектов в массиве pipe.

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"
                }
            }
        ]
    }
}
0
Dr.YSG 28 Фев 2017 в 21:03

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 *
3
Michael Hunger 28 Фев 2017 в 23:11

Я думаю, это потому, что у вас есть 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 *
1
Tomaž Bratanič 28 Фев 2017 в 22:36