取消从JSON文件加载的多个不相关的数组 [英] UNWIND multiple unrelated arrays loaded from JSON file

查看:74
本文介绍了取消从JSON文件加载的多个不相关的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过单次调用apoc.load.json()来取消缠绕多个数组属性.我使用的版本无法完全正常工作:某些关系无法加载.我的猜测是,这是由于通过WITH命令进行的输出管道.如果我对每个基于数组的属性分别运行展开,则可以全部加载,但是我很好奇如何一起完成.

I'm trying to UNWIND multiple array properties with a single call to apoc.load.json(). The version I have doesn't fully work: some relationships don't get loaded. My guess is that it's due to the piping of output via the WITH command. I can have it all load if I run the unwinds separately for each array-based property, but I'm curious as to how it can be done all together.

任何见解和建议都值得赞赏=)

Any insights and pointers are appreciated =)

//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文件中的示例条目:

Example entry in the JSON file:

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

推荐答案

此处的实际问题只是SET子句的最后一行和WITH子句之间不必要的,.摆脱它,就摆脱语法错误.

The actual problem here is just an unnecessary , between the last line of your SET clause and the WITH clause. Get rid of that, and you get rid of the syntax error.

也就是说,我强烈建议将每个UNWIND与对未展开值起作用的子句分组,然后

That said, I highly recommend grouping each UNWIND with the clauses which act on the unwinded values, then resetting the cardinality back to a single row before performing the next UNWIND and processing. Something like this:

//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)

这篇关于取消从JSON文件加载的多个不相关的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆