Neo4j中所有节点/关系的可靠(自动)递增标识符 [英] Reliable (auto)incrementing identifiers for all nodes/relationships in Neo4j

查看:83
本文介绍了Neo4j中所有节点/关系的可靠(自动)递增标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种基于递增计数器(不是大的uuid)为Neo4j中的所有节点/关系生成唯一标识符的方法.

I'm looking for a way to generate unique identifiers for all my nodes/relationships in Neo4j, based on an incrementing counter (not big long uuids).

已知Neo4j引擎维护的内部ID作为外部引用是不可靠的.

The internal ids maintained by the Neo4j engine are known not to be reliable as outside references.

最接近的解决方案是此问题中提出的代码,但是它当单个CREATE子句创建多个新节点时不起作用:

A solution that comes close is the code proposed in this question, but it doesn't work when a single CREATE clause creates multiple new nodes:

// 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 a new node attached to every existing :something node
MATCH (n:something)
CREATE (:somethingRelated {id:uid}) -[:rel]-> (n)

当有多个(n:something)时,每个新创建的(:somethingRelated)将共享相同的ID.仅使用Cypher可以解决此问题吗?

When there are multiple (n:something), every newly created (:somethingRelated) will share the same id. Is there some way around this, using only Cypher?

推荐答案

尝试使用此方法分配ID块:

Try this to allocate a block of ids:

// collect nodes to connect
MATCH (n:Crew) WITH collect(n) AS nodes
MERGE (id:UniqueId { name:'Person' })
// reserve id-range
SET id.count = coalesce(id.count,0)+ size(nodes)
WITH nodes, id.count - size(nodes) AS base 
// for each index
UNWIND range(0,size(nodes)-1) AS idx
// get node, compute id
WITH nodes[idx] AS n, base +idx AS id
CREATE (:SomethingRelated { uid:id })-[:rel]->(n)

这篇关于Neo4j中所有节点/关系的可靠(自动)递增标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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