Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating nodes and relationships at the same time in neo4j

Tags:

neo4j

cypher

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. The cypher code i am currently using is that i first create the nodes with the code:

Create (node1:type {name:'example1', type:'example2'})

Around 4000 of that example with unique nodes.

Then I've got relationships stated as such:

Create
(node1)-[:r]-(node51),
(node2)-[:r]-(node5),
(node3)-[:r]-(node2);

Around 40000 of such unique relationships.

With smaller scale graphs this has not been any problem at all. But with this one, the Executing query never stops loading.

Any suggestions on how I can make this type of query work? Or what i should do instead?

edit. What I'm trying to build is a big graph over a product, with it's releases, release versions, features etc. in the same way as the Movie graph example is built.

The product has about 6 releases in total, each release has around 20 releaseversion. In total there is 371 features and of there 371 features there is also 438 featureversions. ever releaseversion (120 in total) then has around 2-300 featureversions each. These Featureversions are mapped to its Feature whom has dependencies towards a little bit of everything in the db. I have also involed HW dependencies such as the possible hw to run these Features on, releases on etc. so basicaly im using cypher code such as:

Create (Product1:Product {name:'ABC', type:'Product'})
Create (Release1:Release {name:'12A', type:'Release'})
Create (Release2:Release {name:'13A, type:'release'})
Create (ReleaseVersion1:ReleaseVersion {name:'12.0.1, type:'ReleaseVersion'})
Create (ReleaseVersion2:ReleaseVersion {name:'12.0.2, type:'ReleaseVersion'})    

and below those i've structured them up using

Create (Product1)<-[:Is_Version_Of]-(Release1),
(Product1)<-[:Is_Version_Of]-(Release2),
(Release2)<-[:Is_Version_Of]-(ReleaseVersion21),        

All the way down to features, and then I've also added dependencies between them such as:

(Feature1)-[:Requires]->(Feature239),
(Feature239)-[:Requires]->(Feature51);       

Since i had to find all this information from many different excel-sheets etc, i made the code this way thinking i could just put it together in one mass cypher query and run it on the /browser on the localhost. it worked really good as long as i did not use more than 4-5000 queries at a time. Then it created the entire database in about 5-10 seconds at maximum, but now when I'm trying to run around 45000 queries at the same time it has been running for almost 24 hours, and are still loading and saying "executing query...". I wonder if there is anyway i can improve the time it takes, will the database eventually be created? or can i do some smarter indexes or other things to improve the performance? because by the way my cypher is written now i cannot divide it into pieces since everything in the database has some sort of connection to the product. Do i need to rewrite the code or is there any smooth way around?

like image 757
ErikOstergren Avatar asked Apr 28 '14 09:04

ErikOstergren


People also ask

Can you store data as nodes and relationships in Neo4j?

Neo4j is often described as schema optional, meaning that it is not necessary to create indexes and constraints. You can create data — nodes, relationships and properties — without defining a schema up front.

What are nodes and relationships in Neo4j?

Neo4j's Property Graphs are composed of nodes and relationships, either of which may have properties. Nodes represent entities, for example concepts, events, places and things. Relationships connect pairs of nodes. However, nodes and relationships can be considered as low-level building blocks.

How do you create nodes and edges in Neo4j?

In Neo4j to create relationship between nodes you have to use the CREATE statement like we used to create nodes. lets create relation between two already created nodes.


1 Answers

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.

like image 149
FrobberOfBits Avatar answered Sep 23 '22 03:09

FrobberOfBits