使用curl发布密码时为什么没有创建关系?

时间:2017-02-10 03:04:17

标签: curl neo4j cypher

我使用curl执行下面的代码(当我真正运行它时,一切都在一行。我在这里分解它以便它更容易阅读)。节点已成功创建,但关系不是。它无法识别节点之间的关系。

curl --user neo4j:mypassword -i -H accept:application/json -H 
content-type:application/json -XPOST http://localhost:7474/db/data/transaction/commit -d 
'{"statements":[
{"statement":"CREATE (keanu:Actor { name : '\''Keanu Reeves'\''}) return id(keanu)"},
{"statement":"CREATE (chad:Director { name : '\''Chad Stahelski'\''}) return id(chad)"},
{"statement":"CREATE (david:Director { name : '\''David Leitch'\''}) return id(david)"},
{"statement":"CREATE (william:Actor { name : '\''William Dafoe'\''}) return id(william)"},
{"statement":"CREATE (michael:Actor { name : '\''Michael Nyquist'\''}) return id(michael)"},
{"statement":"CREATE (movienew:Movie { title : '\''John Wick'\''}) return id(movienew)"},
{"statement":"CREATE (keanu)-[:ACTS_IN ]->(movienew)"},
{"statement":"CREATE (chad)-[:Directed_IN ]->(movienew)"},
{"statement":"CREATE (david)-[:Directed_IN ]->(movienew)"},
{"statement":"CREATE (william)-[:ACTS_IN ]->(movienew)"},
{"statement":"CREATE (michael)-[:ACTS_IN ]->(movienew)"}
]}'

enter image description here

它无法识别哪些节点之间的关系。

我该如何解决?

更新:我现在已将代码更改为此

curl --user neo4j:mypassword -i -H accept:application/json -H content-type:application/json -XPOST http://localhost:7474/db/data/transaction/commit -d '{"statements":[{"statement":"CREATE (matrix1:Movie { title : '\''The Matrix'\'', year : '\''1999-03-31'\'' }) return id(matrix1)","CREATE (matrix2:Movie { title : '\''The Matrix Reloaded'\'', year : '\''2003-05-07'\'' }) return id(matrix2)","CREATE (matrix3:Movie { title : '\''The Matrix Revolutions'\'', year : '\''2003-10-27'\'' }) return id(matrix3)","CREATE (keanu:Actor { name : '\''Keanu Reeves'\''}) return id(keanu)","CREATE (laurence:Actor { name : '\''Laurence Fishburne'\''})","CREATE (carrieanne:Actor { name : '\''Carrie-Anne Moss'\''})","CREATE (keanu)-[:ACTS_IN { role : '\''Neo'\'' }]->(matrix1)","CREATE (keanu)-[:ACTS_IN { role : '\''Neo'\'' }]->(matrix2)","CREATE (keanu)-[:ACTS_IN { role : '\''Neo'\'' }]->(matrix3)","CREATE (laurence)-[:ACTS_IN { role : '\''Morpheus'\'' }]->(matrix1)","CREATE (laurence)-[:ACTS_IN { role : '\''Morpheus'\'' }]->(matrix2)","CREATE (laurence)-[:ACTS_IN { role : '\''Morpheus'\'' }]->(matrix3)","CREATE (carrieanne)-[:ACTS_IN { role : '\''Trinity'\'' }]->(matrix1)","CREATE (carrieanne)-[:ACTS_IN { role : '\''Trinity'\'' }]->(matrix2)","CREATE (carrieanne)-[:ACTS_IN { role : '\''Trinity'\'' }]->(matrix3)"}]}'

然后我收到错误'无法反序列化请求:意外字符(','(代码44))'

更新:根据接受的答案,我复制了确切的格式并将其应用到其他程序,但这次我收到了以下错误:

  

{“results”:[],“errors”:[{“code”:“Neo.ClientError.Request.InvalidFormat”,“message”:“无法反序列化请求:意外字符('。'(代码46) )):期待双引号开始字段名称\ n在[来源:HttpInputOverHTTP @ 442ac29b;行:1,列:3]“}]}

我的代码是:

curl --user neo4j:mypassword -i -H accept:application/json \
-H content-type:application/json \
-XPOST http://localhost:7474/db/data/transaction/commit \
-d @- << EOF
{
    "statements":
    [
      {
       "statement":
        "CREATE (matrix1:Movie { title : 'The Matrix', year : '1999-03-31'})
         CREATE (matrix2:Movie { title : 'The Matrix Reloaded', year : '2003-05-07'})
         CREATE (matrix3:Movie { title : 'The Matrix Revolutions', year : '2003-10-27'})
         CREATE (keanu:Actor { name : 'Keanu Reeves'})
         CREATE (laurence:Actor { name : 'Laurence Fishburne'})
         CREATE (carrieanne:Actor { name : 'Carrie-Anne Moss'})
         CREATE (keanu)-[:ACTS_IN { role : 'Neo' }]->(matrix1)
     CREATE (keanu)-[:ACTS_IN { role : 'Neo' }]->(matrix2)
     CREATE (keanu)-[:ACTS_IN { role : 'Neo' }]->(matrix3)
     CREATE (laurence)-[:ACTS_IN { role : 'Morpheus' }]->(matrix1)
     CREATE (laurence)-[:ACTS_IN { role : 'Morpheus' }]->(matrix2)
     CREATE (laurence)-[:ACTS_IN { role : 'Morpheus' }]->(matrix3)
     CREATE (carrieanne)-[:ACTS_IN { role : 'Trinity' }]->(matrix1)
     CREATE (carrieanne)-[:ACTS_IN { role : 'Trinity' }]->(matrix2)
     CREATE (carrieanne)-[:ACTS_IN { role : 'Trinity' }]->(matrix3) 
     RETURN *"
  }
]
}
EOF

有人可以告诉我为什么这不正确吗?

2 个答案:

答案 0 :(得分:3)

我认为最好的方法是删除一堆不必要的滴答声,这样你就可以看到你做得更好了。正如@inversefalcon所说,当你执行每个语句时,你会失去上下文。您希望将其全部保存在一个中,以便在最后创建关系时保留每个CREATE的标识符。

您应该可以按原样剪切和粘贴此块。

curl --user neo4j:password -i -H accept:application/json \
  -H content-type:application/json \
  -XPOST http://localhost:7474/db/data/transaction/commit \
  -d @- << END_OF_CYPHER
{
  "statements":
  [
    {
      "statement":
        "CREATE (keanu:Actor { name : 'Keanu Reeves'})
         CREATE (chad:Director { name : 'Chad Stahelski'})
         CREATE (david:Director { name : 'David Leitch'})
         CREATE (william:Actor { name : 'William Dafoe'})
         CREATE (michael:Actor { name : 'Michael Nyquist'})
         CREATE (movienew:Movie { title : 'John Wick'})
         CREATE (keanu)-[:ACTS_IN ]->(movienew)
         CREATE (chad)-[:Directed_IN ]->(movienew)
         CREATE (david)-[:Directed_IN ]->(movienew)
         CREATE (william)-[:ACTS_IN ]->(movienew)
         CREATE (michael)-[:ACTS_IN ]->(movienew)
         RETURN *"
     }
  ]
}
END_OF_CYPHER

答案 1 :(得分:2)

单独的语句具有单独的变量范围。一旦你进入下一个陈述,任何以前的变量都不可用。

这就是为什么在每个关系创建语句中,您使用的变量都是全新的,因此每个关系创建语句会创建由给定关系连接的新三个空白节点。

为了避免这种情况,你需要在一个语句中运行创建查询的所有行(并且除去那些返回,至少到最后一行)。