2015-06-16 57 views
5

我在列表中有許多節點和邊。目前,我正在循環查看列表並使用非常慢的查詢插入每個節點。如何使用neo4jclient執行批量插入?批處理插入節點和關係neo4jclient

節點對象:

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

當前方法用於插入節點的列表::

List<myNode> nodeList; 
foreach(var elem in nodeList) 
    addNode(client, elem, "foo"); 
+0

與其他答案類似的問題在這裏:https://stackoverflow.com/q/47360571/237509 – JOG

回答

5

而不是僅僅使單個節點

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

插入節點當前方法進入你的Cypher,你可以通過收集。根據Neo4j的手動

通過提供Cypher支架的地圖的陣列,它會爲每一個地圖

的節點請參閱部分與它們在Neo4j Manual v2.2.2屬性的參數創建的多個節點。

因此,您的C#代碼將變得簡化,應該表現更好。

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

希望有所幫助。

+0

我只是在發佈這個問題後出於好奇才試過這個,它令我驚訝,它工作!很高興知道這是解決方案。但有一個問題,我無法弄清楚在這之後如何在這些節點之間添加一批關係。我構建了用於一次添加所有節點和關係的查詢字符串,但這有點違背了使用ORM的許多目的。 – codeln

+0

@codeln很高興你有相同的解決方案。問問你能否將標記作爲讓其他人清楚的答案?至於你的其他問題 - 我會用更多的細節開始另一個問題。如果我能幫忙,我會的。 – ceej

+0

:D當然,感謝您的幫助。如果你找到關係的解決方案,請回到我身邊(該問題確實需要批量插入節點和關係) – codeln

1

爲了完整起見,以下是一個可以使用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(); 
} 

這些關係是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; } 
} 

在我的開發VM上,這段代碼在6s中加載了54000個關係。