【问题标题】:Neo4J Cypher query creates more links than expectedNeo4J Cypher 查询创建的链接比预期的多
【发布时间】:2017-02-28 18:03:08
【问题描述】:

在下面的查询中(我从网上找到的一个修改)。我正在使用 APOC 创建 14 个节点,而我认为是 12 条边(将每个节点连接成两行)。 IE。如果你取 14 个节点,然后将它们放在两列中,每列 7 个项目,你只需要 6 行将它们连接在一起。

无论如何,代码正在使用 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"
                }
            }
        ]
    }
}

【问题讨论】:

    标签: neo4j cypher neo4j-apoc


    【解决方案1】:
    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 *
    

    【讨论】:

    • 仅供参考: WITH 中的表达式必须使用别名(使用 AS)(第 4 行,第 11 列(偏移量:190))“WITH 行,计数(*)”
    • 我想我理解这一点,但是让我看看我是否理解你的 cmets,方法是重述我听到的方式:。 1.通过添加计数(*),您实际上是在做一个独特的并结束展开,如:stackoverflow.com/questions/30744331/…
    • 2,您建议为 :Actor 和 :Pipe 标签添加标签,以便它们都是 :Actor:Node 和 :Pipe:Node ,这将自动为 key 属性添加索引,还是还需要添加?
    【解决方案2】:

    我认为这是因为您在 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 *
    

    【讨论】:

    • 是的,这行得通。但是有没有 CYPHER 向导可以一步一步告诉我如何做到这一点?
    • 在 FOREACH 中使用 CALL 无效(第 4 行,第 1 列(偏移量:165))“CALL apoc.create.node(node.labels, node.properties) YIELD node AS n)” ^
    猜你喜欢
    • 2017-05-12
    • 1970-01-01
    • 2018-11-17
    • 2020-03-04
    • 2022-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多