Neo4j 中的自增属性 [英] Auto increment property in Neo4j

查看:30
本文介绍了Neo4j 中的自增属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,Neo4j (ID(node)) 给出的 ID 不稳定并且表现得有点像 SQL 中的行号.由于 ID 主要用于 SQL 中的关系,并且这些在 Neo4j 中很容易建模,因此 ID 似乎没有太大用处,但是您如何解决特定节点的检索问题?拥有一个应该为每个节点(例如 /api/concept/23)提供唯一路由的 REST API 似乎是 Web 应用程序的一个非常标准的案例.但尽管它如此基础,我发现的唯一可行的方法是通过

As far as I understand it the IDs given by Neo4j (ID(node)) are unstable and behave somewhat like row numbers in SQL. Since IDs are mostly used for relations in SQL and these are easily modeled in Neo4j, there doesn't seem to be much use for IDs, but then how do you solve retrieval of specific nodes? Having a REST API which is supposed to have unique routes for each node (e.g. /api/concept/23) seems like a pretty standard case for web applications. But despite it being so fundamental, the only viable way I found were either via

  • 特定于语言的框架
  • 作为维护增量的未连接节点:
// get unique id
MERGE (id:UniqueId{name:'Person'})
ON CREATE SET id.count = 1
ON MATCH SET id.count = id.count + 1
WITH id.count AS uid
// create Person node
CREATE (p:Person{id:uid,firstName:'Gabriel',lastName:'Smith'})
RETURN p AS person

来源:http://www.neo4j.org/graphgist?8012859

真的没有更简单的方法吗?如果没有,是否有特殊原因?我的方法是 Neo4j 上下文中的反模式吗?

Is there really not a simpler way and if not, is there a particular reason for it? Is my approach an anti-pattern in the context of Neo4j?

推荐答案

Neo4j 内部 id 比 sql 行 id 更稳定一点,因为它们在事务期间永远不会改变,例如

Neo4j internal ids are a bit more stable than sql row id's as they will never change during a transaction for e.g.

并且确实不建议将它们暴露给外部使用.我知道 Neo 内部有一些意图来实现这样的功能.

And indeed exposing them for external usage is not recommended. I know there are some intentions at Neo internals to implement such a feature.

基本上人们倾向于使用两种解决方案:

Basically people tend to use two solutions for this :

  1. 在应用程序级别使用 UUID 生成器,例如 PHP:https://packagist.org/packages/rhumsaa/uuid 并在所有节点上添加标签/uuid 唯一约束.

  1. Using a UUID generator at the application level like for PHP : https://packagist.org/packages/rhumsaa/uuid and add a label/uuid unique constraint on all nodes.

使用非常少的 Neo4j 插件,例如 https://github.com/graphaware/neo4j-uuid这将动态添加 uuid 属性,因此它消除了在应用程序级别处理它的负担,并且更容易管理节点对象的持久性状态

Using a very handful Neo4j plugin like https://github.com/graphaware/neo4j-uuid that will add uuid properties on the fly, so it remove you the burden to handle it at the application level and it is easier to manage the persistence state of your node objects

这篇关于Neo4j 中的自增属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆