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

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

问题描述

Neo4j(ID(node))给出的ID不稳定,其行为有点像SQL中的行号.由于ID主要用于SQL中的关系,并且可以在Neo4j中轻松建模,因此ID似乎用处不大,但是如何解决特定节点的检索呢?对于每个Web应用程序来说,拥有一个REST API(应该为每个节点具有唯一的路由)(例如/api/concept/23)似乎是一个非常标准的情况. 但是,尽管它是如此基础,但我发现的唯一可行方法是通过

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. 在像PHP这样的应用程序级别使用UUID生成器: 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天全站免登陆