簡體   English   中英

Neo4jClient創建和更新關系

[英]Neo4jClient Create and Update a Relationship

我有一個可以通過Neo4jClient訪問的Neo4j Graphdatabase (它是Neo4j的REST api的.NET客戶端)

文檔的開頭。

我做了什么

與數據庫的連接有效。

Client = new GraphClient(new Uri("http://localhost:7474/db/data"));
Client.Connect();

這樣我可以插入節點...

Client.Create(new myNodeClass { name = "Nobody" });

...並查詢它們。

Node<myNodeClass> Node = Client.Get<WordNode>(138);
return Node.Data.name;

我想做的事

我只是想添加和更新節點之間的關系。 (關系的類型必須是數字。)

不幸的是,還沒有關於關系的文檔。

有一個名為CreateRelationship的命令。 但我無法讓它發揮作用。

Client.CreateRelationship(Neo4jClient.NodeReference<TSourceNode>, TRelationship);

你能給我一個添加和更新(數字)關系的例子嗎?

在測試用例中有很多東西......例如:

http://hg.readify.net/neo4jclient/src/4693da483a90/Test/ApiUsageIdeas.cs

我被卡住了然后我意識到我需要在CreateRelationship方法中指定源節點引用參數的類型參數。

在這個例子中,我創建了這種關系。 我還沒有更新這段關系。

披露(它在我的機器上作為運行visual studio 2012,YMMV的控制台應用程序工作)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using Neo4jClient;

namespace Neo4jClientExample
{

    class MyConsoleProgram
    {

        private GraphClient Client {get;set; }

        static void Main(string[] args)
        {

        try{
            GraphClient client = new GraphClient(new Uri("http://localhost:7474/db/data"));
            client.Connect();

            Us us = new Us { Name = "We are Us" };
            NodeReference<Us> usRef = client.Create(us);
            Console.WriteLine("us node.id: {0}", usRef.Id);

            var queryUs = client.Cypher.Start("n", "node(" + usRef.Id + ")").Return<Node<Us>>("n");
            Console.WriteLine("Us node name: {0}\n", queryUs.Results.AsEnumerable<Node<Us>>().First().Data);


            AllYourBase allYourBase = new AllYourBase { Name = "We are all your base" };
            NodeReference<AllYourBase> allYourBaseRef = client.Create(allYourBase);
            Console.WriteLine("AllYourBase node.id: {0}",allYourBaseRef.Id);

            var queryAllYourBase = client.Cypher.Start("n", "node(" + allYourBaseRef.Id + ")").Return<Node<AllYourBase>>("n");
            Console.WriteLine("AllYourBase node name: {0}\n", queryAllYourBase.Results.AsEnumerable<Node<AllYourBase>>().First().Data);

            RelationshipReference areBelongToRef = client.CreateRelationship(allYourBaseRef, new AreBelongTo(usRef));

            var query = client.Cypher.Start("allyourbase", "node(" + allYourBaseRef.Id + ")").Match("allyourbase-[:ARE_BELONG_TO]->us").Return<Node<AllYourBase>>("allyourbase");
            query.ExecuteWithoutResults();
            Console.WriteLine("Result of querying for all your base that belongs to us: {0}", query.Results.AsEnumerable<Node<AllYourBase>>().First().Data.Name);
        }
        catch(Exception ex)
        {
            Console.WriteLine("{0}", ex.Message);
            Console.WriteLine("{0}", ex.InnerException);
        }
        Console.ReadKey();
    }
}

public class Us
{
    public string Name {get; set;}

    public Us()
    {
    }
}

public class AllYourBase
{
    public string Name { get; set; }

    public AllYourBase()
    {
    }
}
public class AreBelongTo : Relationship, IRelationshipAllowingSourceNode<AllYourBase>,
                                         IRelationshipAllowingTargetNode<Us>
{
    public AreBelongTo(NodeReference targetNode)
        : base(targetNode)
    {}

    public const string TypeKey = "ARE_BELONG_TO";

    public override string RelationshipTypeKey
    {
        get { return TypeKey; }
    }
}

您可以查看測試, http://hg.readify.net/neo4jclient/src/4693da483a90/Test/RelationshipTests.cs或聯系Neo4j郵件列表上的作者groups.google.com/group/neo4j?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM