Observação
O acesso a essa página exige autorização. Você pode tentar entrar ou alterar diretórios.
O acesso a essa página exige autorização. Você pode tentar alterar os diretórios.
本文介绍了在创建 Azure Cosmos DB 容器时定义 unique 键的不同方法。 目前可以使用Azure portal或通过其中一个 SDK 执行此作。
使用Azure portal
登录到 Azure portal。
创建新的 Azure Cosmos DB 帐户或选择现有帐户。
打开 Data Explorer 窗格,然后选择要使用的容器。
单击“新建容器”。
在“添加容器”对话框中,单击“+ 添加唯一键”,添加唯一键条目 。
输入唯一键约束的路径
如果需要,可通过单击“+ 添加唯一键”,添加更多唯一键条目
Azure 门户上的唯一键约束项截图
使用 PowerShell
若要创建具有唯一键的容器,请参阅创建具有唯一键和 TTL 的 Azure Cosmos DB 容器
使用 .NET SDK
使用 .NET SDK v2 创建新容器时,可以使用 UniqueKeyPolicy 对象来定义唯一键约束。
client.CreateDocumentCollectionAsync(UriFactory.CreateDatabaseUri("database"), new DocumentCollection
{
Id = "container",
PartitionKey = new PartitionKeyDefinition { Paths = new Collection<string>(new List<string> { "/myPartitionKey" }) },
UniqueKeyPolicy = new UniqueKeyPolicy
{
UniqueKeys = new Collection<UniqueKey>(new List<UniqueKey>
{
new UniqueKey { Paths = new Collection<string>(new List<string> { "/firstName", "/lastName", "/emailAddress" }) },
new UniqueKey { Paths = new Collection<string>(new List<string> { "/address/zipCode" }) }
})
}
});
使用 Java SDK
使用 Java SDK 创建新容器时时,可以使用 UniqueKeyPolicy 对象来定义唯一键约束。
// create a new DocumentCollection object
DocumentCollection container = new DocumentCollection();
container.setId("container");
// create array of strings and populate them with the unique key paths
Collection<String> uniqueKey1Paths = new ArrayList<String>();
uniqueKey1Paths.add("/firstName");
uniqueKey1Paths.add("/lastName");
uniqueKey1Paths.add("/emailAddress");
Collection<String> uniqueKey2Paths = new ArrayList<String>();
uniqueKey2Paths.add("/address/zipCode");
// create UniqueKey objects and set their paths
UniqueKey uniqueKey1 = new UniqueKey();
UniqueKey uniqueKey2 = new UniqueKey();
uniqueKey1.setPaths(uniqueKey1Paths);
uniqueKey2.setPaths(uniqueKey2Paths);
// create a new UniqueKeyPolicy object and set its unique keys
UniqueKeyPolicy uniqueKeyPolicy = new UniqueKeyPolicy();
Collection<UniqueKey> uniqueKeys = new ArrayList<UniqueKey>();
uniqueKeys.add(uniqueKey1);
uniqueKeys.add(uniqueKey2);
uniqueKeyPolicy.setUniqueKeys(uniqueKeys);
// set the unique key policy
container.setUniqueKeyPolicy(uniqueKeyPolicy);
// create the container
client.createCollection(String.format("/dbs/%s", "database"), container, null);
使用 Node.js SDK
使用 Node.js SDK 创建新容器时,可以使用 UniqueKeyPolicy 对象来定义唯一键约束。
client.database('database').containers.create({
id: 'container',
uniqueKeyPolicy: {
uniqueKeys: [
{ paths: ['/firstName', '/lastName', '/emailAddress'] },
{ paths: ['/address/zipCode'] }
]
}
});
使用 Python SDK
使用 Python SDK 创建新容器时,可以将唯一键约束指定为作为参数传递的字典的一部分。
client.CreateContainer('dbs/' + config['DATABASE'], {
'id': 'container',
'uniqueKeyPolicy': {
'uniqueKeys': [
{'paths': ['/firstName', '/lastName', '/emailAddress']},
{'paths': ['/address/zipCode']}
]
}
})
使用 Go SDK
使用 Go SDK 创建新容器时,可以使用 UniqueKeyPolicy 来定义唯一键约束。
db, _ := c.NewDatabase("demodb")
pkDef := azcosmos.PartitionKeyDefinition{
Paths: []string{"/state"},
Kind: azcosmos.PartitionKeyKindHash,
}
uniqueKeyPolicy := azcosmos.UniqueKeyPolicy{
UniqueKeys: []azcosmos.UniqueKey{
{
Paths: []string{"/firstName", "/lastName", "/emailAddress"},
},
},
}
_, err = db.CreateContainer(context.Background(), azcosmos.ContainerProperties{
ID: "demo_container",
PartitionKeyDefinition: pkDef,
UniqueKeyPolicy: &uniqueKeyPolicy,
}, nil)