gpt4 book ai didi

Neo4jClient:对CRUD API的疑惑

转载 作者:行者123 更新时间:2023-12-02 07:01:53 24 4
gpt4 key购买 nike

我的持久层基本上使用 Neo4jClient 来访问 Neo4j 1.9.4 数据库。更具体地说,为了创建节点,我在 Neo4jClient 的 CRUD API 中使用了 IGraphClient#Create(),为了查询图表,我使用了 Neo4jClient 的 Cypher 支持。

一切都很好,直到我的一个 friend 指出对于每个查询,我基本上都执行了两个 HTTP 请求:

  • 一个请求通过节点的唯一 ID(不是它的节点 ID!而是 SnowMaker 生成的唯一 ID)从遗留索​​引中获取节点引用
  • 一个 Cypher 查询,从该节点引用开始执行实际工作。

对于读取 操作,我做了显而易见的事情并将索引查找移动到我的Start() 调用中,即:

GraphClient.Cypher
.Start(new { user = Node.ByIndexLookup("User", "Id", userId) })
// ... the rest of the query ...

另一方面,对于创建 操作,我认为这实际上是不可能的。我的意思是:Create() 方法需要一个 POCO、几个关系实例和几个索引条目,以便在一个事务/HTTP 中创建一个节点、它的关系和它的索引条目要求。问题是您传递给关系实例的节点引用:它们来自哪里?来自以前的 HTTP 请求,对吧?

我的问题:

  1. 我能否使用 CRUD API 通过其 ID 查找节点 A,从 POCO 创建节点 B,在 A 和 B 之间创建关系并在一个请求中将 B 的 ID 添加到遗留索引?
  2. 如果不行,还有什么选择? CRUD API 是否被视为遗留代码,我们是否应该转向基于 Cypher 的 Neo4j 2.0 方法?
  3. 这种基于 Cypher 的方法是否意味着我们失去了创建操作的 POCO 到节点的转换?那很方便。

此外,能否更新 Neo4jClient 的文档,因为坦率地说,它很差。我确实意识到 Readify 还提供商业支持,因此可以解释一些事情。

谢谢!

最佳答案

我是 Neo4jClient 的作者。 (免费赠送软件的人。)

问题 1a:

"Can I use the CRUD API to look up node A by its ID, create node B from a POCO, create a relationship between A and B"

Cypher 不仅是 future 的方式,也是“现在”的方式。

从 Cypher 开始(有很多相关资源):

START user=node:user(Id: 1234)
CREATE user-[:INVITED]->(user2 { Id: 4567, Name: "Jim" })
Return user2

然后将其转换为 C#:

graphClient.Cypher
.Start(new { user = Node.ByIndexLookup("User", "Id", userId) })
.Create("user-[:INVITED]->(user2 {newUser})")
.WithParam("newUser", new User { Id = 4567, Name = "Jim" })
.Return(user2 => user2.Node<User>())
.Results;

这里还有很多类似的例子:https://github.com/Readify/Neo4jClient/wiki/cypher-examples

问题 1b:

" and add B's ID to a legacy index in one request?"

不,Cypher 不支持遗留索引。如果你真的想继续使用它们,那么你应该坚持使用 CRUD API。没关系:如果您想使用遗留索引,请使用遗留 API。

Q2.

"If not, what is the alternative? Is the CRUD API considered legacy code and should we move towards a Cypher-based Neo4j 2.0 approach?"

这正是您想要做的。 Cypher,带有标签和自动索引:

// One time op to create the index
// Yes, this syntax is a bit clunky in C# for now
graphClient.Cypher
.Create("INDEX ON :User(Id)")
.ExecuteWithoutResults();

// Find an existing user, create a new one, relate them,
// and index them, all in a single HTTP call
graphClient.Cypher
.Match("(user:User)")
.Where((User user) => user.Id == userId)
.Create("user-[:INVITED]->(user2 {newUser})")
.WithParam("newUser", new User { Id = 4567, Name = "Jim" })
.ExecuteWithoutResults();

这里有更多例子:https://github.com/Readify/Neo4jClient/wiki/cypher-examples

Q3.

"Does this Cypher-based approach mean that we lose POCO-to-node translation for create operations? That was very convenient."

正确。但这是我们所有人都想做的事情,Neo4j 的发展方向,以及 Neo4jClient 的发展方向。

想一想 SQL(我假设您很熟悉)。您是否运行查询来查找节点的内部标识符,包括其在磁盘上的文件偏移量,然后在第二个查询中使用此内部标识符来操作它?不需要。您只需运行一次查询即可完成所有操作。

现在,人们喜欢传递 Node<T>NodeReference 实例的一个常见用例是减少查询中的重复。这是一个合理的担忧,但是因为 .NET 中的流畅查询是不可变的,我们可以构建一个基本查询:

public ICypherFluentQuery FindUserById(long userId)
{
return graphClient.Cypher
.Match("(user:User)")
.Where((User user) => user.Id == userId);
// Nothing has been executed here: we've just built a query object
}

然后像这样使用它:

public void DeleteUser(long userId)
{
FindUserById(userId)
.Delete("user")
.ExecuteWithoutResults();
}

或者,添加更多的 Cypher 逻辑来删除所有关系:

然后像这样使用它:

public void DeleteUser(long userId)
{
FindUserById(userId)
.Match("user-[:?rel]-()")
.Delete("rel, user")
.ExecuteWithoutResults();
}

通过这种方式,您可以有效地重用引用,而无需首先通过网络将它们拉回。

关于Neo4jClient:对CRUD API的疑惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19494846/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com