Batch insert nodes and neo4jclient relationships

I have many nodes and edges in the list. I am currently browsing through a list and inserting each node with a very slow query. How to perform batch insert using neo4jclient?

Node object:

public class myNode { public int id { get; set; } public int floor { get; set; } public double x { get; set; } public double y { get; set; } } 

Current method for inserting node:

 public static void addNode(GraphClient client, myNode node, string nodeName) { client.Cypher .Create("(" + nodeName + ":Node {node})") .WithParams(new { node }) .ExecuteWithoutResults(); } 

Current method for inserting a list of nodes:

 List<myNode> nodeList; foreach(var elem in nodeList) addNode(client, elem, "foo"); 
+5
source share
2 answers

Instead of just skipping one node into your Cypher, you can pass it to the collection. According to Neo4j manual

Providing Cypher with an array of maps, it will create a node for each map

See Create multiple nodes with a parameter for their properties in Neo4j Manual v2.2.2 .

Therefore, your C # code will be simplified and will work better.

 public static void AddNodes(GraphClient client, List<MyNode> nodes) { client.Cypher .Create("(n:Node {nodes})") .WithParams(new { nodes }) .ExecuteWithoutResults(); } 

Hope this helps.

+5
source

For completeness, an example is given that can be adapted to mass load relationships and their properties using neo4jclient.

 public void CreateRelationships(List<RelationshipCommon> relationships) { graphClient.Cypher .Unwind(relationships, "relationship") .Match("(grant:GRANT)", "(target:THEME)") .Where("grant.node_id = relationship.SourceEntityId and target.node_id = relationship.TargetEntityId") .Create("grant-[r:IS_IN_THEME]->target") .Set("r.relationship_id = relationship.RelationshipId") .Set("r.grant_proportional_value = relationship.ProportionalValue") .ExecuteWithoutResults(); } 

Relationships is a List collection of type RelationshipCommon. RelationshipCommon has the following structure

  public class RelationshipCommon { public string SourceEntityId { get; set; } public string TargetEntityId { get; set; } public string RelationshipId { get; set; } public long ProportionalValue { get; set; } } 

In my VM development, this code loaded 54,000 relationships into 6s.

+1
source

All Articles