UUID
该库支持手动和自动化生成 UUID,这些 UUID 可以作为属性存储在节点上。
UUID 使用 Java randomUUID 实用方法 生成,该方法生成 v4UUID。
UUID 可以编码为众所周知的十六进制表示字符串(32 个字符,例如 1051af4f-b81d-4a76-8605-ecfb8ef703d5
)或 Base64 编码字符串(22 个字符,例如 vX8dM5XoSe2ldoc/QzMEyw
)
自动 UUID
还有一些过程通过 UUID 处理程序生命周期来处理自动添加 UUID 属性。UUID 处理程序是一个事务事件处理程序,它会自动将 UUID 属性添加到提供的标签和指定的属性名称。请查看以下文档以获取详细说明。
所有这些过程(除了 list 和 show 过程)都旨在系统数据库中执行,因此必须通过打开系统数据库会话来执行。有几种方法可以做到这一点: - 使用 cypher-shell 或 Neo4j Browser 时,可以在 Cypher 查询前加上 此外,它们接受第一个参数为我们想要安装/更新/移除自动 UUID 的数据库名称。通过这种实现,我们可以利用集群路由机制在集群环境中使用这些过程。 |
安装、更新或移除自动 UUID 是一个最终一致性操作。因此,它们不会立即添加/更新/移除,而是有一个由 Apoc 配置 |
首先在 $NEO4J_HOME/config/apoc.conf
中启用 apoc.uuid.enabled=true
或 apoc.uuid.enabled.[DATABASE_NAME]=true
。
配置值 apoc.uuid.format
允许您选择不同的 UUID 编码方法:hex
(默认选项)或 base64
。
限定名称 | 类型 | 发布版本 |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
UUID 示例
创建自动 UUID
这些示例假设我们在 neo4j
数据库上,并且想要在该数据库中创建自动 UUID。
添加 uuid
CALL apoc.uuid.setup('Person')
YIELD label, installed, properties
RETURN label, installed, properties
请注意,apoc.uuid.setup
以及 apoc.uuid.drop
、apoc.uuid.dropAll
都必须在系统数据库中执行。
结果是
label | installed | properties |
---|---|---|
"Person" |
true |
|
执行 apoc.uuid.setup
过程时,会自动执行一个创建约束的查询(如果不存在),(同样在此情况下,在 apoc.uuid.refresh
配置定义的时间后):CREATE CONSTRAINT IF NOT EXISTS FOR (n:<label>) REQUIRE (n.<uuidProperty>) IS UNIQUE
然后,在配置 apoc.uuid.refresh
定义的时间后,可以执行以下查询
CREATE (n:Person {name: 'Daniel'})-[:Work]->(:Company {name: 'Neo4j'})
结果将是具有 2 个属性的 :Person
节点

如果选择默认配置 addToExistingNodes: true
,则在后台(通过 apoc.periodic.iterate
过程)所有现有节点也将填充具有 uuid 值的属性。执行完成后,将打印包含查询结果的日志,如下所示
Result of batch computation obtained from existing nodes for UUID handler with label `MyLabel`:
{failedParams={}, committedOperations=1, batch={total=10, committed=10, failed=0, errors={}}, wasTerminated=false, batches=1, timeTaken=0, retries=0, errorMessages={}, total=1, operations={total=10, committed=10, failed=0, errors={}}, failedOperations=0, updateStatistics={nodesDeleted=0, labelsAdded=0, relationshipsCreated=0, nodesCreated=0, propertiesSet=1, relationshipsDeleted=0, labelsRemoved=0}, failedBatches=0}
自动 UUID 列表
可以返回数据库中所有自动 UUID 的完整列表。例如,如果在以下查询中创建了 UUID
CALL apoc.uuid.setup('TestShow')
然后可以运行(同样在此情况下,在配置 apoc.uuid.refresh
定义的时间后)
CALL apoc.uuid.show()
label | installed | properties | "Person" |
---|
请注意,由于自动 UUID 操作是最终一致的(基于 |
移除自动 UUID
CALL apoc.uuid.drop('Person')
YIELD label, installed, properties
RETURN label, installed, properties
结果是
label | installed | properties |
---|---|---|
"Person" |
false |
{uuidProperty → "uuid", addToExistingNodes → true} |
您也可以通过调用以下过程来移除所有已安装的 uuid
CALL apoc.uuid.dropAll()
YIELD label, installed, properties
RETURN label, installed, properties
结果是
label | installed | properties |
---|---|---|
"Person" |
false |
{uuidProperty → "uuid", addToExistingNodes → true} |
导出元数据
要将 uuid 导入到另一个数据库(例如在 |