2017-06-18 34 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