【问题标题】:How to create a parameter on a node with the name of the parameter being a value from the file being imported with Cypher/ APOC?如何在节点上创建参数,参数名称是使用 Cypher/APOC 导入的文件中的值?
【发布时间】:2023-02-02 19:15:24
【问题描述】:

我是 neo4j 的新手,正在尝试导入一些 json 格式的数据。 我得到了读取所有 json 文件并将一些数据转换为节点和边缘的第一步。如何即时创建节点参数,我想不通。 SET j[issn.type] = issn.value 应该使用在 json 数据中找到的值的名称在 j 上创建一个新参数,并将值赋予它 issn.value。后者应该没问题,但是j[issn.type]好像不行。

我如何实现这一目标?

谢谢

完整查询

call apoc.load.directory("*.json") yield value as files unwind files as file 
CALL apoc.load.json(file) YIELD value as object 
UNWIND object.items AS entry 
MERGE (p:Publisher {name: entry.publisher}) 
MERGE (j:Journal {name: entry.`container-title`}) 
ON CREATE SET j.created = timestamp()
FOREACH (issn IN entry.`issn-type` | 
  SET j[issn.type] = issn.value
)
MERGE (p)-[r:publishes]->(j) 
ON CREATE SET r.created = timestamp()
RETURN p

【问题讨论】:

    标签: neo4j cypher neo4j-apoc


    【解决方案1】:

    尚不支持用于在 Cypher 中设置属性的语法 SET j[issn.type] = issn.value。要实现此目的,请使用 apoc.create.setProperty 函数,如下所示:

    call apoc.load.directory("*.json") yield value as files 
    unwind files as file 
    CALL apoc.load.json(file) YIELD value as object 
    UNWIND object.items AS entry 
    MERGE (p:Publisher {name: entry.publisher}) 
    MERGE (j:Journal {name: entry.`container-title`}) 
    ON CREATE SET j.created = timestamp()
    WITH p, j, entry
    UNWIND entry.`issn-type` AS issn 
    CALL apoc.create.setProperty(j, issn.type, issn.value)
    YIELD node
    MERGE (p)-[r:publishes]->(node) 
    ON CREATE SET r.created = timestamp()
    RETURN p
    

    这是文档link。我在查询中假设 entry.issn_type 是某种列表,这就是我展开它的原因,因为我们不能从 FOREACH 循环中调用 apoc 函数。

    【讨论】:

    • 使用 apoc 似乎不可行...这样: Invalid use of CALL inside FOREACH (line 9, column 2 (offset: 331)) " CALL apoc.create.setProperty(j, issn.type, issn 。价值)”
    • 更新了@jallmer 的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多