Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j create multiple nodes and relationships

Tags:

neo4j

cypher

I am using Neo4j to build a huge graph database (over a million nodes). The way I'm doing it right now is running a cypher CREATE (n {property:'value'}) query for each of the node. As expected, this is quite an inefficient method and it takes lots of time. Can somebody suggest me some alternative method to overcome this issue? I've heard that Neo4j also provides some default batch interface to create multiple nodes. I'm currently using this version of code (including the relationship):

create (a { name: "a" })-[:rel1]->(b {name : "b"}),(c {name: "c"})-[:rel2]->(d {name:"d"}),...

Is it an efficient method or are there any better methods? Thanks in advance! :)

like image 487
AnotherCodingEnthusiast Avatar asked Jul 07 '14 13:07

AnotherCodingEnthusiast


2 Answers

Two alternatives I'd consider:

  1. Use the LOAD CSV support: http://docs.neo4j.org/chunked/stable/query-load-csv.html
  2. Use a single paramaterized Cypher statement, and supply an array of parameters: http://docs.neo4j.org/chunked/stable/query-create.html#create-create-multiple-nodes-with-a-parameter-for-their-properties

Otherwise there is the batch inserter, as mentioned.

like image 82
Chris Leishman Avatar answered Sep 23 '22 13:09

Chris Leishman


There is an overview on the Neo4j website: http://www.neo4j.org/develop/import

But to be short, for more than a million nodes I would say:

If you can code in Java:

http://docs.neo4j.org/chunked/stable/batchinsert.html

else:

https://github.com/jexp/batch-import

like image 21
Ron van Weverwijk Avatar answered Sep 24 '22 13:09

Ron van Weverwijk