Neo4j有效地添加多个节点和边缘

时间:2018-06-08 23:25:15

标签: neo4j neo4jclient

我有以下示例。

Example

我想知道在单个事务中添加节点和边缘列表的最佳和最快方法是什么?我使用标准的C#Neo4j .NET软件包,但对Neo4jClient开放,因为我读的更快。任何支持.NET和4.5的东西都是诚实的。

我有一个大约60000个FooA对象的列表,需要添加到Neo4j中,可能需要几个小时!

首先,FooB对象几乎没有变化所以我不必每天都添加它们。性能问题是每天两次添加新的FooA对象。

每个FooA对象都有一个FooB对象列表,其中有两个列表,其中包含我需要添加的关系; RelA和RelB(见下文)。

public class FooA
{
  public long Id {get;set;} //UniqueConstraint
  public string Name {get;set;}
  public long Age {get;set;}
  public List<RelA> ListA {get;set;}
  public List<RelB> ListB {get;set;}
}

public class FooB
{
  public long Id {get;set;} //UniqueConstraint
  public string Prop {get;set;}
}

public class RelA
{
      public string Val1 {get;set;} 
      pulic NodeTypeA Node {get;set;
}

public class RelB
{
 public FooB Start {get;set;}
 public FooB End {get;set;}
 public string ValExample {get;set;} 

}

目前,我通过Id匹配检查节点'A'是否存在。如果是,那么我完全跳过并移动到下一个项目。如果没有,我创建具有自己属性的节点'A'。然后我创建具有自己独特属性的边。

每件商品都有不少交易。通过Id匹配节点 - &gt;添加节点 - &gt;添加边缘。

    foreach(var ntA in FooAList)
    {
        //First transaction.
        MATCH (FooA {Id: ntA.Id)})

        if not exists
        {
           //2nd transaction
           CREATE (n:FooA {Id: 1234, Name: "Example", Age: toInteger(24)})

           //Multiple transactions.
           foreach (var a in ListA)
           {
              MATCH (n:FooA {Id: ntA.Id}), (n2:FooB {Id: a.Id }) with n,n2 LIMIT 1
              CREATE (n)-[:RelA {Prop: a.Val1}]-(n2)
           }

            foreach (var b in Listb)
            {
               MATCH (n:FooB {Id: b.Start.Id}), (n2:FooB {Id: b.End.Id }) with n,n2 LIMIT 1
               CREATE (n)-[:RelA {Prop: b.ValExample}]-(n2)
            }
         }

如何使用例如Neo4jClient和UNWIND或除CSV导入之外的任何其他方式添加FooA列表。

希望有意义,谢谢!

1 个答案:

答案 0 :(得分:1)

最大的问题是嵌套列表,这意味着您必须执行foreach循环,因此最终执行最少 4个查询 FooA,其中60,000 - 很好 - 这很多!

快速注释RE:索引

首先,您需要在IdFooA节点的FooB属性上建立索引,这样可以大大加快您的查询速度。

我已经玩了一些,并且它存储了60,000个FooA条目,并且在我老化的计算机上大约12-15秒内创建了96,000个RelB实例。

解决方案

我把它分成了2个部分--FooA和RelB:

FooA

我必须将FooA规范化我可以在Neo4jClient中使用 - 所以让我们来介绍一下:

public class CypherableFooA
{
    public CypherableFooA(FooA fooA){
        Id = fooA.Id;
        Name = fooA.Name;
        Age = fooA.Age;
    }

    public long Id { get; set; }
    public string Name { get; set; }
    public long Age { get; set; }

    public string RelA_Val1 {get;set;}
    public long RelA_FooBId {get;set;}
}

我添加了RelA_Val1RelA_FooBId属性,以便能够在UNWIND中访问它们。我使用辅助方法转换FooA

public static IList<CypherableFooA> ConvertToCypherable(FooA fooA){
    var output = new List<CypherableFooA>();

    foreach (var element in fooA.ListA)
    {
        var cfa = new CypherableFooA(fooA);
        cfa.RelA_FooBId = element.Node.Id;
        cfa.RelA_Val1 = element.Val1;
        output.Add(cfa);
    }

    return output;
}

这结合:

var cypherable = fooAList.SelectMany(a => ConvertToCypherable(a)).ToList();

展开FooA个实例,因此我CypherableFooA的{​​{1}}属性中的每个项目最终只有ListAFooA。例如如果每个ListA FooA中有2个项目,并且您有5,000个FooA个实例,那么最终会得到包含10,000个项目的cypherable

现在,使用cypherable我调用AddFooAs方法:

public static void AddFooAs(IGraphClient gc, IList<CypherableFooA> fooAs, int batchSize = 10000, int startPoint = 0)
{
    var batch = fooAs.Skip(startPoint).Take(batchSize).ToList();
    Console.WriteLine($"FOOA--> {startPoint} to {batchSize + startPoint} (of {fooAs.Count}) = {batch.Count}");

    if (batch.Count == 0)
        return;

    gc.Cypher
        .Unwind(batch, "faItem")
        .Merge("(fa:FooA {Id: faItem.Id})")
        .OnCreate().Set("fa = faItem")
        .Merge("(fb:FooB {Id: faItem.RelA_FooBId})")
        .Create("(fa)-[:RelA {Prop: faItem.RelA_Val1}]->(fb)")
        .ExecuteWithoutResults();

    AddFooAs(gc, fooAs, batchSize, startPoint + batch.Count);
}

这会将查询批量分批处理10,000个(默认情况下) - 这大约需要5-6秒 - 就像我一次尝试全部60,000个一样。

RelB基因

您使用RelB在示例中存储了FooA,但您正在编写的查询根本不使用FooA,所以我所做的就是提取并展平RelB属性中的所有ListB个实例:

var relBs = fooAList.SelectMany(a => a.ListB.Select(lb => lb));

然后我将它们添加到Neo4j中:

public static void AddRelBs(IGraphClient gc, IList<RelB> relbs, int batchSize = 10000, int startPoint = 0)
{
    var batch = relbs.Select(r => new { StartId = r.Start.Id, EndId = r.End.Id, r.ValExample }).Skip(startPoint).Take(batchSize).ToList();
    Console.WriteLine($"RELB--> {startPoint} to {batchSize + startPoint} (of {relbs.Count}) = {batch.Count}");
    if(batch.Count == 0)
        return;

    var query = gc.Cypher
        .Unwind(batch, "rbItem")
        .Match("(fb1:FooB {Id: rbItem.StartId}),(fb2:FooB {Id: rbItem.EndId})")
        .Create("(fb1)-[:RelA {Prop: rbItem.ValExample}]->(fb2)");

    query.ExecuteWithoutResults();
    AddRelBs(gc, relbs, batchSize, startPoint + batch.Count);
}

再次,批量默认为10,000。

显然,时间会有所不同,具体取决于ListBListA中的相关人数 - 我的测试在ListA中有一项,在ListB中有2项。

相关问题