【问题标题】:Bulk Insertion in Py2neoPy2neo 中的批量插入
【发布时间】:2021-03-29 08:21:55
【问题描述】:

我正在为 mongo-connector 编写一个自定义文档管理器,以将 mongodb 文档复制到 neo4j。在这里,我想创建批量关系。我正在使用 py2neo2020.0。

以前的版本似乎有一些选项,但在这个版本中没有。有什么方法可以在 py2neo 中创建批量节点和关系

【问题讨论】:

    标签: neo4j py2neo mongo-connector


    【解决方案1】:

    我目前正在开发批量加载功能。下一个版本将提供一些新功能。在此之前,Cypher UNWIND...CREATE 查询是您提高性能的最佳选择。

    【讨论】:

      【解决方案2】:

      我强烈建议切换到 neo4j Python 驱动程序,因为 Neo4j 直接支持它。

      无论如何,您也可以直接在 Cypher 中进行批量插入,和/或使用 neo4j 驱动程序从 Python 中调用该 Cypher。

      我建议先导入节点,然后再导入关系。如果您有保证节点的唯一标识符,这将很有帮助,因为您可以在加载之前在该属性上设置索引。然后您可以像这样从 CSV(或者更好的是 TSV)文件加载节点:

      // Create constraint on the unique ID - greatly improves performance.
      CREATE CONSTRAINT ON (a:my_label) ASSERT a.id IS UNIQUE
      ;
      
      // Load the nodes, along with any properties you might want, from
      // a file in the Neo4j import folder.
      USING PERIODIC COMMIT 1000
      LOAD CSV WITH HEADERS FROM "file:///my_nodes.tsv" AS tsvLine FIELDTERMINATOR '\t'
      CREATE (:my_label{id: toInteger(tsvLine.id), my_field2: tsvLine.my_field2})
      ;
      
      // Load relationships.
      USING PERIODIC COMMIT 1000
      LOAD CSV WITH HEADERS FROM "file:///my_relationships.tsv" AS tsvLine FIELDTERMINATOR '\t'
          MATCH(parent_node:my_label)
              WHERE parent_node.id = toInteger(tsvLine.parent)
          MATCH(child_node:my_label)
              WHERE child_node.id = toInteger(tsvLine.child)
          CREATE(parent_node) --> (child_node)
      ;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-15
        • 2012-12-29
        相关资源
        最近更新 更多