如何在 Neo4j 中使用 apoc.load.json() 时从 Json 文件中读取关系



我想从我的 Json 文件中读取关系,而不是硬编码它们。

例如:而不是

MERGE (arg1)-[:relation]->(arg2)

我想要这样的东西:

MERGE (arg1)-[:v.relation]->(arg2).

我的 Json 文件如下:

{
  "bacf06771e0f4fc5a8e68c30fc77c9c4": {
    "arg1": "the Treasury",
    "arg2": "details of the November refunding",
    "relation": "will announce",
    "id": "bacf06771e0f4fc5a8e68c30fc77c9c4",
    "linkedContexts": [
      {
        "targetID": "948eeebd73564adab7dee5c6f177b3b9",
        "classification": "CONTRAST"
      }
    ]  

  },
  "948eeebd73564adab7dee5c6f177b3b9": {
    "arg1": "the funding",
    "arg2": "",
    "relation": "will be delayed",
    "id": "948eeebd73564adab7dee5c6f177b3b9",
    "linkedContexts": [
      {
        "targetID": "006a71e51295440fab7a8e8c697d2ba6",
        "classification": "CONDITION"
      }
    ]    
  }
}

我试过了:

CALL apoc.load.json("files:///example.json") YIELD value
UNWIND [k IN KEYS(value) | value[k]] AS v
MERGE (arg1:Arg1 {subject:v.arg1}) 
MERGE (arg2:Arg2 {object:v.arg2}) 
MERGE (arg1)-[:v.relation]->(arg2)

我收到此错误:

Neo.ClientError.Statement.SyntaxError: Invalid input '.': expected an identifier character, whitespace, '|', a length specification, a property map or ']' (line 13, column 17 (offset: 444))
"merge (arg1)-[:v.relation]->(arg2) "
                 ^

目前,无法与Cypher动态创建关系。

您可以使用 apoc 的 apoc.merge.relationship 过程动态创建节点/关系。

CALL apoc.load.json("files:///example.json") YIELD value
UNWIND [k IN KEYS(value) | value[k]] AS v
MERGE (arg1:Arg1 {subject:v.arg1}) 
MERGE (arg2:Arg2 {object:v.arg2}) 
CALL apoc.merge.relationship(arg1,v.relation,{},{},arg2) YIELD rel
RETURN count(*);

最新更新