2017-06-18 28 views
3

我试图通过对apoc.load.json()的单个调用来UNWIND多个数组属性。我拥有的版本并未完全正常工作:某些关系不会被加载。我的猜测是,这是由于通过WITH命令输出管道。如果我对每个基于数组的属性单独运行展开,那么我可以全部加载,但我很好奇它是如何一起完成的。UNWIND从JSON文件加载多个不相关的数组

任何见解和指针赞赏=)

//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" 
    ] 
} 
+3

你能粘贴json文件的一些对象吗? –

+0

好点;完成=) – VeraKozya

回答

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) 
+0

谢谢!这工作:D 我确实有UNWIND之前有自己的属性,但不能没有额外的工作: “WITH distinct c,class” – VeraKozya