批量插入节点和关系neo4jclient [英] Batch insert nodes and relations neo4jclient
问题描述
我在列表中有很多节点和边。目前,我正在遍历列表,并使用非常慢的查询插入每个节点。如何使用neo4jclient执行批量插入?
I have many nodes and edges in a list. Currently I'm looping through the list and inserting each node with a query which is very slow. How do I perform a batch insert using 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();
}
当前插入节点列表的方法:
Current method for inserting List of nodes:
List<myNode> nodeList;
foreach(var elem in nodeList)
addNode(client, elem, "foo");
推荐答案
不仅仅是将单个节点传递到Cypher中,您可以传递收藏。根据Neo4j手册
Instead of just passing a single node into your Cypher, you could pass in the collection. According to the Neo4j manual
通过为Cypher提供一系列地图,它将为每个地图创建一个节点
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.
因此,您的C#代码将得到简化
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();
}
希望有帮助。
这篇关于批量插入节点和关系neo4jclient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!