gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 03:21:50 25 4
gpt4 key购买 nike

我正在尝试通过一次对 apoc.load.json() 的调用来解除多个数组属性。我的版本不能完全正常工作:某些关系没有加载。我的猜测是这是由于通过 WITH 命令进行的输出管道。如果我为每个基于数组的属性单独运行展开,我可以将其全部加载,但我很好奇如何一起完成。

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

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

最佳答案

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

也就是说,我强烈建议将每个 UNWIND 与作用于展开值的子句分组,然后 resetting the cardinality在执行下一个 UNWIND 和处理之前返回到单行。像这样的东西:

//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 - UNWIND 从 JSON 文件加载的多个不相关的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44618338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com