批量插入节点和关系neo4jclient

我在列表中有很多节点和边。 目前我正在循环遍历列表并使用非常慢的查询插入每个节点。 如何使用neo4jclient执行批量插入?

节点对象:

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

插入节点的当前方法:

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

插入节点列表的当前方法:

 List nodeList; foreach(var elem in nodeList) addNode(client, elem, "foo"); 

您可以传入集合,而不是仅将单个节点传递到Cypher。 根据Neo4j手册

通过为Cypher提供一组地图,它将为每个地图创建一个节点

请参阅Neo4j手册v2.2.2中的“ 为其属性创建具有参数的多个节点”一 节 。

因此,您的C#代码将变得简化并且应该更好。

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

希望有所帮助。

为了完整起见,以下是一个示例,可以使用neo4jclient调整批量加载关系及其属性。

 public void CreateRelationships(List 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(); } 

关系是RelationshipCommon类型的List集合。 RelationshipCommon具有以下结构

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

在我的开发虚拟机上,此代码在6秒内加载了54000个关系。