简体   繁体   English

Neo4jClient创建和更新关系

[英]Neo4jClient Create and Update a Relationship

I have a Neo4j Graphdatabase with access via the Neo4jClient . 我有一个可以通过Neo4jClient访问的Neo4j Graphdatabase (It is a .NET client for the REST api of Neo4j) (它是Neo4j的REST api的.NET客户端)

There is the beginning of a documentation . 文档的开头。

What I have done 我做了什么

The connection to the database works. 与数据库的连接有效。

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

This way I can insert Nodes... 这样我可以插入节点...

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

... and query them. ...并查询它们。

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

What I want to do 我想做的事

I simply want to add and update relationships between Nodes. 我只是想添加和更新节点之间的关系。 (The type of relationship have to be numeric.) (关系的类型必须是数字。)

Unfortunately there is no documentation about relationships yet. 不幸的是,还没有关于关系的文档。

There is a command named CreateRelationship . 有一个名为CreateRelationship的命令。 But I can't get it work. 但我无法让它发挥作用。

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

Can you give me an example of adding and updating (numeric) relationships? 你能给我一个添加和更新(数字)关系的例子吗?

There's a lot to be found in the test cases... Such as this: 在测试用例中有很多东西......例如:

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

I was stuck too then I realized I needed to specify the type parameter of the source node reference parameter in the CreateRelationship method. 我被卡住了然后我意识到我需要在CreateRelationship方法中指定源节点引用参数的类型参数。

In this example, I have created the relationship. 在这个例子中,我创建了这种关系。 I have not yet updated the relationship. 我还没有更新这段关系。

Disclosure(It works on my machine as a console application running visual studio 2012, YMMV) 披露(它在我的机器上作为运行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