Batch insert nodes and relations neo4jclient

我的未来我决定 提交于 2019-12-05 07:52:47

Instead of just passing a single node into your Cypher, you could pass in the collection. According to the Neo4j manual

By providing Cypher an array of maps, it will create a node for each map

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

Therefore your C# code will become simplified and should perform better.

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

Hope that helps.

For completeness the following is an example that can be adapted to bulk 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();
}

The relationships are 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; }
}

On my development VM this code loaded 54000 relationships in 6s.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!