Creating nodes and relationships at the same time in neo4j

前端 未结 4 637
夕颜
夕颜 2021-02-08 06:06

I am trying to build an database in Neo4j with a structure that contains seven different types of nodes, in total around 4-5000 nodes and between them around 40000 relationships

相关标签:
4条回答
  • 2021-02-08 06:31

    You can create multiple nodes and relationships interlinked with a single create statement, like this:

    create (a { name: "foo" })-[:HELLO]->(b {name : "bar"}),
           (c {name: "Baz"})-[:GOODBYE]->(d {name:"Quux"});
    

    So that's one approach, rather than creating each node individually with a single statement, then each relationship with a single statement.

    You can also create multiple relationships from objects by matching first, then creating:

    match (a {name: "foo"}), (d {name:"Quux"}) create (a)-[:BLAH]->(d);
    

    Of course you could have multiple match clauses, and multiple create clauses there.

    You might try to match a given type of node, and then create all necessary relationships from that type of node. You have enough relationships that this is going to take many queries. Make sure you've indexed the property you're using to match the nodes. As your DB gets big, that's going to be important to permit fast lookup of things you're trying to create new relationships off of.

    You haven't specified which query you're running that isn't "stopping loading". Update your question with specifics, and let us know what you've tried, and maybe it's possible to help.

    0 讨论(0)
  • 2021-02-08 06:31

    Another interesting approach might be to generate your statements directly in Excel, see http://blog.bruggen.com/2013/05/reloading-my-beergraph-using-in-graph.html?view=sidebar for an example. You can run a lot of CREATE statements in one transaction, so this should not be overly complicated.

    0 讨论(0)
  • 2021-02-08 06:43

    If you're able to use the Neo4j 2.1 prerelease milestones, then you should try using the new LOAD CSV and PERIODIC COMMIT features. They are designed for just this kind of use case.

    LOAD CSV allows you to describe the structure of your data with one or more Cypher patterns, while providing the values in CSV to avoid duplication.

    PERIODIC COMMIT can help make large imports more reliable and also improve performance by reducing the amount of memory that is needed.

    0 讨论(0)
  • 2021-02-08 06:53

    If you have one of the nodes already created then a simple approach would be:

    MATCH (n: user {uid: "1"}) CREATE (n) -[r: posted]-> (p: post {pid: "42", title: "Good Night", msg: "Have a nice and peaceful sleep.", author: n.uid});
    

    Here the user node already exists and you have created a new relation and a new post node.

    0 讨论(0)
提交回复
热议问题