UNWIND从JSON文件加载的多个不相关的数组

时间:2017-06-18 18:45:24

标签: json neo4j cypher neo4j-apoc

我试图通过一次调用apoc.load.json()来UNWIND多个数组属性。我的版本没有完全发挥作用:某些关系不会被加载。我的猜测是,这是由于通过WITH命令输出的管道。如果我为每个基于数组的属性分别运行unwinds,我可以全部加载,但我很好奇它是如何一起完成的。

任何见解和指示都表示赞赏=)

//LOAD CLASSES AND UNWIND COMMON ITEMS,COMPANIONS,LOCATIONS 
CALL apoc.load.json("file:///c://pathToFile//classes.json") YIELD value AS class
MERGE (c:Class {name: class.name})
SET 
c.strength = class.strength,
c.intelligence = class.intelligence,
c.dexterity = class.dexterity,

WITH c, class.items AS items, class.companions AS companions, class.locations AS locations
UNWIND items AS item
UNWIND companions AS companion
UNWIND locations AS location

MERGE (i:Item {name: item})
MERGE (i)-[:LIKELY_HAS]->(c)
MERGE (c)-[:LIKELY_BELONGS_TO]->(i)

MERGE (comp:Class {name: companion})
MERGE (comp)-[:LIKELY_COMPANION_OF]->(c)
MERGE (c)-[:LIKELY_ACCOMPANIED_BY]->(comp)

MERGE (l:Location {name: location})
MERGE (l)-[:LIKELY_LOCATION_OF]->(c)
MERGE (c)-[:LIKELY_LOCATED_IN]->(l)

JSON文件中的示例条目:

 {
    "name": "KNIGHT",
    "strength": [75,100],
    "intelligence": [40,80],
    "dexterity": [40,85],
    "items": [
        "SWORD",
        "SHIELD"
    ],
    "companions":[
        "KNIGHT",
        "SERVANT",
        "STEED"
    ],
    "locations": [
        "CASTLE",
        "VILLAGE",
        "CITY"
    ]
}

1 个答案:

答案 0 :(得分:2)

这里的实际问题只是SET子句的最后一行和WITH子句之间不必要的,。摆脱它,你摆脱了语法错误。

尽管如此,我强烈建议将每个UNWIND分组为对未展开的值起作用的子句,然后在执行下一个UNWIND和处理之前将resetting the cardinality分回一行。像这样:

//LOAD CLASSES AND UNWIND COMMON ITEMS,COMPANIONS,LOCATIONS 
CALL apoc.load.json("file:///c://pathToFile//classes.json") YIELD value AS class
MERGE (c:Class {name: class.name})
SET 
c.strength = class.strength,
c.intelligence = class.intelligence,
c.dexterity = class.dexterity

WITH c, class

UNWIND class.items AS item
MERGE (i:Item {name: item})
MERGE (i)-[:LIKELY_HAS]->(c)
MERGE (c)-[:LIKELY_BELONGS_TO]->(i)

WITH distinct c, class
UNWIND class.companions AS companion
MERGE (comp:Class {name: companion})
MERGE (comp)-[:LIKELY_COMPANION_OF]->(c)
MERGE (c)-[:LIKELY_ACCOMPANIED_BY]->(comp)

WITH distinct c, class
UNWIND class.locations AS location
MERGE (l:Location {name: location})
MERGE (l)-[:LIKELY_LOCATION_OF]->(c)
MERGE (c)-[:LIKELY_LOCATED_IN]->(l)