UUID

该库支持手动和自动生成 UUID,这些 UUID 可以存储为节点的属性。

UUID 是使用 Java randomUUID 工具方法生成的,该方法会生成一个 v4 UUID

UUID 可以编码为常见的十六进制字符串(32 个字符,例如 1051af4f-b81d-4a76-8605-ecfb8ef703d5)或 Base64 编码(22 个字符,例如 vX8dM5XoSe2ldoc/QzMEyw)。

自动 UUID

此外还有一些通过 UUID 生命周期处理器来处理自动添加 UUID 属性的过程。UUID 处理器是一个事务事件处理器,它会自动为指定的标签和指定的属性名称添加 UUID 属性。请查看以下文档以获取深入描述。

所有这些过程(列出和显示过程除外)都旨在系统数据库中执行,因此必须通过开启系统数据库会话来执行。有几种实现方式:- 使用 cypher-shell 或 Neo4j Browser 时,可以在 Cypher 查询前加上 :use system - 使用 fabric 时,可以在 Cypher 查询前加上 USE system - 使用驱动程序时,可以直接针对系统数据库开启会话

此外,它们接受数据库名称作为第一个参数,指定要在其中安装/更新/移除自动 UUID 的目标数据库。通过此实现,我们可以利用集群路由机制在集群环境中使用这些过程。

安装、更新或删除自动 UUID 是一个最终一致性操作。因此,它们不会立即被添加/更新/删除,而是由 Apoc 配置 apoc.uuid.refresh=<毫秒数> 控制其刷新频率。

首先需在 $NEO4J_HOME/config/apoc.conf 中启用 apoc.uuid.enabled=trueapoc.uuid.enabled.[数据库名称]=true

配置值 apoc.uuid.format 允许您选择不同的 UUID 编码方法:hex(默认选项)或 base64

限定名称 类型 版本

apoc.uuid.setup

CALL apoc.uuid.setup(label, databaseName, $config) | 最终会为提供的 `label` 和 `uuidProperty` 添加 UUID 事务处理器。如果 UUID 处理器已存在,它将被新的处理器替换。

过程

Apoc Extended

apoc.uuid.drop

CALL apoc.uuid.drop(label, databaseName) yield label, installed, properties | 最终删除先前添加的 UUID 处理程序并返回 uuid 信息

过程

Apoc Extended

apoc.uuid.dropAll

CALL apoc.uuid.dropAll(databaseName) yield label, installed, properties | 最终删除所有先前添加的 UUID 处理程序并返回 uuids 信息

过程

Apoc Extended

apoc.uuid.list

CALL apoc.uuid.list() yield label, installed, properties | 提供所有已安装的 uuid 处理程序列表及其相关配置

过程

Apoc Extended

apoc.uuid.show

CALL apoc.uuid.show(databaseName) | 列出数据库中所有最终安装的 UUID 处理程序

过程

Apoc Extended

UUID 示例

创建自动 UUID

本示例假设我们位于 neo4j 数据库中,并且希望在该数据库中创建自动 UUID。

添加 uuid

CALL apoc.uuid.setup('Person')
YIELD label, installed, properties
RETURN label, installed, properties

请注意,apoc.uuid.setup 以及 apoc.uuid.dropapoc.uuid.dropAll 必须在系统数据库(system database)中执行。

结果为:

标签 (label) installed 属性

"Person"

true

{uuidProperty → "uuid", addToExistingNodes → 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 节点

apoc.uuid.result

如果选择了默认配置 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()
表 1. 结果
标签 (label) installed 属性 "Person"

请注意,由于自动 UUID 操作是最终一致的(基于 apoc.uuid.refresh 配置),apoc.uuid.show 可能会返回一些尚未添加/更新/删除的 UUID。要获取当前安装的所有 UUID 列表,请使用 apoc.uuid.list

删除自动 UUID

CALL apoc.uuid.drop('Person')
YIELD label, installed, properties
RETURN label, installed, properties

结果为:

标签 (label) installed 属性

"Person"

false

{uuidProperty → "uuid", addToExistingNodes → true}

您还可以通过调用以下过程来删除所有已安装的 uuid:

CALL apoc.uuid.dropAll()
YIELD label, installed, properties
RETURN label, installed, properties

结果为:

标签 (label) installed 属性

"Person"

false

{uuidProperty → "uuid", addToExistingNodes → true}

导出元数据

要在另一个数据库中导入 uuid(例如在执行 ./neo4j-admin backup/neo4j-admin restore 之后),请参阅 apoc.systemdb.export.metadata 过程。

© . This site is unofficial and not affiliated with Neo4j, Inc.