apoc.uuid.install

存储过程 Apoc Extended 已弃用

CALL apoc.uuid.install(label, {addToExistingNodes: true/false, uuidProperty: 'uuid'}) yield label, installed, properties, batchComputationResult | 它将为提供的 labeluuidProperty 添加 UUID 事务处理程序,如果 UUID 处理程序已存在,它将被新的处理程序替换

签名

apoc.uuid.install(label :: STRING?, config = {} :: MAP?) :: (batchComputationResult :: MAP?, label :: STRING?, installed :: BOOLEAN?, properties :: MAP?)

请注意,此存储过程已弃用。

请改用以下存储过程,它们在集群中提供更好的支持

已弃用的存储过程 新存储过程

apoc.uuid.install('<name>', $config)

apoc.uuid.setup('<name>', '<query>', '<dbName>', $config)

apoc.uuid.remove('<name>')

apoc.uuid.drop('<name>', '<dbName>')

apoc.uuid.removeAll()

apoc.uuid.removeAll('<dbName>')

其中 <dbName> 是我们想要执行自动 UUID 的数据库,默认值为 "neo4j"。

此过程不适用于集群环境,使用时可能会出现不可预期的行为。

输入参数

名称 类型 默认

标签 (label)

STRING?

null

config

MAP?

{}

配置参数

该过程支持以下配置参数

表 1. 配置参数
名称 (name) type 默认 description(描述)

addToExistingNodes

布尔值

true

将 UUID 值添加到现有节点。将会覆盖现有值。

addToSetLabels

布尔值

false

即使在设置了标签时也添加 UUID 值。例如: MATCH (p:OtherLabel) SET p:LabelWithUuid

uuidProperty

字符串

"uuid"

UUID 值的属性键

输出参数

名称 类型

batchComputationResult

MAP?

标签 (label)

STRING?

installed

布尔值?

属性

MAP?

启用自动 UUID

此存储过程属于一组通过 UUID 处理程序生命周期处理自动添加 UUID 属性的存储过程。UUID 处理程序是一个事务事件处理程序,它会自动为提供的标签和属性名称添加 UUID 属性。

默认情况下,自动添加 UUID 是禁用的。我们可以通过在 apoc.conf 中设置以下属性来启用它

apoc.conf
apoc.uuid.enabled=true

使用示例

我们需要为想要添加 UUID 的标签和属性创建唯一约束。

如果我们尝试在没有添加约束的情况下设置 Person 标签节点的 UUID 创建,我们将得到如下所示的异常

CALL apoc.uuid.install("Person");
表 2. 结果

无法调用存储过程 apoc.uuid.install:原因:java.lang.RuntimeException:找不到标签 Person 的约束,请使用以下语句添加约束: CREATE CONSTRAINT FOR (person:Person) REQUIRE person.uuid IS UNIQUE

我们可以通过运行以下查询在 (Person, uuid) 标签/属性对上创建约束

CREATE CONSTRAINT FOR (person:Person)
REQUIRE person.uuid IS UNIQUE;

现在我们可以通过运行以下查询自动为所有新节点以及现有节点添加 UUID

CALL apoc.uuid.install("Person");

默认情况下,UUID 值将被添加到现有节点并覆盖现有值。我们可以传递配置 addToExistingNodes: false 以仅将 UUID 添加到新节点。

表 3. 结果

batchComputationResult

标签 (label)

installed

属性

{failedParams: {}, committedOperations: 3, batch: {total: 1, committed: 1, failed: 0, errors: {}}, wasTerminated: FALSE, batches: 1, timeTaken: 0, retries: 0, errorMessages: {}, total: 3, operations: {total: 3, committed: 3, failed: 0, errors: {}}, failedOperations: 0, failedBatches: 0}

"Person"

TRUE

{uuidProperty: "uuid"}

现在让我们创建一个新的 Person 节点;

CREATE (:Person {name: "Tom Hanks"});

如果我们查找所有 Person 节点,我们将看到它有一个 uuid 属性

MATCH (p:Person {name: "Tom Hanks"})
RETURN p;
表 4. 结果
p

(:Person {name: "Tom Hanks", uuid: "cec34337-9709-46af-bbb7-9e0742d8aaa7"})

addToSetLabels 配置设置为 true 时,uuid 属性也会在标签 SET 时创建。例如

CREATE (:AnotherLabel {name: "Tom Hanks"});
// ...
MATCH (n:AnotherLabel) SET n:Person;

如果我们想为 UUID 值使用不同的属性键,我们可以传入 uuidProperty 键,不要忘记先设置约束

CREATE CONSTRAINT FOR (person:Person)
REQUIRE person.myUUID IS UNIQUE;
CALL apoc.uuid.install("Person", {uuidProperty: "myUUID"});
表 5. 结果

batchComputationResult

标签 (label)

installed

属性

{failedParams: {}, committedOperations: 1, batch: {total: 1, committed: 1, failed: 0, errors: {}}, wasTerminated: FALSE, batches: 1, timeTaken: 0, retries: 0, errorMessages: {}, total: 1, operations: {total: 1, committed: 1, failed: 0, errors: {}}, failedOperations: 0, failedBatches: 0}

"Person"

TRUE

{uuidProperty: "myUUID"}

现在让我们再次查找那些 Person 节点

MATCH (p:Person {name: "Tom Hanks"})
RETURN p;
表 6. 结果
p

(:Person {name: "Tom Hanks", uuid: "cec34337-9709-46af-bbb7-9e0742d8aaa7", myUUID: "d09f177b-ff91-4eb9-aac0-73e7a850c9ba"})

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