【问题标题】:How do I qualify and name a relationship in Neo4j APOC?如何在 Neo4j APOC 中限定和命名关系?
【发布时间】:2020-07-12 23:59:31
【问题描述】:

我们有两个使用 APOC 加载 neo4j 的单独文件。首先,加载节点,这似乎工作正常。现在我需要加载关系。我有这个 json 文件进来。

{
"0": {
    "child": "54690",
    "compile": "webgoat.combined.source",
    "parent": "0",
    "tree": "runs"
},
"1": {
    "child": "2",
    "compile": "webgoat.combined.source",
    "parent": "1",
    "tree": "calls"
},

parent 和 child 是需要连接的两个节点。关系名称需要基于tree,并基于compile字段限定。换句话说,我只需要在父子共享相同编译字符串的情况下创建关系。

这是我目前的 APOC 命令:

    with graphDB_Driver.session() as ses:
        ses.run("UNWIND $batch AS row MATCH (a:ProgNode{parent:row.a}) MATCH (b:ProgNode{child:row.b}) CALL apoc.merge.relationship(a, 'TREE', {}, apoc.map.removeKeys(properties(row), ['parent', 'child']), b) YIELD rel RETURN 1", batch=BATCH["batch"])

我尝试用各种变量名替换“TREE”,包括 row.tree、tree:row.a,但似乎没有任何效果。我已经验证下面的 BATCH['batch'] 包含相同的数据。

[{"child": "54690", "compile": "webgoat.combined.source", "parent": "0", "tree": "runs", "from": 0}, {"child": "2", "compile": "webgoat.combined.source", "parent": "1", "tree": "calls", "from": 1}, {"child": "101", "compile": "webgoat.combined.source", "parent": "100", "tree": "runs", "from": 100}, {"child": "1001", "compile": "webgoat.combined.source", "parent": "1000", "tree": "runs", "from": 1000}, {"child": "10001", "compile": "webgoat.combined.source", "parent": "10000", "tree": "runs", "from": 10000}, {"child": "10004", "compile": "webgoat.combined.source", "parent": "10003", "tree": "runs", "from": 10003}, {"child": "10009", "compile": "webgoat.combined.source", "parent": "10004", "tree": "runs", "from": 10004}, {"child": "10007", "compile": "webgoat.combined.source", "parent": "10005", "tree": "runs", "from": 10005}, {"child": "10008", "compile": "webgoat.combined.source", "parent": "10007", "tree": "runs", "from": 10007}, {"child": "1005", "compile": "webgoat.combined.source", "parent": "1001", "tree": "runs", "from": 1001}, {"child": "1003", "compile": "webgoat.combined.source", "parent": "1002", "tree": "runs", "from": 1002}, {"child": "10028", "compile": "webgoat.combined.source", "parent": "10022", "tree": "runs", "from": 10022}, {"child": "10025", "compile": "webgoat.combined.source", "parent": "10023", "tree": "runs", "from": 10023}, {"child": "10026", "compile": "webgoat.combined.source", "parent": "10025", "tree": "runs", "from": 10025}, {"child": "10041", "compile": "webgoat.combined.source", "parent": "10028", "tree": "runs", "from": 10028}, {"child": "10034", "compile": "webgoat.combined.source", "parent": "10029", "tree": "runs", "from": 10029}, {"child": "10031", "compile": "webgoat.combined.source", "parent": "10030", "tree": "runs", "from": 10030}, {"child": "10040", "compile": "webgoat.combined.source", "parent": "10035", "tree": "runs", "from": 10035}, {"child": "10037", "compile": "webgoat.combined.source", "parent": "10036", "tree": "runs", "from": 10036}, {"child": "10051", "compile": "webgoat.combined.source", "parent": "10041", "tree": "runs", "from": 10041}, {"child": "10044", "compile": "webgoat.combined.source", "parent": "10042", "tree": "runs", "from": 10042}, {"child": "10045", "compile": "webgoat.combined.source", "parent": "10044", "tree": "runs", "from": 10044}, {"child": "10046", "compile": "webgoat.combined.source", "parent": "10045", "tree": "runs", "from": 10045}, {"child": "10050", "compile": "webgoat.combined.source", "parent": "10048", "tree": "runs", "from": 10048}, {"child": "10052", "compile": "webgoat.combined.source", "parent": "10051", "tree": "runs", "from": 10051}, {"child": "10071", "compile": "webgoat.combined.source", "parent": "10052", "tree": "runs", "from": 10052}, {"child": "10054", "compile": "webgoat.combined.source", "parent": "10053", "tree": "runs", "from": 10053}, {"child": "10061", "compile": "webgoat.combined.source", "parent": "10054", "tree": "runs", "from": 10054}, {"child": "10057", "compile": "webgoat.combined.source", "parent": "10055", "tree": "runs", "from": 10055}, {"child": "10059", "compile": "webgoat.combined.source", "parent": "10057", "tree": "runs", "from": 10057}, {"child": "10060", "compile": "webgoat.combined.source", "parent": "10059", "tree": "calls", "from": 10059}, {"child": "1007", "compile": "webgoat.combined.source", "parent": "1006", "tree": "runs", "from": 1006}, {"child": "10063", "compile": "webgoat.combined.source", "parent": "10062", "tree": "runs", "from": 10062}, {"child": "10065", "compile": "webgoat.combined.source", "parent": "10063", "tree": "runs", "from": 10063}, {"child": "10067", "compile": "webgoat.combined.source", "parent": "10065", "tree": "runs", "from": 10065}, {"child": "10068", "compile": "webgoat.combined.source", "parent": "10067", "tree": "runs", "from": 10067}, {"child": "10070", "compile": "webgoat.combined.source", "parent": "10068", "tree": "runs", "from": 10068}, {"child": "1009", "compile": "webgoat.combined.source", "parent": "1007", "tree": "runs", "from": 1007}, {"child": "10072", "compile": "webgoat.combined.source", "parent": "10071", "tree": "runs", "from": 10071}, {"child": "10074", "compile": "webgoat.combined.source", "parent": "10072", "tree": "runs", "from": 10072}, {"child": "10102", "compile": "webgoat.combined.source", "parent": "10088", "tree": "runs", "from": 10088}, {"child": "10090", "compile": "webgoat.combined.source", "parent": "10089", "tree": "runs", "from": 10089}, {"child": "1011", "compile": "webgoat.combined.source", "parent": "1009", "tree": "runs", "from": 1009}, {"child": "10093", "compile": "webgoat.combined.source", "parent": "10091", "tree": "runs", "from": 10091}, {"child": "10094", "compile": "webgoat.combined.source", "parent": "10093", "tree": "runs", "from": 10093}, {"child": "10097", "compile": "webgoat.combined.source", "parent": "10095", "tree": "runs", "from": 10095}, {"child": "10098", "compile": "webgoat.combined.source", "parent": "10097", "tree": "runs", "from": 10097}, {"child": "10100", "compile": "webgoat.combined.source", "parent": "10098", "tree": "runs", "from": 10098}, {"child": "103", "compile": "webgoat.combined.source", "parent": "101", "tree": "runs", "from": 101}, {"child": "10261", "compile": "webgoat.combined.source", "parent": "10102", "tree": "runs", "from": 10102}, {"child": "10107", "compile": "webgoat.combined.source", "parent": "10103", "tree": "runs", "from": 10103}, {"child": "10106", "compile": "webgoat.combined.source", "parent": "10105", "tree": "runs", "from": 10105}, {"child": "10129", "compile": "webgoat.combined.source", "parent": "10107", "tree": "runs", "from": 10107}, {"child": "10115", "compile": "webgoat.combined.source", "parent": "10108", "tree": "runs", "from": 10108}, {"child": "10110", "compile": "webgoat.combined.source", "parent": "10109", "tree": "runs", "from": 10109}, {"child": "1013", "compile": "webgoat.combined.source", "parent": "1011", "tree": "runs", "from": 1011}, {"child": "10114", "compile": "webgoat.combined.source", "parent": "10112", "tree": "runs", "from": 10112}, {"child": "10116", "compile": "webgoat.combined.source", "parent": "10115", "tree": "runs", "from": 10115}, {"child": "10117", "compile": "webgoat.combined.source", "parent": "10116", "tree": "runs", "from": 10116}, {"child": "10118", "compile": "webgoat.combined.source", "parent": "10117", "tree": "runs", "from": 10117}, {"child": "10126", "compile": "webgoat.combined.source", "parent": "10119", "tree": "runs", "from": 10119}, {"child": "10121", "compile": "webgoat.combined.source", "parent": "10120", "tree": "runs", "from": 10120}, {"child": "10125", "compile": "webgoat.combined.source", "parent": "10123", "tree": "runs", "from": 10123}, {"child": "10127", "compile": "webgoat.combined.source", "parent": "10126", "tree": "runs", "from": 10126}, {"child": "10128", "compile": "webgoat.combined.source", "parent": "10127", "tree": "runs", "from": 10127}, {"child": "10138", "compile": "webgoat.combined.source", "parent": "10129", "tree": "runs", "from": 10129}, {"child": "10131", "compile": "webgoat.combined.source", "parent": "10130", "tree": "runs", "from": 10130}, {"child": "10137", "compile": "webgoat.combined.source", "parent": "10133", "tree": "runs", "from": 10133}, {"child": "10135", "compile": "webgoat.combined.source", "parent": "10134", "tree": "runs", "from": 10134}, {"child": "10136", "compile": "webgoat.combined.source", "parent": "10135", "tree": "runs", "from": 10135}, {"child": "10139", "compile": "webgoat.combined.source", "parent": "10138", "tree": "runs", "from": 10138}, {"child": "10151", "compile": "webgoat.combined.source", "parent": "10139", "tree": "runs", "from": 10139}, {"child": "1045", "compile": "webgoat.combined.source", "parent": "1014", "tree": "runs", "from": 1014}, {"child": "10141", "compile": "webgoat.combined.source", "parent": "10140", "tree": "runs", "from": 10140}, {"child": "10143", "compile": "webgoat.combined.source", "parent": "10141", "tree": "runs", "from": 10141}, {"child": "10145", "compile": "webgoat.combined.source", "parent": "10143", "tree": "runs", "from": 10143}, {"child": "10146", "compile": "webgoat.combined.source", "parent": "10145", "tree": "runs", "from": 10145}, 

想法?

【问题讨论】:

  • 您的 JSON 文件包含一个 map 地图,而不是一个 list 地图。 batch 参数的值是多少?也是地图的地图,还是地图的列表?
  • 我为第一组负载添加了 BATCH["batch"]
  • 您添加到问题底部的数据不是列表。它由多个列表(不是单个列表)组成,并且列表之间没有逗号分隔。
  • 道歉,但不确定复制/粘贴对我做了什么,但我重新粘贴到代码格式中,它更好地保留了结构。无论如何,它是一个字典列表,并且格式中确实包含逗号。所以前一个是移动太快的神器。其他想法?
  • 新列表没有正确结束。有一个悬空的逗号,没有右方括号。即使该语法错误已修复,您的 Cypher 查询也会尝试使用地图的 ab 属性,但这些属性不存在。

标签: neo4j cypher neo4j-apoc


【解决方案1】:

解决了。正确的格式是将 a,b 的选择限定为 (a:ProgNode{inode:row.parent, compileunit:row.compile}),并简单地将 row.tree 添加到索引中。暂时全部关闭。感谢 Data Nostra 的 Maxime Guery

完整的查询最终是

        with graphDB_Driver.session() as ses:
        ses.run("UNWIND $batch AS row MATCH (a:ProgNode{inode:row.parent, compileunit:row.compile}) MATCH (b:ProgNode{inode:row.child, compileunit:row.compile}) CALL apoc.merge.relationship(a, row.tree, {compileunit:row.compile}, apoc.map.removeKeys(properties(row), ['parent', 'child']),b) YIELD rel RETURN 1", batch=BATCH["batch"])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多